- 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