Lucene PrefixQuery類


PrefixQuery用於匹配其索引開始以指定的字串的文件。

類宣告

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

public class PrefixQuery
   extends MultiTermQuery

類別建構函式

S.N. 建構函式和說明
1 PrefixQuery(Term prefix)
構造的開始字首詞的查詢。

類方法

S.N. 方法及說明
1 boolean equals(Object o) 
返回true如果o為等於當前物件。
2 protected FilteredTermEnumgetEnum(IndexReader reader) 
構造所使用的列舉,擴大模式詞條
3 Term getPrefix() 
返回此查詢的字首。
4 int hashCode() 
返回此物件的雜湊碼值。
5 String toString(String field) 
列印這個查詢的一個使用者可讀的版本。

繼承的方法

這個類繼承了以下類方法:

  • org.apache.lucene.search.MultiTermQuery

  • org.apache.lucene.search.Query

  • java.lang.Object

使用

private void searchUsingPrefixQuery(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 PrefixQuery(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();
}

Example Application

Let us create a test Lucene application to test search using PrefixQuery.

步驟 描述
1 Create a project with a name LuceneFirstApplication under a packagecom.yiibai.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 Applicationchapter. Keep 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

This class is used to provide various constants to be used across the sample application.

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

This class is used to read the indexes made on raw data and searches data using lucene library.

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

This class is used to test the searching capability of lucene library.

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.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.PrefixQuery;
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.searchUsingPrefixQuery("record1");
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ParseException e) {
         e.printStackTrace();
      }
   }

   private void searchUsingPrefixQuery(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 PrefixQuery(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();
   }
}

資料和索引目錄的建立

從record1.txt到record10.txt的檔案中包含簡單的名稱以及學生的其他細節,並把它們放在目錄E:LuceneData。這是測試資料。索引目錄路徑應建立為E:LuceneIndex。期間,執行 Lucene索引程式後- 索引過程中,可以看到該檔案夾中建立的索引檔案的列表。

執行程式:

一旦建立源,創造了原始資料,資料目錄,索引目錄和索引完成後,已經準備好這一步是編譯和執行程式。要做到這一點,請LuceneTester.Java檔案索引標籤中使用Eclipse IDE可使用Run選項,或使用Ctrl+ F11來編譯和執行應用程式LuceneTester。如果您的應用程式一切正常,這將在Eclipse IDE的控制台列印以下訊息:

2 documents found. Time :20ms
File: E:LuceneDataecord1.txt
File: E:LuceneDataecord10.txt