Lucene - 萬用字元查詢



WildcardQuery 用於使用萬用字元(如 '*' 代表任意字元序列,'?' 代表單個字元)搜尋文件。

類宣告

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

public class WildcardQuery 
   extends MultiTermQuery 

欄位

  • protected Term term

類建構函式

序號 建構函式 & 描述
1

WildcardQuery(Term term)

類方法

序號 方法 & 描述
1

boolean equals(Object obj)

2

protected FilteredTermEnum getEnum(IndexReader reader)

構建要使用的列舉,擴充套件模式項。

3

Term getTerm()

返回模式項。

4

int hashCode()

5

String toString(String field)

列印此查詢的使用者可讀版本。

繼承的方法

此類繼承自以下類:

  • org.apache.lucene.search.MultiTermQuery
  • org.apache.lucene.search.Query
  • java.lang.Object

用法

private void searchUsingWildCardQuery(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 WildcardQuery(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.println("File: "+ doc.get(LuceneConstants.FILE_PATH)); 
   } 
	
   searcher.close(); 
} 

示例應用程式

讓我們建立一個測試 Lucene 應用程式來測試使用 WildcardQuery 進行搜尋。

步驟 描述
1

建立一個名為 *LuceneFirstApplication* 的專案,放在 *com.tutorialspoint.lucene* 包下,如 *Lucene - 第一個應用程式* 章節所述。您也可以使用 *Lucene - 第一個應用程式* 章節中建立的專案,以便理解搜尋過程。

2

建立 *LuceneConstants.java* 和 *Searcher.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.WildcardQuery; 

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.searchUsingWildCardQuery("record1*"); 
      } catch (IOException e) { 
         e.printStackTrace(); 
      } catch (ParseException e) { 
         e.printStackTrace(); 
      } 
   } 
	
   private void searchUsingWildCardQuery(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 WildcardQuery(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.println("File: "+ doc.get(LuceneConstants.FILE_PATH)); 
      } 
		
      searcher.close(); 
   } 
} 

資料 & 索引目錄建立

我使用了 10 個文字檔案,從 record1.txt 到 record10.txt,簡單地包含學生姓名和其他詳細資訊,並將它們放在 **E:\Lucene\Data** 目錄中。測試資料。應建立索引目錄路徑為 **E:\Lucene\Index**。在 *Lucene - 索引過程* 章節中執行索引程式後,您可以在該資料夾中看到建立的索引檔案列表。

執行程式

完成原始碼建立、原始資料建立、資料目錄建立、索引目錄建立和索引建立後,您就可以進行編譯和執行程式了。為此,請保持 **LuceneTester.Java** 檔案選項卡處於活動狀態,並使用 Eclipse IDE 中提供的“執行”選項,或使用 **Ctrl + F11** 編譯並執行 **LuceneTester** 應用程式。如果應用程式一切正常,這將在 Eclipse IDE 的控制檯中列印以下訊息:

2 documents found. Time :47ms 
File: E:\Lucene\Data\record1.txt 
File: E:\Lucene\Data\record10.txt
lucene_query_programming.htm
廣告

© . All rights reserved.