Lucene - 模糊查詢



FuzzyQuery 用於基於編輯距離演算法進行近似搜尋,即模糊匹配文件。

類宣告

以下是org.apache.lucene.search.FuzzyQuery類的宣告:

public class FuzzyQuery
   extends MultiTermQuery

欄位

以下是 FuzzyQuery 的欄位:

  • static int defaultMaxExpansions
  • static float defaultMinSimilarity
  • static int defaultPrefixLength
  • protected Term term

類建構函式

下表顯示了不同的類建構函式:

序號 建構函式 & 描述
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,它將匹配至少與 term 的相似度為 minimum Similarity 的術語。

類方法

下表顯示了不同的類方法:

序號 方法 & 描述
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的專案,位於com.tutorialspoint.lucene包下,如Lucene - 第一個應用程式章節所述。您也可以使用Lucene - 第一個應用程式章節中建立的專案,以便理解搜尋過程。
2 建立LuceneConstants.javaSearcher.java,如Lucene - 第一個應用程式章節所述。保持其餘檔案不變。
3 建立如下所示的LuceneTester.java
4 清理並構建應用程式,以確保業務邏輯按要求工作。

LuceneConstants.java

此類用於提供可在示例應用程式中使用的各種常量。

package com.tutorialspoint.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.tutorialspoint.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.tutorialspoint.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目錄中。測試資料。應建立索引目錄路徑為E:\Lucene\Index。在Lucene - 索引過程章節中執行索引程式後,您可以在該資料夾中看到建立的索引檔案列表。

執行程式

完成原始碼、原始資料、資料目錄、索引目錄和索引的建立後,您可以透過編譯和執行程式繼續進行。為此,請保持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
lucene_query_programming.htm
廣告
© . All rights reserved.