Cleanup code and always use strict parsing on query tests

This commit is contained in:
Simon Willnauer 2015-09-10 12:08:50 +02:00
parent 9f722a6674
commit de3c9a23e9
9 changed files with 125 additions and 72 deletions

View File

@ -99,6 +99,13 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
return score; return score;
} }
/**
* Returns the parents type name
*/
public String type() {
return type;
}
/** /**
* Returns inner hit definition in the scope of this query and reusing the defined type and query. * Returns inner hit definition in the scope of this query and reusing the defined type and query.
*/ */

View File

@ -78,7 +78,7 @@ public class NotQueryParser extends BaseQueryParser<NotQueryBuilder> {
} }
if (!queryFound) { if (!queryFound) {
throw new QueryParsingException(parseContext, "filter is required when using `not` query"); throw new QueryParsingException(parseContext, "query is required when using `not` query");
} }
NotQueryBuilder notQueryBuilder = new NotQueryBuilder(query); NotQueryBuilder notQueryBuilder = new NotQueryBuilder(query);

View File

@ -39,10 +39,7 @@ import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.indices.cache.query.terms.TermsLookup; import org.elasticsearch.indices.cache.query.terms.TermsLookup;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/** /**
* A filter for a field based on several terms matching on any of them. * A filter for a field based on several terms matching on any of them.
@ -51,16 +48,29 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
public static final String NAME = "terms"; public static final String NAME = "terms";
static final TermsQueryBuilder PROTOTYPE = new TermsQueryBuilder(null); static final TermsQueryBuilder PROTOTYPE = new TermsQueryBuilder("");
public static final boolean DEFAULT_DISABLE_COORD = false; public static final boolean DEFAULT_DISABLE_COORD = false;
private final String fieldName; private final String fieldName;
private List<Object> values; private final List<Object> values;
@Deprecated
private String minimumShouldMatch; private String minimumShouldMatch;
@Deprecated
private boolean disableCoord = DEFAULT_DISABLE_COORD; private boolean disableCoord = DEFAULT_DISABLE_COORD;
private TermsLookup termsLookup; private TermsLookup termsLookup;
TermsQueryBuilder(String fieldName, List<Object> values, String minimumShouldMatch, boolean disableCoord, TermsLookup termsLookup) {
this.fieldName = fieldName;
if (values == null && termsLookup == null) {
throw new IllegalArgumentException("No value specified for terms query");
}
this.values = values;
this.disableCoord = disableCoord;
this.minimumShouldMatch = minimumShouldMatch;
this.termsLookup = termsLookup;
}
/** /**
* A filter for a field based on several terms matching on any of them. * A filter for a field based on several terms matching on any of them.
* *
@ -128,6 +138,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
*/ */
public TermsQueryBuilder(String fieldName) { public TermsQueryBuilder(String fieldName) {
this.fieldName = fieldName; this.fieldName = fieldName;
this.values = null;
} }
/** /**
@ -176,7 +187,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
return this; return this;
} }
public boolean disableCoord() { boolean disableCoord() {
return this.disableCoord; return this.disableCoord;
} }
@ -308,7 +319,9 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
if (minimumShouldMatch != null) { if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch); builder.field("minimum_should_match", minimumShouldMatch);
} }
builder.field("disable_coord", disableCoord); if (disableCoord != DEFAULT_DISABLE_COORD) {
builder.field("disable_coord", disableCoord);
}
printBoostAndQueryName(builder); printBoostAndQueryName(builder);
builder.endObject(); builder.endObject();
} }
@ -391,14 +404,15 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
protected TermsQueryBuilder doReadFrom(StreamInput in) throws IOException { protected TermsQueryBuilder doReadFrom(StreamInput in) throws IOException {
TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder(in.readString()); String field = in.readString();
TermsLookup lookup = null;
if (in.readBoolean()) { if (in.readBoolean()) {
termsQueryBuilder.termsLookup = TermsLookup.readTermsLookupFrom(in); lookup = TermsLookup.readTermsLookupFrom(in);
} }
termsQueryBuilder.values = ((List<Object>) in.readGenericValue()); List<Object> values = (List<Object>) in.readGenericValue();
termsQueryBuilder.minimumShouldMatch = in.readOptionalString(); String minimumShouldMatch = in.readOptionalString();
termsQueryBuilder.disableCoord = in.readBoolean(); boolean disableCoord = in.readBoolean();
return termsQueryBuilder; return new TermsQueryBuilder(field, values, minimumShouldMatch, disableCoord, lookup);
} }
@Override @Override

View File

@ -104,16 +104,7 @@ public class TermsQueryParser extends BaseQueryParser {
if (fieldName == null) { if (fieldName == null) {
throw new QueryParsingException(parseContext, "terms query requires a field name, followed by array of terms or a document lookup specification"); throw new QueryParsingException(parseContext, "terms query requires a field name, followed by array of terms or a document lookup specification");
} }
TermsQueryBuilder termsQueryBuilder; return new TermsQueryBuilder(fieldName, values, minShouldMatch, disableCoord, termsLookup)
if (values == null) {
termsQueryBuilder = new TermsQueryBuilder(fieldName);
} else {
termsQueryBuilder = new TermsQueryBuilder(fieldName, values);
}
return termsQueryBuilder
.disableCoord(disableCoord)
.minimumShouldMatch(minShouldMatch)
.termsLookup(termsLookup)
.boost(boost) .boost(boost)
.queryName(queryName); .queryName(queryName);
} }

View File

@ -254,7 +254,7 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
QB testQuery = createTestQueryBuilder(); QB testQuery = createTestQueryBuilder();
assertParsedQuery(testQuery.toString(), testQuery); assertParsedQuery(testQuery.toString(), testQuery);
for (Map.Entry<String, QB> alternateVersion : getAlternateVersions().entrySet()) { for (Map.Entry<String, QB> alternateVersion : getAlternateVersions().entrySet()) {
assertParsedQuery(alternateVersion.getKey(), alternateVersion.getValue(), ParseFieldMatcher.EMPTY); assertParsedQuery(alternateVersion.getKey(), alternateVersion.getValue());
} }
} }
@ -270,7 +270,7 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
* Parses the query provided as string argument and compares it with the expected result provided as argument as a {@link QueryBuilder} * Parses the query provided as string argument and compares it with the expected result provided as argument as a {@link QueryBuilder}
*/ */
protected void assertParsedQuery(String queryAsString, QueryBuilder<?> expectedQuery) throws IOException { protected void assertParsedQuery(String queryAsString, QueryBuilder<?> expectedQuery) throws IOException {
assertParsedQuery(queryAsString, expectedQuery, getDefaultParseFieldMatcher()); assertParsedQuery(queryAsString, expectedQuery, ParseFieldMatcher.STRICT);
} }
protected void assertParsedQuery(String queryAsString, QueryBuilder<?> expectedQuery, ParseFieldMatcher matcher) throws IOException { protected void assertParsedQuery(String queryAsString, QueryBuilder<?> expectedQuery, ParseFieldMatcher matcher) throws IOException {
@ -281,7 +281,7 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
} }
protected QueryBuilder<?> parseQuery(String queryAsString) throws IOException { protected QueryBuilder<?> parseQuery(String queryAsString) throws IOException {
return parseQuery(queryAsString, getDefaultParseFieldMatcher()); return parseQuery(queryAsString, ParseFieldMatcher.STRICT);
} }
protected QueryBuilder<?> parseQuery(String queryAsString, ParseFieldMatcher matcher) throws IOException { protected QueryBuilder<?> parseQuery(String queryAsString, ParseFieldMatcher matcher) throws IOException {
@ -292,16 +292,6 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
return context.parseInnerQueryBuilder(); return context.parseInnerQueryBuilder();
} }
/**
* Returns the default {@link ParseFieldMatcher} used for parsing non-alternative XContent representations.
* The default is {@link ParseFieldMatcher#STRICT}.
* Note: Queries returned from {@link #getAlternateVersions()} are always parsed with {@link ParseFieldMatcher#EMPTY} as they might
* not be backwards compatible.
*/
protected ParseFieldMatcher getDefaultParseFieldMatcher() {
return ParseFieldMatcher.STRICT;
}
/** /**
* Test creates the {@link Query} from the {@link QueryBuilder} under test and delegates the * Test creates the {@link Query} from the {@link QueryBuilder} under test and delegates the
* assertions being made on the result to the implementing subclass. * assertions being made on the result to the implementing subclass.
@ -384,6 +374,13 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
@Test @Test
public void testSerialization() throws IOException { public void testSerialization() throws IOException {
QB testQuery = createTestQueryBuilder(); QB testQuery = createTestQueryBuilder();
assertSerialization(testQuery);
}
/**
* Serialize the given query builder and asserts that both are equal
*/
protected QB assertSerialization(QB testQuery) throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) { try (BytesStreamOutput output = new BytesStreamOutput()) {
testQuery.writeTo(output); testQuery.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
@ -392,6 +389,7 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
assertEquals(deserializedQuery, testQuery); assertEquals(deserializedQuery, testQuery);
assertEquals(deserializedQuery.hashCode(), testQuery.hashCode()); assertEquals(deserializedQuery.hashCode(), testQuery.hashCode());
assertNotSame(deserializedQuery, testQuery); assertNotSame(deserializedQuery, testQuery);
return (QB) deserializedQuery;
} }
} }
} }

View File

@ -160,37 +160,35 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
builder.field("type", "foo"); // deprecated builder.field("type", "foo"); // deprecated
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
String queryAsString = builder.string();
QueryShardContext shardContext = createShardContext();
QueryParseContext context = shardContext.parseContext();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(queryAsString);
context.reset(parser);
context.parseFieldMatcher(ParseFieldMatcher.STRICT);
try { try {
context.parseInnerQueryBuilder(); parseQuery(builder.string());
fail("type is deprecated"); fail("type is deprecated");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("Deprecated field [type] used, expected [parent_type] instead", ex.getMessage()); assertEquals("Deprecated field [type] used, expected [parent_type] instead", ex.getMessage());
} }
HasParentQueryBuilder queryBuilder = (HasParentQueryBuilder) parseQuery(builder.string(), ParseFieldMatcher.EMPTY);
assertEquals("foo", queryBuilder.type());
boolean score = randomBoolean();
String key = RandomPicks.randomFrom(random(), Arrays.asList("score_mode", "scoreMode", "score_type", "scoreType")); String key = RandomPicks.randomFrom(random(), Arrays.asList("score_mode", "scoreMode", "score_type", "scoreType"));
builder = XContentFactory.jsonBuilder().prettyPrint(); builder = XContentFactory.jsonBuilder().prettyPrint();
builder.startObject(); builder.startObject();
builder.startObject("has_parent"); builder.startObject("has_parent");
builder.field("query"); builder.field("query");
EmptyQueryBuilder.PROTOTYPE.toXContent(builder, ToXContent.EMPTY_PARAMS); EmptyQueryBuilder.PROTOTYPE.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.field(key, "score"); builder.field(key, score ? "score": "none");
builder.field("parent_type", "foo");
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
queryAsString = builder.string();
parser = XContentFactory.xContent(XContentType.JSON).createParser(queryAsString);
context.reset(parser);
context.parseFieldMatcher(ParseFieldMatcher.STRICT);
try { try {
context.parseInnerQueryBuilder(); parseQuery(builder.string());
fail(key + " is deprecated"); fail(key + " is deprecated");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("Deprecated field [" + key + "] used, replaced by [score]", ex.getMessage()); assertEquals("Deprecated field [" + key + "] used, replaced by [score]", ex.getMessage());
} }
queryBuilder = (HasParentQueryBuilder) parseQuery(builder.string(), ParseFieldMatcher.EMPTY);
assertEquals(score, queryBuilder.score());
} }
} }

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.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseFieldMatcher;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -69,15 +70,6 @@ public class NotQueryBuilderTests extends AbstractQueryTestCase<NotQueryBuilder>
@Override @Override
protected Map<String, NotQueryBuilder> getAlternateVersions() { protected Map<String, NotQueryBuilder> getAlternateVersions() {
Map<String, NotQueryBuilder> alternateVersions = new HashMap<>(); Map<String, NotQueryBuilder> alternateVersions = new HashMap<>();
NotQueryBuilder testQuery1 = new NotQueryBuilder(createTestQueryBuilder().innerQuery());
String contentString1 = "{\n" +
" \"not\" : {\n" +
" \"filter\" : " + testQuery1.innerQuery().toString() + "\n" +
" }\n" +
"}";
alternateVersions.put(contentString1, testQuery1);
QueryBuilder innerQuery = createTestQueryBuilder().innerQuery(); QueryBuilder innerQuery = createTestQueryBuilder().innerQuery();
//not doesn't support empty query when query/filter element is not specified //not doesn't support empty query when query/filter element is not specified
if (innerQuery != EmptyQueryBuilder.PROTOTYPE) { if (innerQuery != EmptyQueryBuilder.PROTOTYPE) {
@ -90,6 +82,24 @@ public class NotQueryBuilderTests extends AbstractQueryTestCase<NotQueryBuilder>
return alternateVersions; return alternateVersions;
} }
public void testDeprecatedXContent() throws IOException {
String deprecatedJson = "{\n" +
" \"not\" : {\n" +
" \"filter\" : " + EmptyQueryBuilder.PROTOTYPE.toString() + "\n" +
" }\n" +
"}";
try {
parseQuery(deprecatedJson);
fail("filter is deprecated");
} catch (IllegalArgumentException ex) {
assertEquals("Deprecated field [filter] used, expected [query] instead", ex.getMessage());
}
NotQueryBuilder queryBuilder = (NotQueryBuilder) parseQuery(deprecatedJson, ParseFieldMatcher.EMPTY);
assertEquals(EmptyQueryBuilder.PROTOTYPE, queryBuilder.innerQuery());
}
@Test @Test
public void testValidate() { public void testValidate() {
QueryBuilder innerQuery = null; QueryBuilder innerQuery = null;

View File

@ -35,7 +35,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
public class QueryStringQueryBuilderTests extends BaseQueryTestCase<QueryStringQueryBuilder> { public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStringQueryBuilder> {
@Override @Override
protected QueryStringQueryBuilder doCreateTestQueryBuilder() { protected QueryStringQueryBuilder doCreateTestQueryBuilder() {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
@ -34,6 +35,7 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -49,11 +51,6 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
queryParserService().setTermsLookupFetchService(termsLookupFetchService); queryParserService().setTermsLookupFetchService(termsLookupFetchService);
} }
@Override
protected ParseFieldMatcher getDefaultParseFieldMatcher() {
return ParseFieldMatcher.EMPTY;
}
@Override @Override
protected TermsQueryBuilder doCreateTestQueryBuilder() { protected TermsQueryBuilder doCreateTestQueryBuilder() {
TermsQueryBuilder query; TermsQueryBuilder query;
@ -71,12 +68,6 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
query = new TermsQueryBuilder(randomBoolean() ? randomAsciiOfLengthBetween(1,10) : STRING_FIELD_NAME); query = new TermsQueryBuilder(randomBoolean() ? randomAsciiOfLengthBetween(1,10) : STRING_FIELD_NAME);
query.termsLookup(randomTermsLookup()); query.termsLookup(randomTermsLookup());
} }
if (randomBoolean()) {
query.minimumShouldMatch(randomInt(100) + "%");
}
if (randomBoolean()) {
query.disableCoord(randomBoolean());
}
return query; return query;
} }
@ -207,6 +198,50 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
assertThat(termsQueryBuilder.validate().validationErrors().size(), is(1)); assertThat(termsQueryBuilder.validate().validationErrors().size(), is(1));
} }
public void testDeprecatedXContent() throws IOException {
String query = "{\n" +
" \"terms\": {\n" +
" \"field\": [\n" +
" \"blue\",\n" +
" \"pill\"\n" +
" ],\n" +
" \"disable_coord\": true\n" +
" }\n" +
"}";
try {
parseQuery(query);
fail("disable_coord is deprecated");
} catch (IllegalArgumentException ex) {
assertEquals("Deprecated field [disable_coord] used, replaced by [Use [bool] query instead]", ex.getMessage());
}
TermsQueryBuilder queryBuilder = (TermsQueryBuilder) parseQuery(query, ParseFieldMatcher.EMPTY);
TermsQueryBuilder copy = assertSerialization(queryBuilder);
assertTrue(queryBuilder.disableCoord());
assertTrue(copy.disableCoord());
String randomMinShouldMatch = RandomPicks.randomFrom(random(), Arrays.asList("min_match", "min_should_match", "minimum_should_match"));
query = "{\n" +
" \"terms\": {\n" +
" \"field\": [\n" +
" \"blue\",\n" +
" \"pill\"\n" +
" ],\n" +
" \"" + randomMinShouldMatch +"\": \"42%\"\n" +
" }\n" +
"}";
try {
parseQuery(query);
fail(randomMinShouldMatch + " is deprecated");
} catch (IllegalArgumentException ex) {
assertEquals("Deprecated field [" + randomMinShouldMatch + "] used, replaced by [Use [bool] query instead]", ex.getMessage());
}
queryBuilder = (TermsQueryBuilder) parseQuery(query, ParseFieldMatcher.EMPTY);
copy = assertSerialization(queryBuilder);
assertEquals("42%", queryBuilder.minimumShouldMatch());
assertEquals("42%", copy.minimumShouldMatch());
}
private static class MockTermsLookupFetchService extends TermsLookupFetchService { private static class MockTermsLookupFetchService extends TermsLookupFetchService {
private List<Object> randomTerms = new ArrayList<>(); private List<Object> randomTerms = new ArrayList<>();