dejavu
dejavu

Reputation: 3254

Lucene Query syntax using Boolean Clauses

I have two fields in Lucene

  1. type (can contain values like X, Y, Z)
  2. date (contains values like 2015-18-10 etc)

I want to write following query: (type = X and date=today's data) OR (type = anything except X). How can I write this query using SHOULD, MUST, MUST_NOT? looks like there is no clause for these type of query.

Upvotes: 1

Views: 758

Answers (2)

Allen Chou
Allen Chou

Reputation: 1237

If I got your problem, I think the solution is just some combination of BooleanQuery, following is the code written in Scala to address the issue.

According to the documentation(in BooleanClause.java), MUST_NOT should be used with caution.

Use this operator for clauses that 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.

  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

  }

Anyway, hope it helps.

Upvotes: 1

MatsLindh
MatsLindh

Reputation: 52832

You can express the latter part using *:* -type:X, as this creates the set of all documents, and then subtracts the set of documents that has type:X. The *:* query is represented as MatchAllDocsQuery in code.

Upvotes: 1

Related Questions