Luceneのインデックスデータベースを作成します

パッケージcom.hope.lucene 

輸入org.apache.commons.io.FileUtils
輸入org.apache.lucene.document.Document ;
輸入org.apache.lucene.document.Field ;
輸入org.apache.lucene.document.TextField ;
輸入org.apache.lucene.index.IndexWriter ;
輸入org.apache.lucene.index.IndexWriterConfig ;
輸入org.apache.lucene.store.Directory
輸入org.apache.lucene.store.FSDirectory
輸入org.junit。テスト;

インポートのjava.io.File ;


/ **
* @author newcityman
* @date 2020年1月15日-夜12時01
* /
パブリッククラスLuceneFirst {
@Test ます。public void CREATEINDEX()例外{スローオブジェクトを作成するには、// 1、ディレクターを、ライブラリの場所を保存インデックス指定されたディスク保存する//インデックスデータベースディレクトリディレクトリを= FSDirectory。オープン(新新しいファイル(" G:\\ workspace_idea3 \\ Luceneの\\ TEMP \\ インデックス「)toPath()); // 2、ディレクトリベースのオブジェクト、目標IndexWriter作成IndexWriter IndexWriter = 新しい新しいIndexWriter(ディレクトリ、新しい新しいIndexWriterConfig()); // 3、ディスク上のファイルを読んで、各ファイルに対応する文書オブジェクトの作成ファイルのファイルを= 新しい新しいファイル(「G:\\ workspace_idea3 \\ のLucene







\\ TEMP \\ searchsource「);
ファイル[] = File.listFilesファイル();
(ファイルFのために:ファイル){
//ファイル名取得
のString fileNameに= f.getNameを(); //ファイルのパスを取得する文字列filePathに= f.getPath(); //ファイルの内容を取得する文字列含むFileContent =のfileutilsを。readFileToString(F "UTF-8"); //ファイルサイズロングファイルサイズ=のfileutils。はsizeof(F); //フィールド、作成TextFieldをフィールド名= 新しい新しいテキストフィールド(「名前」は、ファイル名、Field.Store YES) テキストフィールドザfieldPath = 新しい新しいテキストフィールド(「パス」









、ファイルパス、Field.Store。YES)
テキストフィールドfieldContent = 新しい新しいテキストフィールド("コンテンツ" 、含むFileContent 、Field.Store。YES)
テキストフィールドは、fieldSize = 新しい新しいテキストフィールド("サイズ" 、ファイルサイズ+ "" 、Field.Store。YES) ドキュメントオブジェクトフィールドに追加// 4は、//文書を作成する文書ドキュメント= 新しい新しい文書(); (フィールド名)document.add ; (fieldPath)document.add ; (fieldContent)document.add ; (は、fieldSize)document.add // 5、インデックスデータベースに書き込まれたドキュメントオブジェクトindexWriter.addDocument(文書)










}
// 6、近いindexWriterオブジェクトは
(indexWriter.close);
}
}

おすすめ

転載: www.cnblogs.com/newcityboy/p/12194766.html
おすすめ