如何在 Lucene Search 中将 booleanQuery 与 StandardAnalyzer 一起使用?
问问题
2937 次
3 回答
6
我想您指的是使用 QueryParser 对象解析布尔查询,对吗?Lucene 查询语法文档应该有你需要的一切。
于 2009-06-04T09:08:58.353 回答
2
根据该文档,布尔查询应该由 BooleanQuery.QueryBuilder 创建一次,然后被认为是不可变的。请参阅BooleanQuery.Builder.add(org.apache.lucene.search.BooleanClause)
代码示例可以通过http://programtalk.com/java-api-usage-examples/org.apache.lucene.util.QueryBuilder/找到, 给出的一个示例是:
public Query parse(Type type, String fieldName, Object value) throws IOException {
final String field;
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType != null) {
field = fieldType.name();
} else {
field = fieldName;
}
/*
* If the user forced an analyzer we really don't care if they are
* searching a type that wants term queries to be used with query string
* because the QueryBuilder will take care of it. If they haven't forced
* an analyzer then types like NumberFieldType that want terms with
* query string will blow up because their analyzer isn't capable of
* passing through QueryBuilder.
*/
boolean noForcedAnalyzer = this.analyzer == null;
if (fieldType != null && fieldType.tokenized() == false && noForcedAnalyzer) {
return blendTermQuery(new Term(fieldName, value.toString()), fieldType);
}
Analyzer analyzer = getAnalyzer(fieldType);
assert analyzer != null;
MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType);
builder.setEnablePositionIncrements(this.enablePositionIncrements);
Query query = null;
switch(type) {
case BOOLEAN:
if (commonTermsCutoff == null) {
query = builder.createBooleanQuery(field, value.toString(), occur);
} else {
query = builder.createCommonTermsQuery(field, value.toString(), occur, occur, commonTermsCutoff, fieldType);
}
break;
case PHRASE:
query = builder.createPhraseQuery(field, value.toString(), phraseSlop);
break;
case PHRASE_PREFIX:
query = builder.createPhrasePrefixQuery(field, value.toString(), phraseSlop, maxExpansions);
break;
default:
throw new IllegalStateException("No type found for [" + type + "]");
}
if (query == null) {
return zeroTermsQuery();
} else {
return query;
}
}
于 2016-12-05T11:36:07.430 回答
0
BooleanQuery. BooleanQuery
是布尔子句的容器,它们是可选的、必需的或禁止的子查询。您通常可以添加一个子句来BooleanQuery
使用如下所示的 API 方法:
public void add(查询查询,需要布尔值,禁止布尔值)
于 2012-06-27T16:28:40.417 回答