[英]Lucene Query syntax using Boolean Clauses
我在Lucene有兩個領域
我想編寫以下查詢:( (type = X and date=today's data) OR (type = anything except X).
如何使用SHOULD,MUST和MUST_NOT編寫此查詢? 看起來這些查詢類型沒有子句。
您可以使用*:* -type:X
表示后一部分,因為這將創建所有文檔的集合,然后減去具有type:X
的文檔的集合。 *:*
查詢在代碼中表示為MatchAllDocsQuery 。
如果遇到您的問題,我認為解決方案只是BooleanQuery的某種組合,以下是用Scala編寫的用於解決該問題的代碼。
根據文檔(在BooleanClause.java中),必須謹慎使用MUST_NOT。
appear in the matching documents.Note that it is not possible to search for queries that only consist of a
MUST_NOT
clause. 使用此運算符處理出現在匹配文檔中的子句。請注意,不可能搜索僅包含MUST_NOT
子句的查詢。
object LuceneTest extends App {
val query = new BooleanQuery
val subQuery1 = new BooleanQuery
subQuery1.add(new TermQuery(new Term("type", "xx")), BooleanClause.Occur.MUST)
subQuery1.add(new TermQuery(new Term("date", "yy")), BooleanClause.Occur.MUST)
val subQuery2 = new BooleanQuery
// As mentioned above, so I put MatchAllDocsQuery here to avoid only containing MUST_NOT
subQuery2.add(new MatchAllDocsQuery, BooleanClause.Occur.MUST)
subQuery2.add(new TermQuery(new Term("type", "xx")),BooleanClause.Occur.MUST_NOT)
// subQuery1 and subQuery2 construct two subQueries respectively
// then use OR(i.e SHOULD in Lucene) to combine them
query.add(subQuery1, BooleanClause.Occur.SHOULD)
query.add(subQuery2, BooleanClause.Occur.SHOULD)
query
}
無論如何,希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.