views:

436

answers:

1

I've the following lucene index:

Document A
item = level:(1)
item = level:(2)
item = level:(3)

Document B
item = level:(1)
item = level:(4)

Suppose I want to search for all documents which contain level:(1) AND level:(2) ?

The Lucene query could be like:

"item:level\:\(1\) AND level\:\(2\)"

but is it also possible to do something like this:

"item:level\:\(1 OR 2\)"

?

(The reason for this is that I don't want to repeat the string "level\:")

A: 

The simplest solution would be to make level your query parser's default field, allowing your query to be reduced to:

(1 OR 2)

However, I suspect that this isn't exactly what you're looking for...

According to the Lucene query parser syntax documentation, what you're asking for cannot be done using the boolean operators (AND and OR). However, it looks like it may be possible using the plus (+) and minus (-) operators. According to the documentation:

Lucene supports using parentheses to group multiple clauses to a single field.

To search for a title that contains both the word "return" and the phrase "pink panther" use the query:

title:(+return +"pink panther")

This is not exactly what you're looking for, but it may be suitable. I am unsure as to how an OR clause would be written in this manner.

Adam Paynter
@AdamI must admit that my example is somehow confusing. The field is "item" and the content is "level:(1)". (It's maybe confusing that the content also includes an ':' sign)I will try your solution.It will be something like "item:(+level \(1\) \(2\))" ?