tags:

views:

46

answers:

1

I'm using a BooleanQuery to combine several queries. I find that if I add a BooleanQuery to the BooleanQuery, then no result is returned. The added BooleanQuery is a MUST_NOT one, like -city_id:100.

But as lucene's spec says, BooleanQuery could be nested, which I think means it's okay to add such BooleanQuery. Now I have to get all clauses from the BooleanQuery to be added, and then add them to the container BooleanQuery one by one.

I'm a bit confused. Anybody could help? Thank you very much!

+1  A: 

Lucene does not support unary NOT operator. But you can get results for such query by ANDing it with MatchAllDocsQuery.

Shashikant Kore
Ah, it seems work. Thanks. But isn't this confusing? I add BooleanQuery to BooleanQuery, and still I need to check whether the query to be added has AND some query.Also, does ANDing MatchAllDocsQuery in this case undermine the performance?
KailZhang
You can read about the boolean query idiosyncracies on this thread. http://search-lucene.com/m/8x64lENo571/ Essentially, NOT is just suppresses documents. You need another positive set for it to work with.I don't know the details of implementation of MatchAllDocsQuery, but gettig all docs should be fast enough. The addtional ANDing operation should be fast as well as internally it operations on a bitset.
Shashikant Kore
Apologies for the broken sentences in the previous comment.
Shashikant Kore