Skip to content

Commit

Permalink
MinimumNumberShouldMatch inconcistency, closes #2194
Browse files Browse the repository at this point in the history
Streamline the use of minimum should match to all relevant queries to accept `minimum_should_match`, and allow all relevant queries to accept the advanced "string" based config
  • Loading branch information
kimchy committed Aug 22, 2012
1 parent f8c2aae commit 6163539
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java
Expand Up @@ -27,8 +27,6 @@

/**
* A Query that matches documents matching boolean combinations of other queries.
*
*
*/
public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<BoolQueryBuilder> {

Expand All @@ -42,7 +40,7 @@ public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQuery

private Boolean disableCoord;

private int minimumNumberShouldMatch = -1;
private String minimumShouldMatch;

/**
* Adds a query that <b>must</b> appear in the matching documents.
Expand Down Expand Up @@ -103,7 +101,15 @@ public BoolQueryBuilder disableCoord(boolean disableCoord) {
* @param minimumNumberShouldMatch the number of optional clauses that must match
*/
public BoolQueryBuilder minimumNumberShouldMatch(int minimumNumberShouldMatch) {
this.minimumNumberShouldMatch = minimumNumberShouldMatch;
this.minimumShouldMatch = Integer.toString(minimumNumberShouldMatch);
return this;
}

/**
* Sets the minimum should match using the special syntax (for example, supporting percentage).
*/
public BoolQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
return this;
}

Expand All @@ -126,8 +132,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (disableCoord != null) {
builder.field("disable_coord", disableCoord);
}
if (minimumNumberShouldMatch != -1) {
builder.field("minimum_number_should_match", minimumNumberShouldMatch);
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
builder.endObject();
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/elasticsearch/index/query/BoolQueryParser.java
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;

Expand Down Expand Up @@ -56,7 +57,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

boolean disableCoord = false;
float boost = 1.0f;
int minimumNumberShouldMatch = -1;
String minimumShouldMatch = null;

List<BooleanClause> clauses = newArrayList();

Expand Down Expand Up @@ -94,12 +95,12 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
} else if (token.isValue()) {
if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
disableCoord = parser.booleanValue();
} else if ("minimum_number_should_match".equals(currentFieldName) || "minimumNumberShouldMatch".equals(currentFieldName)) {
minimumNumberShouldMatch = parser.intValue();
} else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) {
minimumNumberShouldMatch = parser.intValue();
minimumShouldMatch = parser.textOrNull();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("minimum_number_should_match".equals(currentFieldName) || "minimumNumberShouldMatch".equals(currentFieldName)) {
minimumShouldMatch = parser.textOrNull();
} else {
throw new QueryParsingException(parseContext.index(), "[bool] query does not support [" + currentFieldName + "]");
}
Expand All @@ -111,9 +112,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
query.add(clause);
}
query.setBoost(boost);
if (minimumNumberShouldMatch != -1) {
query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
}
Queries.applyMinimumShouldMatch(query, minimumShouldMatch);
return optimizeQuery(fixNegativeQueryIfNeeded(query));
}
}
Expand Up @@ -32,7 +32,7 @@ public class TermsQueryBuilder extends BaseQueryBuilder implements BoostableQuer

private final Object[] values;

private int minimumMatch = -1;
private String minimumShouldMatch;

private Boolean disableCoord;

Expand Down Expand Up @@ -119,7 +119,12 @@ public TermsQueryBuilder(String name, Object... values) {
* Sets the minimum number of matches across the provided terms. Defaults to <tt>1</tt>.
*/
public TermsQueryBuilder minimumMatch(int minimumMatch) {
this.minimumMatch = minimumMatch;
this.minimumShouldMatch = Integer.toString(minimumMatch);
return this;
}

public TermsQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
return this;
}

Expand Down Expand Up @@ -149,8 +154,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
}
builder.endArray();

if (minimumMatch != -1) {
builder.field("minimum_match", minimumMatch);
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
if (disableCoord != null) {
builder.field("disable_coord", disableCoord);
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
String fieldName = null;
boolean disableCoord = false;
float boost = 1.0f;
int minimumNumberShouldMatch = 1;
String minimumShouldMatch = null;
List<String> values = newArrayList();

String currentFieldName = null;
Expand All @@ -86,7 +87,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
disableCoord = parser.booleanValue();
} else if ("minimum_match".equals(currentFieldName) || "minimumMatch".equals(currentFieldName)) {
minimumNumberShouldMatch = parser.intValue();
minimumShouldMatch = parser.textOrNull();
} else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) {
minimumShouldMatch = parser.textOrNull();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
}
Expand Down Expand Up @@ -115,9 +118,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}
query.setBoost(boost);
if (minimumNumberShouldMatch != -1) {
query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
}
Queries.applyMinimumShouldMatch(query, minimumShouldMatch);
return wrapSmartNameQuery(optimizeQuery(fixNegativeQueryIfNeeded(query)), smartNameFieldMappers, parseContext);
} finally {
if (smartNameFieldMappers != null && smartNameFieldMappers.explicitTypeInNameWithDocMapper()) {
Expand Down

0 comments on commit 6163539

Please sign in to comment.