Search code examples
javalucenebooleanquery

lucene BooleanQuery.Builder Build doesn't Work


Hello Guys i have a Question :)

I create a BooleanQuery Like this :

BooleanQuery.Builder qry = new BooleanQuery.Builder();
qry.add(new TermQuery(new Term("Name", "Anna")), BooleanClause.Occur.SHOULD);

And if i do a search like this now :

TopDocs docs = searcher.search(qry.build(), hitsPerPage);

it gets Zero Results ? But if I use this code :

TopDocs docs = searcher.search(parser.parse(qry.build().toString()),    hitsPerPage);

Then I get the right results ? Can you explain me why I have to parse it again ?

I am using Version 5.5.0 and Name is a TextField


Solution

  • A TextField runs your data through an analyzer and will likely produce the term "anna" (lowercase). A TermQuery does not run anything through an analyzer, so it searches for "Anna" (uppercase) and this does not match. Create the TermQuery with the lowercased term and you should see results: new TermQuery(new Term("Name", "anna")). The BooleanQuery has nothing to do with this, in fact, this particular query would rewrite itself to the underlying TermQuery, as this is the only subquery. The parser takes the string "Name:Anna" (produced by the TermQuery), runs it through the analyzer and gives you a "Name:anna" TermQuery, that's why it works if you run the query through the parser – it involves the necessary analyzing step.