目录

PhraseQuery

短语查询用于搜索包含特定术语序列的文档。

Class 声明 (Class Declaration)

以下是org.apache.lucene.search.PhraseQuery类的声明:

public class PhraseQuery
   extends Query

类构造函数 (Class Constructors)

下表显示了一个类构造函数 -

S.No. 构造函数和描述
1

PhraseQuery()

构造一个空短语查询。

Class Methods

下表显示了不同的类方法 -

S.No. 方法和描述
1

void add(Term term)

在查询短语的末尾添加一个术语。

2

void add(Term term, int position)

在查询短语的末尾添加一个术语。

3

Weight createWeight(Searcher searcher)

专家:为此查询构造适当的权重实现。

4

boolean equals(Object o)

如果object o等于this,则返回true。

5

void extractTerms(Set《Term》 queryTerms)

专家:将此查询中出现的所有术语添加到术语集中。

6

int[] getPositions()

返回此短语中术语的相对位置。

7

int getSlop()

返回slop。

8

Term[] getTerms()

返回此短语中的术语集。

9

int hashCode()

返回此对象的哈希码值。

10

Query rewrite(IndexReader reader)

专家:被调用以将查询重写为原始查询。

11

void setSlop(int s)

设置查询短语中单词之间允许的其他单词数。

12

String toString(String f)

打印此查询的用户可读版本。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

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

用法 (Usage)

private void searchUsingPhraseQuery(String[] phrases)
   throws IOException, ParseException {
   searcher = new Searcher(indexDir);
   long startTime = System.currentTimeMillis();
   PhraseQuery query = new PhraseQuery();
   query.setSlop(0);
   for(String word:phrases) {
      query.add(new Term(LuceneConstants.FILE_NAME,word));
   }
   //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应用程序来使用PhraseQuery测试搜索。

描述
1

Lucene - First Application章节中解释,在com.iowiki.lucene包下创建一个名为LuceneFirstApplication的项目。 您还可以使用Lucene - First Application创建的项目Lucene - First Application本章的Lucene - First Application章节来理解搜索过程。

2

按照Lucene - First Application章节中的说明创建LuceneConstants.javaSearcher.java 。 保持其余文件不变。

3

创建LuceneTester.java ,如下所述。

4

清理并构建应用程序以确保业务逻辑按照要求运行。

LuceneConstants.java

此类用于提供跨示例应用程序使用的各种常量。

package com.iowiki.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.iowiki.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.iowiki.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.PhraseQuery;
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();
         String[] phrases = new String[]{"record1.txt"};
         tester.searchUsingPhraseQuery(phrases);
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ParseException e) {
         e.printStackTrace();
      }
   }
   private void searchUsingPhraseQuery(String[] phrases)
      throws IOException, ParseException {
      searcher = new Searcher(indexDir);
      long startTime = System.currentTimeMillis();
      PhraseQuery query = new PhraseQuery();
      query.setSlop(0);
      for(String word:phrases) {
         query.add(new Term(LuceneConstants.FILE_NAME,word));
      }
      //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的10个文本文件到包含学生姓名和其他详细信息的record10.txt,并将它们放在目录E:\Lucene\Data. 测试数据 。 索引目录路径应创建为E:\Lucene\Index 。 在Lucene - Indexing Process一章中运行索引程序后,您可以看到在该文件夹中创建的索引文件列表。

运行程序 (Running the Program)

完成源,原始数据,数据目录,索引目录和索引的创建后,可以继续编译和运行程序。 为此,请保持LuceneTester.Java文件选项卡处于活动状态,并使用Eclipse IDE中提供的Run选项或使用Ctrl + F11编译并运行LuceneTester应用程序。 如果您的应用程序成功运行,它将在Eclipse IDE的控制台中打印以下消息 -

1 documents found. Time :14ms
File: E:\Lucene\Data\record1.txt
↑回到顶部↑
WIKI教程 @2018