2

Is it possible to search for a phrase of the kind Searching is fun, in Lucene?

When I try to search with this, Lucene ends up looking for the word fun alone.

Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69

2 Answers2

4

If you are using a QueryParser object to parse your query, you can configure it to automatically assume the '+' operator that Aku spoke of in his answer (sorry Aku for not simply commenting, but comments apparently don't support code formatting). For example:

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

queryParser.setDefaultOperator(QueryParser.Operator.AND);

Query query = queryParser.parse("Searching is fun");
Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
3

Try to put in in quotes: "Searching is fun" or add '+' to required words +Searching +fun

See "Lucene - Query Parser Syntax" for available options

aku
  • 122,288
  • 32
  • 173
  • 203