Skip to content

Commit

Permalink
CommonTermsQuery fix for ignored coordination factor
Browse files Browse the repository at this point in the history
CommonTermsQueryParser does not check for disable_coords, only for
disable_coord. Yet the builder only outputs disable_coords, leading to
disabling the coordination factor to be ignored in the Java API.

Closes #11730
Closes #11780
  • Loading branch information
alexksikes committed Jun 19, 2015
1 parent 4a360af commit 2cde0b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Expand Up @@ -62,7 +62,7 @@ public static enum Operator {

private String highFreqMinimumShouldMatch = null;

private Boolean disableCoords = null;
private Boolean disableCoord = null;

private Float cutoffFrequency = null;

Expand Down Expand Up @@ -148,6 +148,11 @@ public CommonTermsQueryBuilder lowFreqMinimumShouldMatch(String lowFreqMinimumSh
return this;
}

public CommonTermsQueryBuilder disableCoord(boolean disableCoord) {
this.disableCoord = disableCoord;
return this;
}

/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
Expand All @@ -162,8 +167,8 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
builder.startObject(name);

builder.field("query", text);
if (disableCoords != null) {
builder.field("disable_coords", disableCoords);
if (disableCoord != null) {
builder.field("disable_coord", disableCoord);
}
if (highFreqOperator != null) {
builder.field("high_freq_operator", highFreqOperator.toString());
Expand Down
Expand Up @@ -51,7 +51,7 @@ public class CommonTermsQueryParser implements QueryParser {

static final Occur DEFAULT_LOW_FREQ_OCCUR = Occur.SHOULD;

static final boolean DEFAULT_DISABLE_COORDS = true;
static final boolean DEFAULT_DISABLE_COORD = true;


@Inject
Expand All @@ -76,7 +76,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
String queryAnalyzer = null;
String lowFreqMinimumShouldMatch = null;
String highFreqMinimumShouldMatch = null;
boolean disableCoords = DEFAULT_DISABLE_COORDS;
boolean disableCoord = DEFAULT_DISABLE_COORD;
Occur highFreqOccur = DEFAULT_HIGH_FREQ_OCCUR;
Occur lowFreqOccur = DEFAULT_LOW_FREQ_OCCUR;
float maxTermFrequency = DEFAULT_MAX_TERM_DOC_FREQ;
Expand Down Expand Up @@ -116,7 +116,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
queryAnalyzer = analyzer;
} else if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
disableCoords = parser.booleanValue();
disableCoord = parser.booleanValue();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("high_freq_operator".equals(currentFieldName) || "highFreqOperator".equals(currentFieldName)) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}

ExtendedCommonTermsQuery commonsQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, maxTermFrequency, disableCoords, mapper);
ExtendedCommonTermsQuery commonsQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, maxTermFrequency, disableCoord, mapper);
commonsQuery.setBoost(boost);
Query query = parseQueryString(commonsQuery, value.toString(), field, parseContext, analyzer, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch, smartNameFieldMappers);
if (queryName != null) {
Expand Down
Expand Up @@ -2379,6 +2379,19 @@ public void testCommonTermsQuery3() throws IOException {
assertThat(ectQuery.getLowFreqMinimumNumberShouldMatchSpec(), equalTo("2"));
}

@Test // see #11730
public void testCommonTermsQuery4() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(commonTermsQuery("field", "text").disableCoord(false)).query();
assertThat(parsedQuery, instanceOf(ExtendedCommonTermsQuery.class));
ExtendedCommonTermsQuery ectQuery = (ExtendedCommonTermsQuery) parsedQuery;
assertFalse(ectQuery.isCoordDisabled());
parsedQuery = queryParser.parse(commonTermsQuery("field", "text").disableCoord(true)).query();
assertThat(parsedQuery, instanceOf(ExtendedCommonTermsQuery.class));
ectQuery = (ExtendedCommonTermsQuery) parsedQuery;
assertTrue(ectQuery.isCoordDisabled());
}

@Test(expected = QueryParsingException.class)
public void assureMalformedThrowsException() throws IOException {
IndexQueryParserService queryParser;
Expand Down

0 comments on commit 2cde0b1

Please sign in to comment.