Tutorial :Lucene: How to do wildcard search inside a Term?



Question:

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\:")


Solution:1

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »