Permalink
Browse files

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

  • Loading branch information...
1 parent 5bd93d6 commit 86cd95aee21971b80349e24c66643d82fac2f67b @kimchy kimchy committed May 22, 2012
@@ -69,6 +69,8 @@
private Integer maxExpansions;
+ private String minimumShouldMatch;
+
/**
* Constructs a new text query.
*/
@@ -140,6 +142,11 @@ public TextQueryBuilder maxExpansions(int maxExpansions) {
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 void doXContent(XContentBuilder builder, Params params) throws IOExceptio
if (maxExpansions != null) {
builder.field("max_expansions", maxExpansions);
}
+ if (minimumShouldMatch != null) {
+ builder.field("minimum_should_match", minimumShouldMatch);
+ }
builder.endObject();
builder.endObject();
@@ -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 Query parse(QueryParseContext parseContext) throws IOException, QueryPars
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 Query parse(QueryParseContext parseContext) throws IOException, QueryPars
} 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 Query parse(QueryParseContext parseContext) throws IOException, QueryPars
tQP.setOccur(occur);
Query query = tQP.parse(type);
+
+ if (query instanceof BooleanQuery) {
+ Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
+ }
+
query.setBoost(boost);
return query;
}

0 comments on commit 86cd95a

Please sign in to comment.