- Lucene 教程
- Lucene - 首页
- Lucene - 概述
- Lucene - 环境设置
- Lucene - 第一个应用程序
- Lucene - 索引类
- Lucene - 搜索类
- Lucene - 索引过程
- Lucene - 索引操作
- Lucene - 搜索操作
- Lucene - 查询编程
- Lucene - 分析
- Lucene - 排序
- Lucene 有用资源
- Lucene - 快速指南
- Lucene - 有用资源
- Lucene - 讨论
Lucene - 布尔查询
BooleanQuery 用于搜索多个查询的结果文档,这些查询使用AND、OR 或 NOT 运算符。
类声明
以下是org.apache.lucene.search.BooleanQuery 类的声明:
public class BooleanQuery
extends Query
implements Iterable<BooleanClause>
字段
以下是 BooleanQuery 的字段:
- protected int minNrShouldMatch
类构造函数
下表显示了不同的类构造函数:
| 序号 | 构造函数 & 描述 |
|---|---|
| 1 | BooleanQuery() 构造一个空的布尔查询。 |
| 2 | BooleanQuery(boolean disableCoord) 构造一个空的布尔查询。 |
类方法
下表显示了不同的类方法:
| 序号 | 方法 & 描述 |
|---|---|
| 1 | void add(BooleanClause clause) 向布尔查询中添加一个子句。 |
| 2 | void add(Query query, BooleanClause.Occur occur) 向布尔查询中添加一个子句。 |
| 3 | List<BooleanClause> clauses() 返回此查询中子句的列表。 |
| 4 | Object clone() 返回此查询的克隆。 |
| 5 | Weight createWeight(Searcher searcher) 专家:为该查询构造一个合适的 Weight 实现。 |
| 6 | boolean equals(Object o) 如果对象 o 等于此对象,则返回 true。 |
| 7 | void extractTerms(Set 专家:将此查询中出现的所有术语添加到术语集中。 |
| 8 | BooleanClause[] getClauses() 返回此查询中子句的集合。 |
| 9 | static int getMaxClauseCount() 返回允许的最大子句数,默认为 1024。 |
| 10 | int getMinimumNumberShouldMatch() 获取必须满足的可选 BooleanClauses 的最小数量。 |
| 11 | int hashCode() 返回此对象的哈希码值。 |
| 12 | boolean isCoordDisabled() 如果在此查询实例的评分中禁用Similarity.coord(int,int),则返回 true。 |
| 13 | Iterator<BooleanClause> iterator() 返回此查询中子句的迭代器。 |
| 14 | Query rewrite(IndexReader reader) 专家:调用以将查询重写为基本查询。 |
| 15 | static void setMaxClauseCount(int maxClauseCount) 设置每个 BooleanQuery 允许的最大子句数。 |
| 16 | void setMinimumNumberShouldMatch(int min) 指定必须满足的可选 BooleanClauses 的最小数量。 |
| 17 | String toString(String field) 打印此查询的用户可读版本。 |
继承的方法
此类继承自以下类的方法:
- org.apache.lucene.search.Query
- java.lang.Object
用法
private void searchUsingBooleanQuery(String searchQuery1,
String searchQuery2)throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term1 = new Term(LuceneConstants.FILE_NAME, searchQuery1);
//create the term query object
Query query1 = new TermQuery(term1);
Term term2 = new Term(LuceneConstants.FILE_NAME, searchQuery2);
//create the term query object
Query query2 = new PrefixQuery(term2);
BooleanQuery query = new BooleanQuery();
query.add(query1,BooleanClause.Occur.MUST_NOT);
query.add(query2,BooleanClause.Occur.MUST);
//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 应用程序来测试使用 BooleanQuery 进行搜索。
| 步骤 | 描述 |
|---|---|
| 1 | 在Lucene - 第一个应用程序章节中说明的包com.tutorialspoint.lucene下创建一个名为LuceneFirstApplication的项目。您也可以使用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.BooleanClause;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanQuery;
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.searchUsingBooleanQuery("record1.txt","record1");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void searchUsingBooleanQuery(String searchQuery1,
String searchQuery2)throws IOException, ParseException {
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
//create a term to search file name
Term term1 = new Term(LuceneConstants.FILE_NAME, searchQuery1);
//create the term query object
Query query1 = new TermQuery(term1);
Term term2 = new Term(LuceneConstants.FILE_NAME, searchQuery2);
//create the term query object
Query query2 = new PrefixQuery(term2);
BooleanQuery query = new BooleanQuery();
query.add(query1,BooleanClause.Occur.MUST_NOT);
query.add(query2,BooleanClause.Occur.MUST);
//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 的 10 个文本文件,其中包含学生姓名和其他详细信息,并将它们放在E:\Lucene\Data目录中。 测试数据。应创建索引目录路径为E:\Lucene\Index。在Lucene - 索引过程章节中运行索引程序后,您可以在该文件夹中看到创建的索引文件列表。
运行程序
完成源代码、原始数据、数据目录、索引目录和索引的创建后,您可以通过编译和运行程序继续。为此,请保持 LuceneTester.Java 文件选项卡处于活动状态,并使用 Eclipse IDE 中提供的运行选项或使用Ctrl + F11编译并运行您的LuceneTester应用程序。如果您的应用程序一切正常,这将在 Eclipse IDE 的控制台中打印以下消息:
1 documents found. Time :26ms File: E:\Lucene\Data\record10.txt