Lucene-search-operation
Lucene-検索操作
検索プロセスは、Luceneが提供するコア機能の1つです。 次の図は、プロセスとその使用法を示しています。 IndexSearcherは、検索プロセスの中核コンポーネントの1つです。
最初に_indexes_を含む_Directory_を作成し、それを_IndexReader_を使用して_Directory_を開く_IndexSearcher_に渡します。 次に、_Term_を使用して_Query_を作成し、_Query_をサーチャーに渡すことで、_IndexSearcher_を使用して検索を行います。 _IndexSearcher_は、検索操作の結果である_Document_のドキュメントIDとともに検索の詳細を含む_TopDocs_オブジェクトを返します。
ここでは、段階的なアプローチを示し、基本的な例を使用してインデックス作成プロセスを理解するのに役立ちます。
QueryParserを作成する
QueryParserクラスは、ユーザーが入力した入力をLuceneの理解可能な形式のクエリに解析します。 QueryParserを作成するには、次の手順に従います-
- ステップ1 *-QueryParserのオブジェクトを作成します。
- ステップ2 *-このクエリを実行するバージョン情報とインデックス名を持つ標準アナライザーで作成されたQueryParserオブジェクトを初期化します。
QueryParser queryParser;
public Searcher(String indexDirectoryPath) throws IOException {
queryParser = new QueryParser(Version.LUCENE_36,
LuceneConstants.CONTENTS,
new StandardAnalyzer(Version.LUCENE_36));
}
IndexSearcherを作成する
IndexSearcherクラスは、検索プロセス中に検索者がインデックスを作成するコアコンポーネントとして機能します。 IndexSearcherを作成するには、次の手順に従います-
- ステップ1 *-IndexSearcherのオブジェクトを作成します。
- ステップ2 *-インデックスを保存する場所をポイントするLuceneディレクトリを作成します。
- ステップ3 *-インデックスディレクトリで作成されたIndexSearcherオブジェクトを初期化します。
IndexSearcher indexSearcher;
public Searcher(String indexDirectoryPath) throws IOException {
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
indexSearcher = new IndexSearcher(indexDirectory);
}
検索する
検索を行うには、次の手順に従ってください-
- ステップ1 *-QueryParserを介して検索式を解析することにより、Queryオブジェクトを作成します。
- ステップ2 *-IndexSearcher.search()メソッドを呼び出して検索を行います。
Query query;
public TopDocs search( String searchQuery) throws IOException, ParseException {
query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
ドキュメントを入手する
次のプログラムは、ドキュメントを取得する方法を示しています。
public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc);
}
IndexSearcherを閉じる
次のプログラムは、IndexSearcherを閉じる方法を示しています。
public void close() throws IOException {
indexSearcher.close();
}
応用例
検索プロセスをテストするためのテストLuceneアプリケーションを作成しましょう。
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApplication under a package com.finddevguides.lucene as explained in the Lucene - First Application chapter. You can also use the project created in Lucene - First Application chapter as such for this chapter to understand the searching process. |
2 | Create LuceneConstants.java,TextFileFilter.java and Searcher.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");
}
}
Searcher.java
このクラスは、未加工データで作成されたインデックスを読み取り、Luceneライブラリを使用してデータを検索するために使用されます。
package com.finddevguides.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Searcher {
IndexSearcher indexSearcher;
QueryParser queryParser;
Query query;
public Searcher(String indexDirectoryPath) throws IOException {
Directory indexDirectory =
FSDirectory.open(new File(indexDirectoryPath));
indexSearcher = new IndexSearcher(indexDirectory);
queryParser = new QueryParser(Version.LUCENE_36,
LuceneConstants.CONTENTS,
new StandardAnalyzer(Version.LUCENE_36));
}
public TopDocs search( String searchQuery)
throws IOException, ParseException {
query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc);
}
public void close() throws IOException {
indexSearcher.close();
}
}
LuceneTester.java
このクラスは、Luceneライブラリの検索機能をテストするために使用されます。
package com.finddevguides.lucene;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
public class LuceneTester {
String indexDir = "E:\\Lucene\\Index";
String dataDir = "E:\\Lucene\\Data";
Searcher searcher;
public static void main(String[] args) {
LuceneTester tester;
try {
tester = new LuceneTester();
tester.search("Mohan");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void search(String searchQuery) throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
TopDocs hits = searcher.search(searchQuery);
long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits +
" documents found. Time :" + (endTime - startTime) +" ms");
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc);
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
}
データおよびインデックスディレクトリの作成
record1.txtという名前の10個のテキストファイルを使用して、学生の名前とその他の詳細を含むrecord10.txtを使用し、ディレクトリE:\ Lucene \ Dataに配置しました。 link:/lucene/data.zip [テストデータ]。 インデックスディレクトリパスは、E:\ Lucene \ Indexとして作成する必要があります。 Lucene-Indexing Process の章でインデックス作成プログラムを実行すると、そのフォルダで作成されたインデックスファイルのリストを見ることができます。
プログラムを実行する
ソース、生データ、データディレクトリ、インデックスディレクトリ、インデックスの作成が完了したら、プログラムをコンパイルして実行することで続行できます。 これを行うには、 LuceneTester.Java ファイルタブをアクティブにして、Eclipse IDEで使用可能な実行オプションを使用するか、 Ctrl + F11 を使用して LuceneTesterapplication をコンパイルおよび実行します。 アプリケーションが正常に実行されると、Eclipse IDEのコンソールに次のメッセージが出力されます-
1 documents found. Time :29 ms
File: E:\Lucene\Data\record4.txt