1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.