It's not a secret anymore!
The moose likes Other Open Source Projects and the fly likes Lucene +PhraseQuery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Lucene +PhraseQuery" Watch "Lucene +PhraseQuery" New topic

Lucene +PhraseQuery

Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

could any let me know what is wrong with my code as its returning 0 hits if the phrase exits.

thanks in advance
Karthik Shiraly

Joined: Apr 04, 2009
Posts: 874


Try this (notice that the search terms are in *lowercase*):

Try this too (notice that search terms are as they are):

The Analyzer you choose is critical - read the javadoc of StandardAnalyzer and you'll find it involves a LowerCaseFilter.
You have to decide for your application whether Managing = managing = MaNaGing.

- Choose an analyzer that doesn't make any modifications you're not expecting, OR
- Based on analyzer javadocs, enforce modifications on your search term (i.e, since you've read StandardAnalyzer javadocs and you know in your mind that it applies a lowercase filter, you do a toLower() on your search terms). This is bad approach because the rules of analyzer are being duplicated in your code.
- Best approach: Apply the same Analyzer that you use to index, on your query too, using the QueryParser class. In future, if you change your indexing Analyzer, the queries will auto conform to its rules.

Use Luke tool to analyze your indices. It tells you the terms stored and a lot more. For debugging ease, it's better to work on a file based index during development till your basic indexing and querying are verified ok.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

thanks Karthik for the reply.

but i got woking it after changing
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);


StandardAnalyzer analyzer = new StandardAnalyzer();

and that was trial and error as I ran out logic.

I agree. Here's the link:
subject: Lucene +PhraseQuery
It's not a secret anymore!