MinimumNumberShouldMatch inconcistency, closes #2194

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
This commit is contained in:
Shay Banon 2012-08-22 14:15:08 +02:00
parent 177568a61f
commit 14c11a94fb
4 changed files with 33 additions and 22 deletions

View File

@ -27,8 +27,6 @@ import java.util.List;
/** /**
* A Query that matches documents matching boolean combinations of other queries. * A Query that matches documents matching boolean combinations of other queries.
*
*
*/ */
public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<BoolQueryBuilder> { public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<BoolQueryBuilder> {
@ -42,7 +40,7 @@ public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQuery
private Boolean disableCoord; private Boolean disableCoord;
private int minimumNumberShouldMatch = -1; private String minimumShouldMatch;
/** /**
* Adds a query that <b>must</b> appear in the matching documents. * Adds a query that <b>must</b> appear in the matching documents.
@ -103,7 +101,15 @@ public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQuery
* @param minimumNumberShouldMatch the number of optional clauses that must match * @param minimumNumberShouldMatch the number of optional clauses that must match
*/ */
public BoolQueryBuilder minimumNumberShouldMatch(int minimumNumberShouldMatch) { 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; return this;
} }
@ -126,8 +132,8 @@ public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQuery
if (disableCoord != null) { if (disableCoord != null) {
builder.field("disable_coord", disableCoord); builder.field("disable_coord", disableCoord);
} }
if (minimumNumberShouldMatch != -1) { if (minimumShouldMatch != null) {
builder.field("minimum_number_should_match", minimumNumberShouldMatch); builder.field("minimum_should_match", minimumShouldMatch);
} }
builder.endObject(); builder.endObject();
} }

View File

@ -23,6 +23,7 @@ import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -56,7 +57,7 @@ public class BoolQueryParser implements QueryParser {
boolean disableCoord = false; boolean disableCoord = false;
float boost = 1.0f; float boost = 1.0f;
int minimumNumberShouldMatch = -1; String minimumShouldMatch = null;
List<BooleanClause> clauses = newArrayList(); List<BooleanClause> clauses = newArrayList();
@ -112,12 +113,12 @@ public class BoolQueryParser implements QueryParser {
} else if (token.isValue()) { } else if (token.isValue()) {
if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) { if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
disableCoord = parser.booleanValue(); 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)) { } else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) {
minimumNumberShouldMatch = parser.intValue(); minimumShouldMatch = parser.textOrNull();
} else if ("boost".equals(currentFieldName)) { } else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue(); boost = parser.floatValue();
} else if ("minimum_number_should_match".equals(currentFieldName) || "minimumNumberShouldMatch".equals(currentFieldName)) {
minimumShouldMatch = parser.textOrNull();
} else { } else {
throw new QueryParsingException(parseContext.index(), "[bool] query does not support [" + currentFieldName + "]"); throw new QueryParsingException(parseContext.index(), "[bool] query does not support [" + currentFieldName + "]");
} }
@ -133,9 +134,7 @@ public class BoolQueryParser implements QueryParser {
query.add(clause); query.add(clause);
} }
query.setBoost(boost); query.setBoost(boost);
if (minimumNumberShouldMatch != -1) { Queries.applyMinimumShouldMatch(query, minimumShouldMatch);
query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
}
return optimizeQuery(fixNegativeQueryIfNeeded(query)); return optimizeQuery(fixNegativeQueryIfNeeded(query));
} }
} }

View File

@ -32,7 +32,7 @@ public class TermsQueryBuilder extends BaseQueryBuilder implements BoostableQuer
private final Object[] values; private final Object[] values;
private int minimumMatch = -1; private String minimumShouldMatch;
private Boolean disableCoord; private Boolean disableCoord;
@ -119,7 +119,12 @@ public class TermsQueryBuilder extends BaseQueryBuilder implements BoostableQuer
* Sets the minimum number of matches across the provided terms. Defaults to <tt>1</tt>. * Sets the minimum number of matches across the provided terms. Defaults to <tt>1</tt>.
*/ */
public TermsQueryBuilder minimumMatch(int minimumMatch) { 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; return this;
} }
@ -149,8 +154,8 @@ public class TermsQueryBuilder extends BaseQueryBuilder implements BoostableQuer
} }
builder.endArray(); builder.endArray();
if (minimumMatch != -1) { if (minimumShouldMatch != null) {
builder.field("minimum_match", minimumMatch); builder.field("minimum_should_match", minimumShouldMatch);
} }
if (disableCoord != null) { if (disableCoord != null) {
builder.field("disable_coord", disableCoord); builder.field("disable_coord", disableCoord);

View File

@ -25,6 +25,7 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperService;
@ -65,7 +66,7 @@ public class TermsQueryParser implements QueryParser {
String fieldName = null; String fieldName = null;
boolean disableCoord = false; boolean disableCoord = false;
float boost = 1.0f; float boost = 1.0f;
int minimumNumberShouldMatch = 1; String minimumShouldMatch = null;
List<String> values = newArrayList(); List<String> values = newArrayList();
String currentFieldName = null; String currentFieldName = null;
@ -86,7 +87,9 @@ public class TermsQueryParser implements QueryParser {
if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) { if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
disableCoord = parser.booleanValue(); disableCoord = parser.booleanValue();
} else if ("minimum_match".equals(currentFieldName) || "minimumMatch".equals(currentFieldName)) { } 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)) { } else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue(); boost = parser.floatValue();
} }
@ -115,9 +118,7 @@ public class TermsQueryParser implements QueryParser {
} }
} }
query.setBoost(boost); query.setBoost(boost);
if (minimumNumberShouldMatch != -1) { Queries.applyMinimumShouldMatch(query, minimumShouldMatch);
query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
}
return wrapSmartNameQuery(optimizeQuery(fixNegativeQueryIfNeeded(query)), smartNameFieldMappers, parseContext); return wrapSmartNameQuery(optimizeQuery(fixNegativeQueryIfNeeded(query)), smartNameFieldMappers, parseContext);
} finally { } finally {
if (smartNameFieldMappers != null && smartNameFieldMappers.explicitTypeInNameWithDocMapper()) { if (smartNameFieldMappers != null && smartNameFieldMappers.explicitTypeInNameWithDocMapper()) {