Reputation: 1
I'm trying to use MultiFieldQueryParser to execute the following search:
contents:hello world priority:high
i.e., I only want to see documents returned which contain the words 'hello' and 'world' and which have a priority of 'high'. The default behaviour for MultiFieldQueryParser appears to return anything which either contains contents:hello world OR priority:high - I can't figure out how to change this.
Any advice?
Upvotes: 0
Views: 3831
Reputation: 135
You can set default operator for the parser via setDefaultOperator
method.
An Example:
...
Analyzer userTermsAnalyzer = searchFactory.getAnalyzer(MyEntity.class);
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_CURRENT, new String[]{"text"}, userTermsAnalyzer);
queryParser.setDefaultOperator(QueryParser.Operator.AND);
Query taskQuery = queryParser.parse("contents:hello world priority:high");
Upvotes: 0
Reputation: 5052
MultiFieldQuery is used when you want to search a term across multiple fields. What you are looking for is a simple Boolean query with two clauses. A query as follows should work.
+(+contents:hello +contents:world) +priority:high
Here you have one boolean query with to Occur.MUST clauses one which is, in turn, a boolean query two clauses and another is a term query.
Upvotes: 1