Milind Ganjoo
Milind Ganjoo

Reputation: 1280

Lucene 3.5.0 QueryParser doesn't find any results while programmatically generated query does

This has been baffling me recently, and I can't seem to find a suitable explanation anywhere.

If I run a query built using the Query API, it works perfectly well.

TermQuery sourceQuery = new TermQuery(new Term("source", "CNN"));

Running results = searcher.search(sourceQuery, 30) and checking results.totalHits shows me a value of 159.

However, in the same program, I set up a QueryParser (my default field is called text):

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
QueryParser parser = new QueryParser(Version.LUCENE_35,
      "text", analyzer);

and input the command-line query

source:CNN

I get no results. Running this command on Luke does give me a result. Does anyone have an idea what's happening?

Upvotes: 1

Views: 1210

Answers (1)

Kai Chan
Kai Chan

Reputation: 2473

You probably have used the wrong Analyzer for the QueryParser object. Note that:

  1. When you build your own Term object, the term's text is in upper case.
  2. You give StandardAnalyzer to QueryParser, so the term's text is converted to lower case (by StandardAnalyzer).
  3. Luke's default analyzer is KeywordAnalyzer, which preserves the case (i.e. upper case) you specify the term in.

I guess your terms are in upper case (or turned into upper case) when you index the text. That explains why approach 1 and 3 work, but 2 does not because the cases do not match. In general, it's a good idea to use the same Analyzer when indexing and when searching, or at least pay attention to the case.

Upvotes: 5

Related Questions