Lucene FuzzyQuery類

FuzzyQuery用於搜索使用模糊實現,它是一種基於編輯距離算法的近似搜索文件。

類聲明

以下是org.apache.lucene.search.FuzzyQuery類的聲明:

public class FuzzyQuery extends MultiTermQuery

字段

  • static int defaultMaxExpansions

  • static float defaultMinSimilarity

  • static int defaultPrefixLength

  • protected Term term

類的構造函數

S.N.

構造函數和說明

1

FuzzyQuery(Term term)
調用 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將匹配方面至少有最小的相似於詞條。

類方法

1

boolean equals(Object obj)

2

protected FilteredTermEnum getEnum(IndexReader reader)
構造所使用的枚舉,擴大模式的詞條。

3

float getMinSimilarity()
返回需要此查詢相匹配的最小相似度。

4

int getPrefixLength()
返回非模糊前綴長度。

5

Term getTerm()
返回模式詞條。

6

int hashCode()

7

String toString(String field)
打印查詢字符串,使用字段假定爲默認的字段中和省略。

方法繼承

這個類從以下類繼承的方法:

  • 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(); }

應用程序示例

讓我們創建一個測試Lucene的應用程序中使用FuzzyQuery測試搜索。

步驟

描述

1

創建名稱爲LuceneFirstApplication的項目在packagecom.yiibai.lucene下的Lucene用於解釋 Lucene應用程序理解搜索過程。

2

創建LuceneConstants.java和Searcher.java作爲Lucene的解釋- 第一應用程序一章。保持其它文件不變。

3

創建LuceneTester.java如下所述。

4

清理和構建應用程序,以確保業務邏輯按要求工作。

LuceneConstants.java

這個類是用來提供可應用於示例應用程序中使用的各種常量。

package com.yiibai.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.yiibai.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.yiibai.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的文件中包含簡單的名稱以及學生的其他細節,並把它們放在目錄E:LuceneData。這是測試數據。索引目錄路徑應創建爲E:LuceneIndex。期間,運行 Lucene索引程序後- 索引過程中,可以看到該文件夾中創建的索引文件的列表。

運行程序:

一旦創建源,創造了原始數據,數據目錄,索引目錄和索引完成後,已經準備好這一步是編譯和運行程序。要做到這一點,請LuceneTester.Java文件選項卡中使用Eclipse IDE可使用Run選項,或使用Ctrl+ F11來編譯和運行應用程序LuceneTester。如果您的應用程序一切正常,這將在Eclipse IDE的控制檯打印以下消息:

10 documents found. Time :78ms
Score: 1.3179655 File: E:LuceneData
ecord3.txt
Score: 0.790779 File: E:LuceneData
ecord1.txt
Score: 0.790779 File: E:LuceneData
ecord2.txt
Score: 0.790779 File: E:LuceneData
ecord4.txt
Score: 0.790779 File: E:LuceneData
ecord5.txt
Score: 0.790779 File: E:LuceneData
ecord6.txt
Score: 0.790779 File: E:LuceneData
ecord7.txt
Score: 0.790779 File: E:LuceneData
ecord8.txt
Score: 0.790779 File: E:LuceneData
ecord9.txt
Score: 0.2635932 File: E:LuceneData
ecord10.txt