SHARE
TWEET

How to make Lucene match all words in query

a guest Feb 27th, 2012 21 Never
  1. String defaultField = ...;
  2. Analyzer analyzer = ...;
  3. QueryParser queryParser = new QueryParser(defaultField, analyzer);
  4.  
  5. queryParser.setDefaultOperator(QueryParser.Operator.AND);
  6.  
  7. Query query = queryParser.parse("Searching is fun");
  8.        
  9. if (required && !prohibited)
  10.   clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST));
  11. else if (!required && !prohibited)
  12.   clauses.addElement(new BooleanClause(q, BooleanClause.Occur.SHOULD));
  13. else if (!required && prohibited)
  14.   clauses.addElement(new BooleanClause(q, BooleanClause.Occur.MUST_NOT));
  15. else
  16.   throw new RuntimeException("Clause cannot be both required and prohibited");
RAW Paste Data
Top