Skip to content

Commit 66e8490

Browse files
author
Christoph Büscher
committed
Fix minimum_should_match for combined_fields query (#72152)
The `minimum_should_match` parameter for queries like `match` or `query_string" accepts either string values like "2" or "25%", numeric values and in most older cases also `null` as a no-op parameter. We also use examples for these different values in our documentation, so we should also support these value types for the new `combined_fields` query. This changes the ConstructingObjectParser used in CombinedFieldsQueryBuilder so it also accepts integer value types and adds a parsing test for the different cases. Closes #72148
1 parent 64c06c8 commit 66e8490

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

server/src/main/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.common.io.stream.StreamOutput;
3030
import org.elasticsearch.common.lucene.search.Queries;
3131
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
32+
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
3233
import org.elasticsearch.common.xcontent.XContentBuilder;
3334
import org.elasticsearch.common.xcontent.XContentParser;
3435
import org.elasticsearch.index.mapper.MappedFieldType;
@@ -75,7 +76,13 @@ public class CombinedFieldsQueryBuilder extends AbstractQueryBuilder<CombinedFie
7576
}, FIELDS_FIELD);
7677

7778
PARSER.declareString(CombinedFieldsQueryBuilder::operator, Operator::fromString, OPERATOR_FIELD);
78-
PARSER.declareString(CombinedFieldsQueryBuilder::minimumShouldMatch, MINIMUM_SHOULD_MATCH_FIELD);
79+
PARSER.declareField(
80+
CombinedFieldsQueryBuilder::minimumShouldMatch,
81+
XContentParser::textOrNull,
82+
MINIMUM_SHOULD_MATCH_FIELD,
83+
// using INT_OR_NULL (which includes VALUE_NUMBER, VALUE_STRING, VALUE_NULL) to also allow for numeric values and null
84+
ValueType.INT_OR_NULL
85+
);
7986
PARSER.declareBoolean(CombinedFieldsQueryBuilder::autoGenerateSynonymsPhraseQuery, GENERATE_SYNONYMS_PHRASE_QUERY);
8087
PARSER.declareString(CombinedFieldsQueryBuilder::zeroTermsQuery, value -> {
8188
if ("none".equalsIgnoreCase(value)) {

server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,27 @@ public void testValuesFromXContent() throws IOException {
8888
assertEquals(json, Operator.OR, parsed.operator());
8989
assertEquals(json, 2.0, parsed.boost, 1e-6);
9090
}
91+
92+
/**
93+
* We parse `minimum_should_match` to a String but other queries supporting this parameter also accept integer values and null
94+
*/
95+
public void testMinumumShouldMatchFromXContent() throws IOException {
96+
Object[] testValues = new Object[] { 2, "\"2\"", "\"2%\"", null };
97+
Object[] expectedValues = new Object[] { "2", "2", "2%", null };
98+
int i = 0;
99+
for (Object value : testValues) {
100+
String json = "{\n"
101+
+ " \"combined_fields\" : {\n"
102+
+ " \"query\" : \"quick brown fox\",\n"
103+
+ " \"minimum_should_match\" : " + value + "\n"
104+
+ " }\n"
105+
+ "}";
106+
107+
CombinedFieldsQueryBuilder parsed = (CombinedFieldsQueryBuilder) parseQuery(json);
108+
109+
assertEquals(json, "quick brown fox", parsed.value());
110+
assertEquals(json, expectedValues[i], parsed.minimumShouldMatch());
111+
i++;
112+
}
113+
}
91114
}

0 commit comments

Comments
 (0)