簡體   English   中英

使用布爾條款的Lucene查詢語法

[英]Lucene Query syntax using Boolean Clauses

我在Lucene有兩個領域

  1. 類型(可以包含X,Y,Z之類的值)
  2. 日期(包含2015-18-10等值)

我想編寫以下查詢:( (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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM