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.xxkt.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.xxkt.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.xxkt.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.xxkt.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:LuceneData
ecord1.txt
File: E:LuceneData
ecord10.txt