Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MappedFieldType;
Expand Down Expand Up @@ -75,7 +76,13 @@ public class CombinedFieldsQueryBuilder extends AbstractQueryBuilder<CombinedFie
}, FIELDS_FIELD);

PARSER.declareString(CombinedFieldsQueryBuilder::operator, Operator::fromString, OPERATOR_FIELD);
PARSER.declareString(CombinedFieldsQueryBuilder::minimumShouldMatch, MINIMUM_SHOULD_MATCH_FIELD);
PARSER.declareField(
CombinedFieldsQueryBuilder::minimumShouldMatch,
XContentParser::textOrNull,
MINIMUM_SHOULD_MATCH_FIELD,
// using INT_OR_NULL (which includes VALUE_NUMBER, VALUE_STRING, VALUE_NULL) to also allow for numeric values and null
ValueType.INT_OR_NULL
);
PARSER.declareBoolean(CombinedFieldsQueryBuilder::autoGenerateSynonymsPhraseQuery, GENERATE_SYNONYMS_PHRASE_QUERY);
PARSER.declareString(CombinedFieldsQueryBuilder::zeroTermsQuery, value -> {
if ("none".equalsIgnoreCase(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,27 @@ public void testValuesFromXContent() throws IOException {
assertEquals(json, Operator.OR, parsed.operator());
assertEquals(json, 2.0, parsed.boost, 1e-6);
}

/**
* We parse `minimum_should_match` to a String but other queries supporting this parameter also accept integer values and null
*/
public void testMinumumShouldMatchFromXContent() throws IOException {
Object[] testValues = new Object[] { 2, "\"2\"", "\"2%\"", null };
Object[] expectedValues = new Object[] { "2", "2", "2%", null };
int i = 0;
for (Object value : testValues) {
String json = "{\n"
+ " \"combined_fields\" : {\n"
+ " \"query\" : \"quick brown fox\",\n"
+ " \"minimum_should_match\" : " + value + "\n"
+ " }\n"
+ "}";

CombinedFieldsQueryBuilder parsed = (CombinedFieldsQueryBuilder) parseQuery(json);

assertEquals(json, "quick brown fox", parsed.value());
assertEquals(json, expectedValues[i], parsed.minimumShouldMatch());
i++;
}
}
}