Lucene-fuzzyquery
Lucene-FuzzyQuery
FuzzyQueryは、編集距離アルゴリズムに基づいた近似検索であるファジー実装を使用してドキュメントを検索するために使用されます。
クラス宣言
以下は org.apache.lucene.search.FuzzyQuery クラスの宣言です-
public class FuzzyQuery
extends MultiTermQuery
フィールド
以下は、FuzzyQueryのフィールドです-
- static int defaultMaxExpansions
- static float defaultMinSimilarity
- static int defaultPrefixLength *保護期間
クラスコンストラクター
次の表は、さまざまなクラスのコンストラクタを示しています-
S.No. | Constructor & Description |
---|---|
1 |
FuzzyQuery(term、0.5f、0、Integer.MAX_VALUE)を呼び出します |
2 |
FuzzyQuery(Term term, float minimumSimilarity) FuzzyQuery(term、minimumSimilarity、0、Integer.MAX_VALUE)を呼び出します |
3 |
FuzzyQuery(Term term, float minimumSimilarity, int prefixLength) FuzzyQuery(term、minimumSimilarity、prefixLength、Integer.MAX_VALUE)を呼び出します |
4 |
FuzzyQuery(Term term, float minimumSimilarity, int prefixLength, int maxExpansions) 少なくとも用語との類似性が最小の類似性を持つ用語に一致する新しいFuzzyQueryを作成します |
クラスメソッド
次の表は、さまざまなクラスメソッドを示しています-
S.No. | Method & Description |
---|---|
1 | *boolean equals(Object obj) * |
2 |
使用する列挙を構築し、パターン用語を展開します。 |
3 |
float getMinSimilarity() このクエリが一致するために必要な最小の類似性を返します。 |
4 |
int getPrefixLength() 非ファジープレフィックス長を返します。 |
5 |
Term getTerm() パターン用語を返します。 |
6 | *int hashCode() * |
7 |
クエリを文字列に出力します。フィールドはデフォルトフィールドと見なされ、省略されます。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- org.apache.lucene.search.MultiTermQuery
- org.apache.lucene.search.Query
- java.lang.Object
使用法
private void searchUsingFuzzyQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term = new Term(LuceneConstants.FILE_NAME, searchQuery);
//create the term query object
Query query = new FuzzyQuery(term);
//do the search
TopDocs hits = searcher.search(query);
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.print("Score: "+ scoreDoc.score + " ");
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
応用例
FuzzyQueryを使用して検索をテストするためのテスト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 searching process. |
2 | Create LuceneConstants.java and Searcher.java as explained in the Lucene - First Application chapter. Keep rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the application to make sure the 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;
}
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 TopDocs search(Query query) throws IOException, ParseException {
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.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
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.searchUsingFuzzyQuery("cord3.txt");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void searchUsingFuzzyQuery(String searchQuery)
throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term = new Term(LuceneConstants.FILE_NAME, searchQuery);
//create the term query object
Query query = new FuzzyQuery(term);
//do the search
TopDocs hits = searcher.search(query);
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.print("Score: "+ scoreDoc.score + " ");
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
}
データおよびインデックスディレクトリの作成
record1.txtからrecord10.txtまでの10個のテキストファイルを使用して、学生の名前やその他の詳細を含め、* E:\ Lucene \ Data。* link:/lucene/data.zip [Test Data]ディレクトリに配置しました。 インデックスディレクトリパスは、 E:\ Lucene \ Index として作成する必要があります。 Lucene-Indexing Process の章でインデックス作成プログラムを実行すると、そのフォルダで作成されたインデックスファイルのリストを見ることができます。
プログラムを実行する
ソース、生データ、データディレクトリ、インデックスディレクトリ、インデックスの作成が完了したら、プログラムをコンパイルして実行することで続行できます。 これを行うには、 LuceneTester.Java ファイルタブをアクティブのままにし、Eclipse IDEで使用可能な実行オプションを使用するか、 Ctrl + F11 を使用して* LuceneTester *アプリケーションをコンパイルおよび実行します。 アプリケーションが正常に実行されると、Eclipse IDEのコンソールに次のメッセージが出力されます-
10 documents found. Time :78ms
Score: 1.3179655 File: E:\Lucene\Data\record3.txt
Score: 0.790779 File: E:\Lucene\Data\record1.txt
Score: 0.790779 File: E:\Lucene\Data\record2.txt
Score: 0.790779 File: E:\Lucene\Data\record4.txt
Score: 0.790779 File: E:\Lucene\Data\record5.txt
Score: 0.790779 File: E:\Lucene\Data\record6.txt
Score: 0.790779 File: E:\Lucene\Data\record7.txt
Score: 0.790779 File: E:\Lucene\Data\record8.txt
Score: 0.790779 File: E:\Lucene\Data\record9.txt
Score: 0.2635932 File: E:\Lucene\Data\record10.txt