Query DSL: text query to support minimum_should_match, closes #1971.

This commit is contained in:
Shay Banon 2012-05-22 16:46:58 +02:00
parent 5bd93d6f93
commit 86cd95aee2
2 changed files with 20 additions and 0 deletions

View File

@ -69,6 +69,8 @@ public class TextQueryBuilder extends BaseQueryBuilder {
private Integer maxExpansions;
private String minimumShouldMatch;
/**
* Constructs a new text query.
*/
@ -140,6 +142,11 @@ public class TextQueryBuilder extends BaseQueryBuilder {
return this;
}
public TextQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(TextQueryParser.NAME);
@ -170,6 +177,9 @@ public class TextQueryBuilder extends BaseQueryBuilder {
if (maxExpansions != null) {
builder.field("max_expansions", maxExpansions);
}
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
builder.endObject();
builder.endObject();

View File

@ -20,9 +20,11 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
@ -68,6 +70,7 @@ public class TextQueryParser implements QueryParser {
int prefixLength = FuzzyQuery.defaultPrefixLength;
int maxExpansions = FuzzyQuery.defaultMaxExpansions;
BooleanClause.Occur occur = BooleanClause.Occur.SHOULD;
String minimumShouldMatch = null;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
@ -111,6 +114,8 @@ public class TextQueryParser implements QueryParser {
} else {
throw new QueryParsingException(parseContext.index(), "text query requires operator to be either 'and' or 'or', not [" + op + "]");
}
} else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) {
minimumShouldMatch = parser.textOrNull();
} else {
throw new QueryParsingException(parseContext.index(), "[text] query does not support [" + currentFieldName + "]");
}
@ -136,6 +141,11 @@ public class TextQueryParser implements QueryParser {
tQP.setOccur(occur);
Query query = tQP.parse(type);
if (query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
query.setBoost(boost);
return query;
}