[TEST] unify parsing code in tests
Also clean up static method in MoreLikeThisFetchService, fix randomly failing TermsQueryBuilderTest#testNullValues and rename mappedFieldNamesSmall to MAPPED_LEAF_FIELD_NAMES.
This commit is contained in:
parent
0872a6ea39
commit
94dd6c5f0e
|
@ -94,7 +94,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
|
|||
|
||||
//norelease to be made abstract once all query builders override doToQuery providing their own specific implementation.
|
||||
protected Query doToQuery(QueryShardContext context) throws IOException {
|
||||
return context.indexQueryParserService().queryParser(getName()).parse(context);
|
||||
return context.indexQueryParserService().indicesQueriesRegistry().queryParsers().get(getName()).parse(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -137,13 +137,7 @@ public class IndexQueryParserService extends AbstractIndexComponent {
|
|||
return this.queryStringLenient;
|
||||
}
|
||||
|
||||
//norelease we might want to get rid of this as it was temporarily introduced for our default doToQuery impl
|
||||
//seems to be used only in tests
|
||||
public QueryParser<?> queryParser(String name) {
|
||||
return indicesQueriesRegistry.queryParsers().get(name);
|
||||
}
|
||||
|
||||
public IndicesQueriesRegistry indicesQueriesRegistry() {
|
||||
IndicesQueriesRegistry indicesQueriesRegistry() {
|
||||
return indicesQueriesRegistry;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
|
|||
|
||||
private final int end;
|
||||
|
||||
static final SpanFirstQueryBuilder SPAN_FIRST_QUERY_BUILDER = new SpanFirstQueryBuilder(null, -1);
|
||||
static final SpanFirstQueryBuilder PROTOTYPE = new SpanFirstQueryBuilder(null, -1);
|
||||
|
||||
/**
|
||||
* Query that matches spans queries defined in <code>matchBuilder</code>
|
||||
|
|
|
@ -89,6 +89,6 @@ public class SpanFirstQueryParser extends BaseQueryParser<SpanFirstQueryBuilder>
|
|||
|
||||
@Override
|
||||
public SpanFirstQueryBuilder getBuilderPrototype() {
|
||||
return SpanFirstQueryBuilder.SPAN_FIRST_QUERY_BUILDER;
|
||||
return SpanFirstQueryBuilder.PROTOTYPE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,8 +22,6 @@ package org.elasticsearch.index.query;
|
|||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -85,12 +83,7 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
|
|||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testMissingFiltersSection() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryString = "{ \"and\" : {}";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, AndQueryBuilder.NAME);
|
||||
context.queryParser(AndQueryBuilder.NAME).fromXContent(context);
|
||||
parseQuery("{ \"and\" : {}", AndQueryBuilder.NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -125,11 +118,7 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
|
|||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testParsingExceptionNonFiltersElementArray() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryString = "{ \"and\" : { \"whatever_filters\" : [ { \"match_all\" : {} } ] } }";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, AndQueryBuilder.NAME);
|
||||
context.queryParser(AndQueryBuilder.NAME).fromXContent(context);
|
||||
parseQuery(queryString, AndQueryBuilder.NAME);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.index.query;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
|
||||
|
@ -55,14 +54,12 @@ import org.elasticsearch.index.cache.IndexCacheModule;
|
|||
import org.elasticsearch.index.mapper.MapperService;
|
||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionParser;
|
||||
import org.elasticsearch.index.query.support.QueryParsers;
|
||||
import org.elasticsearch.index.search.termslookup.TermsLookupFetchService;
|
||||
import org.elasticsearch.index.settings.IndexSettingsModule;
|
||||
import org.elasticsearch.index.similarity.SimilarityModule;
|
||||
import org.elasticsearch.indices.IndicesModule;
|
||||
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
|
||||
import org.elasticsearch.indices.breaker.CircuitBreakerService;
|
||||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
|
||||
import org.elasticsearch.indices.cache.query.terms.TermsLookup;
|
||||
import org.elasticsearch.script.ScriptModule;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
@ -76,9 +73,7 @@ import org.joda.time.DateTimeZone;
|
|||
import org.junit.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
@ -91,13 +86,18 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
protected static final String BOOLEAN_FIELD_NAME = "mapped_boolean";
|
||||
protected static final String DATE_FIELD_NAME = "mapped_date";
|
||||
protected static final String OBJECT_FIELD_NAME = "mapped_object";
|
||||
protected static final String[] mappedFieldNames = new String[] { STRING_FIELD_NAME, INT_FIELD_NAME,
|
||||
protected static final String[] MAPPED_FIELD_NAMES = new String[] { STRING_FIELD_NAME, INT_FIELD_NAME,
|
||||
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, OBJECT_FIELD_NAME };
|
||||
protected static final String[] mappedFieldNamesSmall = new String[] { STRING_FIELD_NAME, INT_FIELD_NAME,
|
||||
protected static final String[] MAPPED_LEAF_FIELD_NAMES = new String[] { STRING_FIELD_NAME, INT_FIELD_NAME,
|
||||
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME };
|
||||
|
||||
private static Injector injector;
|
||||
private static IndexQueryParserService queryParserService;
|
||||
|
||||
protected static IndexQueryParserService queryParserService() {
|
||||
return queryParserService;
|
||||
}
|
||||
|
||||
private static Index index;
|
||||
|
||||
protected static Index getIndex() {
|
||||
|
@ -176,7 +176,6 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
currentTypes[i] = type;
|
||||
}
|
||||
namedWriteableRegistry = injector.getInstance(NamedWriteableRegistry.class);
|
||||
queryParserService.setTermsLookupFetchService(new MockTermsLookupFetchService());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
@ -253,18 +252,18 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
* 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 {
|
||||
QueryBuilder newQuery = parseQuery(queryAsString, expectedQuery);
|
||||
QueryBuilder<?> newQuery = parseQuery(queryAsString, expectedQuery.getName());
|
||||
assertNotSame(newQuery, expectedQuery);
|
||||
assertEquals(expectedQuery, newQuery);
|
||||
assertEquals(expectedQuery.hashCode(), newQuery.hashCode());
|
||||
}
|
||||
|
||||
protected QueryBuilder parseQuery(String queryAsString, QueryBuilder<?> expectedQuery) throws IOException {
|
||||
protected QueryBuilder<?> parseQuery(String queryAsString, String queryId) throws IOException {
|
||||
XContentParser parser = XContentFactory.xContent(queryAsString).createParser(queryAsString);
|
||||
QueryParseContext context = createParseContext();
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, expectedQuery.getName());
|
||||
return queryParser(expectedQuery).fromXContent(context);
|
||||
assertQueryHeader(parser, queryId);
|
||||
return context.queryParser(queryId).fromXContent(context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -339,7 +338,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
try (BytesStreamOutput output = new BytesStreamOutput()) {
|
||||
testQuery.writeTo(output);
|
||||
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
|
||||
QueryBuilder<?> prototype = queryParser(testQuery).getBuilderPrototype();
|
||||
QueryBuilder<?> prototype = queryParser(testQuery.getName()).getBuilderPrototype();
|
||||
QueryBuilder deserializedQuery = prototype.readFrom(in);
|
||||
assertEquals(deserializedQuery, testQuery);
|
||||
assertEquals(deserializedQuery.hashCode(), testQuery.hashCode());
|
||||
|
@ -380,8 +379,8 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
assertThat("different queries should have different hashcode", secondQuery.hashCode(), not(equalTo(firstQuery.hashCode())));
|
||||
}
|
||||
|
||||
protected QueryParser<?> queryParser(QueryBuilder query) {
|
||||
return queryParserService.queryParser(query.getName());
|
||||
private QueryParser<?> queryParser(String queryId) {
|
||||
return queryParserService.indicesQueriesRegistry().queryParsers().get(queryId);
|
||||
}
|
||||
|
||||
//we use the streaming infra to create a copy of the query provided as argument
|
||||
|
@ -389,7 +388,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
try (BytesStreamOutput output = new BytesStreamOutput()) {
|
||||
query.writeTo(output);
|
||||
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
|
||||
QueryBuilder<?> prototype = queryParser(query).getBuilderPrototype();
|
||||
QueryBuilder<?> prototype = queryParser(query.getName()).getBuilderPrototype();
|
||||
@SuppressWarnings("unchecked")
|
||||
QB secondQuery = (QB)prototype.readFrom(in);
|
||||
return secondQuery;
|
||||
|
@ -413,7 +412,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
return createShardContext().parseContext();
|
||||
}
|
||||
|
||||
protected static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException {
|
||||
private static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException {
|
||||
assertThat(parser.nextToken(), is(XContentParser.Token.START_OBJECT));
|
||||
assertThat(parser.nextToken(), is(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.currentName(), is(expectedParserName));
|
||||
|
@ -467,7 +466,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
if (currentTypes == null || currentTypes.length == 0 || randomBoolean()) {
|
||||
return randomAsciiOfLengthBetween(1, 10);
|
||||
}
|
||||
return randomFrom(mappedFieldNamesSmall);
|
||||
return randomFrom(MAPPED_LEAF_FIELD_NAMES);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -539,29 +538,4 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
|
|||
protected static boolean isNumericFieldName(String fieldName) {
|
||||
return INT_FIELD_NAME.equals(fieldName) || DOUBLE_FIELD_NAME.equals(fieldName);
|
||||
}
|
||||
|
||||
protected static class MockTermsLookupFetchService extends TermsLookupFetchService {
|
||||
|
||||
private static List<Object> randomTerms = new ArrayList<>();
|
||||
|
||||
public MockTermsLookupFetchService() {
|
||||
super(null, Settings.Builder.EMPTY_SETTINGS);
|
||||
String[] strings = generateRandomStringArray(10, 10, false, true);
|
||||
for (String string : strings) {
|
||||
randomTerms.add(string);
|
||||
if (rarely()) {
|
||||
randomTerms.add(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> fetch(TermsLookup termsLookup) {
|
||||
return randomTerms;
|
||||
}
|
||||
|
||||
public static List<Object> getRandomTerms() {
|
||||
return randomTerms;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,15 +21,11 @@ package org.elasticsearch.index.query;
|
|||
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantScoreQueryBuilder> {
|
||||
|
||||
|
@ -58,13 +54,8 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
|
|||
*/
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testFilterElement() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryId = ConstantScoreQueryBuilder.NAME;
|
||||
String queryString = "{ \""+queryId+"\" : {}";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, queryId);
|
||||
context.queryParser(queryId).fromXContent(context);
|
||||
String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : {}";
|
||||
parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -21,8 +21,6 @@ package org.elasticsearch.index.query;
|
|||
|
||||
import org.apache.lucene.search.DisjunctionMaxQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -31,9 +29,7 @@ import java.util.HashMap;
|
|||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder> {
|
||||
|
||||
|
@ -104,14 +100,8 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
|
|||
*/
|
||||
@Test
|
||||
public void testInnerQueryReturnsNull() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryId = ConstantScoreQueryBuilder.NAME;
|
||||
String queryString = "{ \""+queryId+"\" : { \"filter\" : { } }";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, queryId);
|
||||
ConstantScoreQueryBuilder innerQueryBuilder = (ConstantScoreQueryBuilder) context.queryParser(queryId).fromXContent(context);
|
||||
|
||||
String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : { \"filter\" : { } }";
|
||||
QueryBuilder<?> innerQueryBuilder = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
|
||||
DisMaxQueryBuilder disMaxBuilder = new DisMaxQueryBuilder().add(innerQueryBuilder);
|
||||
assertNull(disMaxBuilder.toQuery(createShardContext()));
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ public class ExistsQueryBuilderTest extends BaseQueryTestCase<ExistsQueryBuilder
|
|||
protected ExistsQueryBuilder doCreateTestQueryBuilder() {
|
||||
String fieldPattern;
|
||||
if (randomBoolean()) {
|
||||
fieldPattern = randomFrom(mappedFieldNames);
|
||||
fieldPattern = randomFrom(MAPPED_FIELD_NAMES);
|
||||
} else {
|
||||
fieldPattern = randomAsciiOfLengthBetween(1, 10);
|
||||
}
|
||||
|
|
|
@ -21,15 +21,11 @@ package org.elasticsearch.index.query;
|
|||
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuilder> {
|
||||
|
@ -69,15 +65,9 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
|
|||
*/
|
||||
@Test
|
||||
public void testInnerQueryReturnsNull() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
|
||||
// create inner filter
|
||||
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, ConstantScoreQueryBuilder.NAME);
|
||||
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.NAME).fromXContent(context);
|
||||
|
||||
QueryBuilder innerQuery = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
|
||||
// check that when wrapping this filter, toQuery() returns null
|
||||
FQueryFilterBuilder queryFilterQuery = new FQueryFilterBuilder(innerQuery);
|
||||
assertNull(queryFilterQuery.toQuery(createShardContext()));
|
||||
|
|
|
@ -35,7 +35,7 @@ public class FieldMaskingSpanQueryBuilderTest extends BaseQueryTestCase<FieldMas
|
|||
protected FieldMaskingSpanQueryBuilder doCreateTestQueryBuilder() {
|
||||
String fieldName;
|
||||
if (randomBoolean()) {
|
||||
fieldName = randomFrom(mappedFieldNames);
|
||||
fieldName = randomFrom(MAPPED_FIELD_NAMES);
|
||||
} else {
|
||||
fieldName = randomAsciiOfLengthBetween(1, 10);
|
||||
}
|
||||
|
|
|
@ -24,8 +24,6 @@ import org.apache.lucene.queries.TermsQuery;
|
|||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -44,11 +42,7 @@ public class IdsQueryBuilderTest extends BaseQueryTestCase<IdsQueryBuilder> {
|
|||
@Test(expected=QueryParsingException.class)
|
||||
public void testIdsNotProvided() throws IOException {
|
||||
String noIdsFieldQuery = "{\"ids\" : { \"type\" : \"my_type\" }";
|
||||
XContentParser parser = XContentFactory.xContent(noIdsFieldQuery).createParser(noIdsFieldQuery);
|
||||
QueryParseContext context = createParseContext();
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, "ids");
|
||||
context.queryParser("ids").fromXContent(context);
|
||||
parseQuery(noIdsFieldQuery, IdsQueryBuilder.NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -30,7 +30,7 @@ public class MissingQueryBuilderTest extends BaseQueryTestCase<MissingQueryBuild
|
|||
|
||||
@Override
|
||||
protected MissingQueryBuilder doCreateTestQueryBuilder() {
|
||||
MissingQueryBuilder query = new MissingQueryBuilder(randomBoolean() ? randomFrom(mappedFieldNames) : randomAsciiOfLengthBetween(1, 10));
|
||||
MissingQueryBuilder query = new MissingQueryBuilder(randomBoolean() ? randomFrom(MAPPED_FIELD_NAMES) : randomAsciiOfLengthBetween(1, 10));
|
||||
if (randomBoolean()) {
|
||||
query.nullValue(randomBoolean());
|
||||
}
|
||||
|
|
|
@ -23,17 +23,13 @@ import org.apache.lucene.search.BooleanClause;
|
|||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
|
||||
|
||||
|
@ -66,12 +62,8 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
|
|||
*/
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testMissingFilterSection() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryString = "{ \"not\" : {}";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, NotQueryBuilder.NAME);
|
||||
context.queryParser(NotQueryBuilder.NAME).fromXContent(context);
|
||||
parseQuery(queryString, NotQueryBuilder.NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,16 +22,12 @@ package org.elasticsearch.index.query;
|
|||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
|
||||
|
@ -98,12 +94,8 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
|
|||
|
||||
@Test(expected=QueryParsingException.class)
|
||||
public void testMissingFiltersSection() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
String queryString = "{ \"or\" : {}";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, OrQueryBuilder.NAME);
|
||||
context.queryParser(OrQueryBuilder.NAME).fromXContent(context);
|
||||
parseQuery(queryString, OrQueryBuilder.NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -21,15 +21,11 @@ package org.elasticsearch.index.query;
|
|||
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder> {
|
||||
|
@ -62,15 +58,9 @@ public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder
|
|||
*/
|
||||
@Test
|
||||
public void testInnerQueryReturnsNull() throws IOException {
|
||||
QueryParseContext context = createParseContext();
|
||||
|
||||
// create inner filter
|
||||
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
|
||||
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, ConstantScoreQueryBuilder.NAME);
|
||||
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.NAME).fromXContent(context);
|
||||
|
||||
QueryBuilder<?> innerQuery = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
|
||||
// check that when wrapping this filter, toQuery() returns null
|
||||
QueryFilterBuilder queryFilterQuery = new QueryFilterBuilder(innerQuery);
|
||||
assertNull(queryFilterQuery.toQuery(createShardContext()));
|
||||
|
|
|
@ -1644,7 +1644,7 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
|
|||
|
||||
@Test
|
||||
public void testMoreLikeThisIds() throws Exception {
|
||||
MoreLikeThisQueryParser parser = (MoreLikeThisQueryParser) queryParser.queryParser("more_like_this");
|
||||
MoreLikeThisQueryParser parser = (MoreLikeThisQueryParser) queryParser.indicesQueriesRegistry().queryParsers().get("more_like_this");
|
||||
parser.setFetchService(new MockMoreLikeThisFetchService());
|
||||
|
||||
IndexQueryParserService queryParser = queryParser();
|
||||
|
@ -1670,7 +1670,7 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
|
|||
@Test
|
||||
public void testMLTMinimumShouldMatch() throws Exception {
|
||||
// setup for mocking fetching items
|
||||
MoreLikeThisQueryParser parser = (MoreLikeThisQueryParser) queryParser.queryParser("more_like_this");
|
||||
MoreLikeThisQueryParser parser = (MoreLikeThisQueryParser) queryParser.indicesQueriesRegistry().queryParsers().get("more_like_this");
|
||||
parser.setFetchService(new MockMoreLikeThisFetchService());
|
||||
|
||||
// parsing the ES query
|
||||
|
|
|
@ -23,13 +23,12 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.spanTermQuery;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
|
||||
public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryBuilder> {
|
||||
|
||||
|
@ -72,41 +71,38 @@ public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryB
|
|||
*/
|
||||
@Test
|
||||
public void testParseEnd() throws IOException {
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanFirstQueryBuilder.NAME);
|
||||
builder.field("match");
|
||||
spanTermQuery("description", "jumped").toXContent(builder, null);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
QueryParseContext context = createParseContext();
|
||||
XContentParser parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanFirstQueryBuilder.NAME);
|
||||
try {
|
||||
new SpanFirstQueryParser().fromXContent(context);
|
||||
fail("missing [end] parameter should raise exception");
|
||||
} catch (QueryParsingException e) {
|
||||
assertTrue(e.getMessage().contains("spanFirst must have [end] set"));
|
||||
{
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanFirstQueryBuilder.NAME);
|
||||
builder.field("match");
|
||||
spanTermQuery("description", "jumped").toXContent(builder, null);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
try {
|
||||
parseQuery(builder.string(), SpanFirstQueryBuilder.NAME);
|
||||
fail("missing [end] parameter should raise exception");
|
||||
} catch (QueryParsingException e) {
|
||||
assertTrue(e.getMessage().contains("spanFirst must have [end] set"));
|
||||
}
|
||||
}
|
||||
|
||||
builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanFirstQueryBuilder.NAME);
|
||||
builder.field("end", 10);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
{
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanFirstQueryBuilder.NAME);
|
||||
builder.field("end", 10);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
context = createParseContext();
|
||||
parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanFirstQueryBuilder.NAME);
|
||||
try {
|
||||
new SpanFirstQueryParser().fromXContent(context);
|
||||
fail("missing [match] parameter should raise exception");
|
||||
} catch (QueryParsingException e) {
|
||||
assertTrue(e.getMessage().contains("spanFirst must have [match] span query clause"));
|
||||
try {
|
||||
parseQuery(builder.string(), SpanFirstQueryBuilder.NAME);
|
||||
fail("missing [match] parameter should raise exception");
|
||||
} catch (QueryParsingException e) {
|
||||
assertTrue(e.getMessage().contains("spanFirst must have [match] span query clause"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,16 +23,13 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.spanNearQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.spanTermQuery;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuilder> {
|
||||
|
||||
|
@ -133,12 +130,7 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
|
|||
builder.field("dist", 3);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
QueryParseContext context = createParseContext();
|
||||
XContentParser parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanNotQueryBuilder.NAME);
|
||||
SpanNotQueryBuilder query = (SpanNotQueryBuilder) new SpanNotQueryParser().fromXContent(context);
|
||||
SpanNotQueryBuilder query = (SpanNotQueryBuilder)parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
|
||||
assertThat(query.pre(), equalTo(3));
|
||||
assertThat(query.post(), equalTo(3));
|
||||
assertNotNull(query.includeQuery());
|
||||
|
@ -150,7 +142,8 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
|
|||
*/
|
||||
@Test
|
||||
public void testParserExceptions() throws IOException {
|
||||
try {
|
||||
|
||||
{
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanNotQueryBuilder.NAME);
|
||||
|
@ -160,44 +153,40 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
|
|||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
QueryParseContext context = createParseContext();
|
||||
XContentParser parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanNotQueryBuilder.NAME);
|
||||
new SpanNotQueryParser().fromXContent(context);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [include]"));
|
||||
try {
|
||||
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [include]"));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanNotQueryBuilder.NAME);
|
||||
builder.field("include");
|
||||
spanNearQuery(1).clause(QueryBuilders.spanTermQuery("description", "quick"))
|
||||
.clause(QueryBuilders.spanTermQuery("description", "fox")).toXContent(builder, null);
|
||||
.clause(QueryBuilders.spanTermQuery("description", "fox")).toXContent(builder, null);
|
||||
builder.field("dist", 2);
|
||||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
QueryParseContext context = createParseContext();
|
||||
XContentParser parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanNotQueryBuilder.NAME);
|
||||
new SpanNotQueryParser().fromXContent(context);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [exclude]"));
|
||||
try {
|
||||
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [exclude]"));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
builder.startObject();
|
||||
builder.startObject(SpanNotQueryBuilder.NAME);
|
||||
builder.field("include");
|
||||
spanNearQuery(1).clause(QueryBuilders.spanTermQuery("description", "quick"))
|
||||
.clause(QueryBuilders.spanTermQuery("description", "fox")).toXContent(builder, null);
|
||||
.clause(QueryBuilders.spanTermQuery("description", "fox")).toXContent(builder, null);
|
||||
builder.field("exclude");
|
||||
spanTermQuery("description", "jumped").toXContent(builder, null);
|
||||
builder.field("dist", 2);
|
||||
|
@ -205,14 +194,12 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
|
|||
builder.endObject();
|
||||
builder.endObject();
|
||||
|
||||
QueryParseContext context = createParseContext();
|
||||
XContentParser parser = XContentFactory.xContent(builder.string()).createParser(builder.string());
|
||||
context.reset(parser);
|
||||
assertQueryHeader(parser, SpanNotQueryBuilder.NAME);
|
||||
new SpanNotQueryParser().fromXContent(context);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot can either use [dist] or [pre] & [post] (or none)"));
|
||||
try {
|
||||
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
|
||||
fail("QueryParsingException should have been caught");
|
||||
} catch (QueryParsingException e) {
|
||||
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot can either use [dist] or [pre] & [post] (or none)"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,11 +24,15 @@ import org.apache.lucene.search.BooleanClause;
|
|||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.search.termslookup.TermsLookupFetchService;
|
||||
import org.elasticsearch.indices.cache.query.terms.TermsLookup;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -36,6 +40,14 @@ import static org.hamcrest.Matchers.*;
|
|||
|
||||
public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder> {
|
||||
|
||||
private static MockTermsLookupFetchService termsLookupFetchService;
|
||||
|
||||
@BeforeClass
|
||||
public static void mockTermsLookupFetchService() throws IOException {
|
||||
termsLookupFetchService = new MockTermsLookupFetchService();
|
||||
queryParserService().setTermsLookupFetchService(termsLookupFetchService);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TermsQueryBuilder doCreateTestQueryBuilder() {
|
||||
TermsQueryBuilder query;
|
||||
|
@ -77,14 +89,15 @@ public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder>
|
|||
BooleanQuery booleanQuery = (BooleanQuery) query;
|
||||
|
||||
// we only do the check below for string fields (otherwise we'd have to decode the values)
|
||||
if (!queryBuilder.fieldName().equals(STRING_FIELD_NAME) && queryBuilder.termsLookup() == null) {
|
||||
if (queryBuilder.fieldName().equals(INT_FIELD_NAME) || queryBuilder.fieldName().equals(DOUBLE_FIELD_NAME)
|
||||
|| queryBuilder.fieldName().equals(BOOLEAN_FIELD_NAME) || queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// expected returned terms depending on whether we have a terms query or a terms lookup query
|
||||
List<Object> terms;
|
||||
if (queryBuilder.termsLookup() != null) {
|
||||
terms = MockTermsLookupFetchService.getRandomTerms();
|
||||
terms = termsLookupFetchService.getRandomTerms();
|
||||
} else {
|
||||
terms = queryBuilder.values();
|
||||
}
|
||||
|
@ -141,7 +154,7 @@ public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder>
|
|||
try {
|
||||
switch (randomInt(6)) {
|
||||
case 0:
|
||||
new TermsQueryBuilder("field", (String) null);
|
||||
new TermsQueryBuilder("field", (String[]) null);
|
||||
break;
|
||||
case 1:
|
||||
new TermsQueryBuilder("field", (int[]) null);
|
||||
|
@ -156,10 +169,10 @@ public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder>
|
|||
new TermsQueryBuilder("field", (double[]) null);
|
||||
break;
|
||||
case 5:
|
||||
new TermsQueryBuilder("field", (Object) null);
|
||||
new TermsQueryBuilder("field", (Object[]) null);
|
||||
break;
|
||||
default:
|
||||
new TermsQueryBuilder("field", (Iterable) null);
|
||||
new TermsQueryBuilder("field", (Iterable<?>) null);
|
||||
break;
|
||||
}
|
||||
fail("should have failed with IllegalArgumentException");
|
||||
|
@ -184,7 +197,32 @@ public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder>
|
|||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
QueryBuilder termsQueryBuilder = parseQuery(query, TermsQueryBuilder.PROTOTYPE);
|
||||
QueryBuilder termsQueryBuilder = parseQuery(query, TermsQueryBuilder.NAME);
|
||||
assertThat(termsQueryBuilder.validate().validationErrors().size(), is(1));
|
||||
}
|
||||
|
||||
private static class MockTermsLookupFetchService extends TermsLookupFetchService {
|
||||
|
||||
private List<Object> randomTerms = new ArrayList<>();
|
||||
|
||||
MockTermsLookupFetchService() {
|
||||
super(null, Settings.Builder.EMPTY_SETTINGS);
|
||||
String[] strings = generateRandomStringArray(10, 10, false, true);
|
||||
for (String string : strings) {
|
||||
randomTerms.add(string);
|
||||
if (rarely()) {
|
||||
randomTerms.add(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> fetch(TermsLookup termsLookup) {
|
||||
return randomTerms;
|
||||
}
|
||||
|
||||
List<Object> getRandomTerms() {
|
||||
return randomTerms;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue