Lucene-updatedocument
Lucene-ドキュメントの更新操作
更新プロセスは、インデックス作成プロセスの一部としてのもう1つの重要な操作です。 この操作は、すでにインデックス付けされたコンテンツが更新され、インデックスが無効になるときに使用されます。 この操作は、再インデックス付けとも呼ばれます。
_Field(s)_を含む_Document(s)_をIndexWriterに更新し、IndexWriterを使用してインデックスを更新します。
これから、段階的なアプローチを示し、基本的な例を使用してドキュメントを更新する方法を理解できるようにします。
ドキュメントをインデックスに更新する
ドキュメントをインデックスに更新するには、この手順に従ってください-
- ステップ1 *-更新されたテキストファイルからLuceneドキュメントを更新するメソッドを作成します。
private void updateDocument(File file) throws IOException {
Document document = new Document();
//update indexes for file contents
writer.updateDocument(new Term
(LuceneConstants.CONTENTS,
new FileReader(file)),document);
writer.close();
}
IndexWriterを作成する
IndexWriterを作成するには、次の手順に従ってください-
- ステップ1 *-IndexWriterクラスは、インデックス作成プロセス中にインデックスを作成/更新するコアコンポーネントとして機能します。
- ステップ2 *-IndexWriterのオブジェクトを作成します。
- ステップ3 *-インデックスを保存する場所を指す必要があるLuceneディレクトリを作成します。
- ステップ4 *-インデックスディレクトリで作成されたIndexWriterオブジェクトを初期化します。これは、バージョン情報とその他の必須/オプションパラメータを持つ標準アナライザです。
private IndexWriter writer;
public Indexer(String indexDirectoryPath) throws IOException {
//this directory will contain the indexes
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
//create the indexer
writer = new IndexWriter(indexDirectory,
new StandardAnalyzer(Version.LUCENE_36),true,
IndexWriter.MaxFieldLength.UNLIMITED);
}
ドキュメントを更新し、インデックス再作成プロセスを開始します
以下は、ドキュメントを更新する2つの方法です。
- * updateDocument(Term、Document)*-用語を含むドキュメントを削除し、デフォルトのアナライザー(インデックスライターの作成時に指定)を使用してドキュメントを追加します。
- * updateDocument(Term、Document、Analyzer)*-用語を含むドキュメントを削除し、提供されたアナライザーを使用してドキュメントを追加します。
private void indexFile(File file) throws IOException {
System.out.println("Updating index for "+file.getCanonicalPath());
updateDocument(file);
}
応用例
インデックス作成プロセスをテストするために、Luceneアプリケーションテストを作成しましょう。
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApplication under a packagecom.finddevguides.lucene as explained in the Lucene - First Application chapter. You can also use the project created in EJB - First Application chapter as such for this chapter to understand the indexing process. |
2 | Create LuceneConstants.java,TextFileFilter.java and Indexer.java as explained in the Lucene - First Application chapter. Keep the rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the application to make sure business logic is working as per the requirements. |
LuceneConstants.java
このクラスは、サンプルアプリケーション全体で使用されるさまざまな定数を提供するために使用されます。
package com.finddevguides.lucene;
public class LuceneConstants {
public static final String CONTENTS = "contents";
public static final String FILE_NAME = "filename";
public static final String FILE_PATH = "filepath";
public static final int MAX_SEARCH = 10;
}
TextFileFilter.java
このクラスは、*。txt *ファイルフィルターとして使用されます。
package com.finddevguides.lucene;
import java.io.File;
import java.io.FileFilter;
public class TextFileFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().endsWith(".txt");
}
}
Indexer.java
このクラスは、Luceneライブラリを使用して検索可能にするために、生データにインデックスを付けるために使用されます。
package com.finddevguides.lucene;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Indexer {
private IndexWriter writer;
public Indexer(String indexDirectoryPath) throws IOException {
//this directory will contain the indexes
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
//create the indexer
writer = new IndexWriter(indexDirectory,
new StandardAnalyzer(Version.LUCENE_36),true,
IndexWriter.MaxFieldLength.UNLIMITED);
}
public void close() throws CorruptIndexException, IOException {
writer.close();
}
private void updateDocument(File file) throws IOException {
Document document = new Document();
//update indexes for file contents
writer.updateDocument(
new Term(LuceneConstants.FILE_NAME,
file.getName()),document);
writer.close();
}
private void indexFile(File file) throws IOException {
System.out.println("Updating index: "+file.getCanonicalPath());
updateDocument(file);
}
public int createIndex(String dataDirPath, FileFilter filter)
throws IOException {
//get all files in the data directory
File[] files = new File(dataDirPath).listFiles();
for (File file : files) {
if(!file.isDirectory()
&& !file.isHidden()
&& file.exists()
&& file.canRead()
&& filter.accept(file)
){
indexFile(file);
}
}
return writer.numDocs();
}
}
LuceneTester.java
このクラスは、Luceneライブラリのインデックス機能をテストするために使用されます。
package com.finddevguides.lucene;
import java.io.IOException;
public class LuceneTester {
String indexDir = "E:\\Lucene\\Index";
String dataDir = "E:\\Lucene\\Data";
Indexer indexer;
public static void main(String[] args) {
LuceneTester tester;
try {
tester = new LuceneTester();
tester.createIndex();
} catch (IOException e) {
e.printStackTrace();
}
}
private void createIndex() throws IOException {
indexer = new Indexer(indexDir);
int numIndexed;
long startTime = System.currentTimeMillis();
numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
long endTime = System.currentTimeMillis();
indexer.close();
}
}
データおよびインデックスディレクトリの作成
ここでは、学生の名前やその他の詳細を含むrecord1.txtからrecord10.txtまでの10個のテキストファイルを使用し、ディレクトリE:\ Lucene \ Dataに配置しました。 link:/lucene/data.zip [テストデータ]。 インデックスディレクトリパスは、 E:\ Lucene \ Index として作成する必要があります。 このプログラムを実行すると、そのフォルダーに作成されたインデックスファイルのリストを見ることができます。
プログラムを実行する
ソース、生データ、データディレクトリ、インデックスディレクトリの作成が完了したら、プログラムのコンパイルと実行を進めることができます。 これを行うには、 LuceneTester.Java ファイルタブをアクティブのままにして、Eclipse IDEで使用可能な実行オプションを使用するか、 Ctrl + F11 を使用して LuceneTester アプリケーションをコンパイルおよび実行します。 アプリケーションが正常に実行されると、Eclipse IDEのコンソールに次のメッセージが出力されます-
Updating index for E:\Lucene\Data\record1.txt
Updating index for E:\Lucene\Data\record10.txt
Updating index for E:\Lucene\Data\record2.txt
Updating index for E:\Lucene\Data\record3.txt
Updating index for E:\Lucene\Data\record4.txt
Updating index for E:\Lucene\Data\record5.txt
Updating index for E:\Lucene\Data\record6.txt
Updating index for E:\Lucene\Data\record7.txt
Updating index for E:\Lucene\Data\record8.txt
Updating index for E:\Lucene\Data\record9.txt
10 File indexed, time taken: 109 ms
上記のプログラムを正常に実行すると、* indexディレクトリ*に次のコンテンツが表示されます-