2

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

CubanX
  • 5,176
  • 2
  • 29
  • 44
Montezuma
  • 797
  • 6
  • 9

1 Answers1

4

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.

knutwalker
  • 5,924
  • 2
  • 22
  • 29
  • Ah okay Thank you Very much :) and where can i set Ignore Case ? – Montezuma Mar 08 '16 at 14:35
  • 1
    Lucene doesn't really work that way, that you can set ignore case somewhere. It all depends on your analyzer. Most analyzers include a lowercase token filter, which will lowercase everything. You have to make sure, that your query runs through a compatible analyzer (ideally the same one) so that matching tokens are produced. A TermQuery doesn't use any analyzer so you have to perform that step beforehand, e.g. by using the query parser, which does run stuff through the analyzer (without building the BooleanQuery first, actually). – knutwalker Mar 08 '16 at 14:41