Treat zero token in `common` terms query as MatchNoDocsQuery

Currently we return `null` when the query in a common terms query has
zero tokens after analysis. It would be better if query builders
`toQuery()` would never return null and return a meaningful lucene
query instead. Since an ExtendedCommonTermsQuery with no terms gets
rewritten to a MatchNoDocsQuery later, it is enough to leave out the
check for zero tokens.
This commit is contained in:
Christoph Büscher 2016-05-31 14:22:26 +02:00
parent 8f0109c2a5
commit 740bdc8d99
2 changed files with 8 additions and 14 deletions

View File

@ -401,7 +401,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
return parseQueryString(commonsQuery, text, field, analyzerObj, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch);
}
static Query parseQueryString(ExtendedCommonTermsQuery query, Object queryString, String field, Analyzer analyzer,
private static Query parseQueryString(ExtendedCommonTermsQuery query, Object queryString, String field, Analyzer analyzer,
String lowFreqMinimumShouldMatch, String highFreqMinimumShouldMatch) throws IOException {
// Logic similar to QueryParser#getFieldQuery
int count = 0;
@ -417,9 +417,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
}
}
if (count == 0) {
return null;
}
query.setLowFreqMinimumNumberShouldMatch(lowFreqMinimumShouldMatch);
query.setHighFreqMinimumNumberShouldMatch(highFreqMinimumShouldMatch);
return query;

View File

@ -37,12 +37,16 @@ public class CommonTermsQueryBuilderTests extends AbstractQueryTestCase<CommonTe
protected CommonTermsQueryBuilder doCreateTestQueryBuilder() {
CommonTermsQueryBuilder query;
int numberOfTerms = randomIntBetween(0, 10);
StringBuilder text = new StringBuilder("");
for (int i = 0; i < numberOfTerms; i++) {
text.append(randomAsciiOfLengthBetween(1, 10)).append(" ");
}
// mapped or unmapped field
String text = randomAsciiOfLengthBetween(1, 10);
if (randomBoolean()) {
query = new CommonTermsQueryBuilder(STRING_FIELD_NAME, text);
query = new CommonTermsQueryBuilder(STRING_FIELD_NAME, text.toString());
} else {
query = new CommonTermsQueryBuilder(randomAsciiOfLengthBetween(1, 10), text);
query = new CommonTermsQueryBuilder(randomAsciiOfLengthBetween(1, 10), text.toString());
}
if (randomBoolean()) {
@ -134,13 +138,6 @@ public class CommonTermsQueryBuilderTests extends AbstractQueryTestCase<CommonTe
assertEquals(query, "nelly the elephant not as a cartoon", queryBuilder.value());
}
public void testNoTermsFromQueryString() throws IOException {
CommonTermsQueryBuilder builder = new CommonTermsQueryBuilder(STRING_FIELD_NAME, "");
QueryShardContext context = createShardContext();
context.setAllowUnmappedFields(true);
assertNull(builder.toQuery(context));
}
public void testCommonTermsQuery1() throws IOException {
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/commonTerms-query1.json");
Query parsedQuery = parseQuery(query).toQuery(createShardContext());