多字段查询需要使用MultiFieldQueryParser来做解析:
String params[] = new String[]{par,par};
String fields[] = new String[]{"title","context"};
BooleanClause.Occur rules[] = new BooleanClause.Occur[]{BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD};
Query query = MultiFieldQueryParser.parse(params,fields,rules,analyzer);
其中par是查询关键字,params是根据需要查询的字段数定义的查询参数列表。以上可以翻译为:title like par or context like par。
fields是需要同时查询的字段列表。
analyzer是分词器,不必说。
rules是定义多个参数的匹配规则列表,总共有以下几种情况:
/** Use this operator for clauses that <i>must</i> appear in the matching documents. */
MUST { @Override public String toString() { return "+"; } },
/** Like {@link #MUST} except that these clauses do not participate in scoring. */
FILTER { @Override public String toString() { return "#"; } },
/** Use this operator for clauses that <i>should</i> appear in the
* matching documents. For a BooleanQuery with no <code>MUST</code>
* clauses one or more <code>SHOULD</code> clauses must match a document
* for the BooleanQuery to match.
* @see BooleanQuery.Builder#setMinimumNumberShouldMatch
*/
SHOULD { @Override public String toString() { return ""; } },
/** Use this operator for clauses that <i>must not</i> appear in the matching documents.
* Note that it is not possible to search for queries that only consist
* of a <code>MUST_NOT</code> clause. These clauses do not contribute to the
* score of documents. */
MUST_NOT { @Override public String toString() { return "-"; } };
BooleanClause.Occur.MUST | and |
BooleanClause.Occur.MUST_NOT | not |
BooleanClause.Occur.SHOULD | or |