Thanks,s.grinovero
But I would expect an analyzer can perform this job. If search keyword is "parking lot in salt lake city" and generated term can have multiple words, I could have queries like:
"parking lot" OR "salt lake city"
"parking lot" AND "salt lake city"
I think less search terms would be more efficient. In my case, 2 queries vs 5 queries.
In addition, I can use salt lake city in term query directly.
String loc = "salt lake city";
locationQuery.add(new TermQuery(new Term("vendorAddress.addrName", loc)), BooleanClause.Occur.SHOULD);
s.grinovero wrote:
ok, now I got it :)
It's much simpler than that, the QueryParser defaults to "OR" operator, so parsing a query like "United States" results in a query like "united OR states".
You want the "AND" as default behaviour, just use the options on the QueryParser instance:
Code:
QueryParser parser = ...
parser.setDefaultOperator( org.apache.lucene.queryParser.QueryParser.Operator.AND );
parser.parse( "united states " );
Just keep in mind that "OR" operator is quite cool when using relevance sorting (the default) as it returns the documents which match most terms on top, so when the query is more complex (say 5 terms) and no document matches all five, you won't get an empty result but you will get those documents which matched most terms on top, descending up to document matching just one term; this is normally useful as users do enter typos or mistakes.