鍍金池/ 教程/ 大數(shù)據(jù)/ Lucene PrefixQuery類
Lucene TermRangeQuery類
Lucene排序
Lucene Query類
Lucene搜索操作
Lucene TokenStream
Lucene IndexWriter類
Lucene Term類
Lucene Token
Lucene索引操作
Lucene Field選項
Lucene BooleanQuery類
Lucene StandardAnalyzer類
Lucene字段
Lucene添加文檔操作
Lucene環(huán)境設(shè)置
Lucene Searching類
Lucene StopAnalyzer類
Lucene第一個應(yīng)用程序
Lucene MatchAllDocsQuery類
Lucene IndexSearcher類
Lucene索引類
Lucene更新文檔操作
Lucene教程
Lucene PrefixQuery類
Lucene Analyzer類
Lucene TopDocs類
Lucene TermQuery類
Lucene文檔
Lucene查詢編程
Lucene WildcardQuery類
Lucene WhitespaceAnalyzer
Lucene SimpleAnalyzer類
Lucene目錄
Lucene刪除文檔操作
Lucene索引過程
Lucene FuzzyQuery類
Lucene PhraseQuery類
Lucene分析

Lucene PrefixQuery類

PrefixQuery用于匹配其索引開始以指定的字符串的文檔。

類聲明

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

public class PrefixQuery
   extends MultiTermQuery

類的構(gòu)造函數(shù)

S.N. 構(gòu)造函數(shù)和說明
1 PrefixQuery(Term prefix)
構(gòu)造的開始前綴詞的查詢。

類方法

S.N. 方法及說明
1 boolean equals(Object o) 
返回true如果o為等于當(dāng)前對象。
2 protected FilteredTermEnumgetEnum(IndexReader reader) 
構(gòu)造所使用的枚舉,擴大模式詞條
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.上一篇:Lucene第一個應(yīng)用程序下一篇:Lucene SimpleAnalyzer類