Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Lucene match all words in query?

Tags:

search

lucene

I am using Lucene to allow a user to search for words in a large number of documents. Lucene seems to default to returning all documents containing any of the words entered.

Is it possible to change this behaviour? I know that '+' can be use to force a term to be included but I would like to make that the default action.

Ideally I would like functionality similar to Google's: '-' to exclude words and "abc xyz" to group words.

Just to clarify I also thought of inserting '+' into all spaces in the query. I just wanted to avoid detecting grouped terms (brackets, quotes etc) and potentially breaking the query. Is there another approach?

like image 506
paul Avatar asked Jan 16 '09 14:01

paul


2 Answers

This looks similar to the Lucene Sentence Search question. If you're interested, this is how I answered that question:

String defaultField = ...;
Analyzer analyzer = ...;
QueryParser queryParser = new QueryParser(defaultField, analyzer);

queryParser.setDefaultOperator(QueryParser.Operator.AND);

Query query = queryParser.parse("Searching is fun");
like image 65
Adam Paynter Avatar answered Oct 03 '22 11:10

Adam Paynter


Like Adam said, there's no need to do anything to the query string. QueryParser's setDefaultOperator does exactly what you're asking for.

like image 39
itsadok Avatar answered Oct 03 '22 12:10

itsadok