[TEST] remove createExpectedQuery from BaseQueryTestCase

We currently test the toQuery method by comparing its return value with the output of createExpectedQuery. This seemed at first like a good deep test for the lucene generated queries, but ends up causing a lot of code duplication between tests and prod code, which smells like this test is not that useful after all.

This commit removes the requirement for createExpectedQuery, we test a bit less the lucene generated queries, but we do still have a testToQuery method in BaseQueryTestCase. This test acts as smoke test to verify that there are no issues with our toQuery method implementation for each query: it verifies that two equal queries should result in the same lucene query. It also verifies that changing the boost value of a query affects the result of the toQuery.

Part of the previous createExpectedQuery can be moved to assertLuceneQuery using normal assertions, we do it when it makes sense only though.

This commit also adds support for boost and queryName to SpanMultiTermQueryParser and SpanMultiTermQueryBuilder, plus it removes no-op setters for queryName and boost from QueryFilterBuilder. Instead we simply declare in our test infra that query filter doesn't support boost and queryName and we disable tests aroudn these fields in that specific case. Boost and queryName support is also moved down to the QueryBuilder interface.

Closes #12473
This commit is contained in:
javanna 2015-07-27 11:25:08 +02:00 committed by Luca Cavanna
parent b780346cb7
commit f6ca11db30
43 changed files with 664 additions and 697 deletions

View File

@ -95,7 +95,8 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
* Returns the query name for the query.
*/
@SuppressWarnings("unchecked")
public QB queryName(String queryName) {
@Override
public final QB queryName(String queryName) {
this.queryName = queryName;
return (QB) this;
}
@ -103,6 +104,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
/**
* Sets the query name for the query.
*/
@Override
public final String queryName() {
return queryName;
}
@ -110,6 +112,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
/**
* Returns the boost for this query.
*/
@Override
public final float boost() {
return this.boost;
}
@ -119,7 +122,8 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> exte
* weightings) have their score multiplied by the boost provided.
*/
@SuppressWarnings("unchecked")
public QB boost(float boost) {
@Override
public final QB boost(float boost) {
this.boost = boost;
return (QB) this;
}

View File

@ -81,4 +81,26 @@ public class EmptyQueryBuilder extends ToXContentToBytes implements QueryBuilder
public EmptyQueryBuilder readFrom(StreamInput in) throws IOException {
return EmptyQueryBuilder.PROTOTYPE;
}
@Override
public EmptyQueryBuilder queryName(String queryName) {
//no-op
return this;
}
@Override
public String queryName() {
return null;
}
@Override
public float boost() {
return -1;
}
@Override
public EmptyQueryBuilder boost(float boost) {
//no-op
return this;
}
}

View File

@ -120,16 +120,6 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder<FuzzyQueryBuilder> i
this(name, (Object) value);
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public FuzzyQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
public FuzzyQueryBuilder fuzziness(Fuzziness fuzziness) {
this.fuzziness = fuzziness;
return this;

View File

@ -42,6 +42,10 @@ public class LimitQueryBuilder extends AbstractQueryBuilder<LimitQueryBuilder> {
this.limit = limit;
}
public int limit() {
return limit;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);

View File

@ -56,4 +56,25 @@ public interface QueryBuilder<QB extends QueryBuilder> extends NamedWriteable<QB
*/
//norelease once we move to serializing queries over the wire in Streamable format, this method shouldn't be needed anymore
BytesReference buildAsBytes();
/**
* Sets the query name for the query.
*/
QB queryName(String queryName);
/**
* Returns the query name for the query.
*/
String queryName();
/**
* Returns the boost for this query.
*/
float boost();
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
QB boost(float boost);
}

View File

@ -58,18 +58,6 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
return this.queryBuilder;
}
@Override
public QueryFilterBuilder boost(float boost) {
//no-op: QueryFilterParser doesn't support boost, we should be consistent and ignore it here too.
return this;
}
@Override
public QueryFilterBuilder queryName(String queryName) {
//no-op: QueryFilterParser doesn't support _name, we should be consistent and ignore it here too.
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(NAME);

View File

@ -52,6 +52,7 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder<SpanMultiTer
builder.startObject(NAME);
builder.field(SpanMultiTermQueryParser.MATCH_NAME);
multiTermQueryBuilder.toXContent(builder, params);
printBoostAndQueryName(builder);
builder.endObject();
}
@ -101,16 +102,4 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder<SpanMultiTer
public String getName() {
return NAME;
}
@Override
public SpanMultiTermQueryBuilder boost(float boost) {
//no-op: SpanMultiTermQueryParser doesn't support boost, we should be consistent and ignore it here too.
return this;
}
@Override
public SpanMultiTermQueryBuilder queryName(String queryName) {
//no-op: SpanMultiTermQueryParser doesn't support _name, we should be consistent and ignore it here too.
return this;
}
}

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.query;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import java.io.IOException;
@ -44,24 +43,40 @@ public class SpanMultiTermQueryParser extends BaseQueryParser {
@Override
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
Token token = parser.nextToken();
if (!MATCH_NAME.equals(parser.currentName()) || token != XContentParser.Token.FIELD_NAME) {
throw new QueryParsingException(parseContext, "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause");
String currentFieldName = null;
MultiTermQueryBuilder subQuery = null;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (MATCH_NAME.equals(currentFieldName)) {
QueryBuilder innerQuery = parseContext.parseInnerQueryBuilder();
if (innerQuery instanceof MultiTermQueryBuilder == false) {
throw new QueryParsingException(parseContext, "[span_multi] [" + MATCH_NAME + "] must be of type multi term query");
}
subQuery = (MultiTermQueryBuilder) innerQuery;
} else {
throw new QueryParsingException(parseContext, "[span_multi] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[span_multi] query does not support [" + currentFieldName + "]");
}
}
}
token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new QueryParsingException(parseContext, "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause");
if (subQuery == null) {
throw new QueryParsingException(parseContext, "[span_multi] must have [" + MATCH_NAME + "] multi term query clause");
}
QueryBuilder subQuery = parseContext.parseInnerQueryBuilder();
if (subQuery instanceof MultiTermQueryBuilder == false) {
throw new QueryParsingException(parseContext, "spanMultiTerm [" + MATCH_NAME + "] must be of type multi term query");
}
parser.nextToken();
return new SpanMultiTermQueryBuilder((MultiTermQueryBuilder) subQuery);
return new SpanMultiTermQueryBuilder(subQuery).queryName(queryName).boost(boost);
}
@Override

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -27,28 +27,17 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
@SuppressWarnings("deprecation")
public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(AndQueryBuilder queryBuilder, QueryParseContext context) throws QueryParsingException, IOException {
if (queryBuilder.filters().isEmpty()) {
return null;
}
BooleanQuery query = new BooleanQuery();
for (QueryBuilder subQuery : queryBuilder.filters()) {
Query innerQuery = subQuery.toQuery(context);
if (innerQuery != null) {
query.add(innerQuery, Occur.MUST);
}
}
if (query.clauses().isEmpty()) {
return null;
}
return query;
}
/**
* @return a AndQueryBuilder with random limit between 0 and 20
*/
@ -62,6 +51,33 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
return query;
}
@Override
protected void doAssertLuceneQuery(AndQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
if (queryBuilder.filters().isEmpty()) {
assertThat(query, nullValue());
} else {
List<Query> clauses = new ArrayList<>();
for (QueryBuilder innerFilter : queryBuilder.filters()) {
Query clause = innerFilter.toQuery(context);
if (clause != null) {
clauses.add(clause);
}
}
if (clauses.isEmpty()) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.clauses().size(), equalTo(clauses.size()));
Iterator<Query> queryIterator = clauses.iterator();
for (BooleanClause booleanClause : booleanQuery) {
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanClause.getQuery(), equalTo(queryIterator.next()));
}
}
}
}
/**
* test corner case where no inner queries exist
*/

View File

@ -25,7 +25,7 @@ import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Injector;
@ -180,11 +180,13 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
protected final QB createTestQueryBuilder() {
QB query = doCreateTestQueryBuilder();
if (randomBoolean()) {
query.boost(2.0f / randomIntBetween(1, 20));
}
if (randomBoolean()) {
query.queryName(randomAsciiOfLengthBetween(1, 10));
if (supportsBoostAndQueryName()) {
if (randomBoolean()) {
query.boost(2.0f / randomIntBetween(1, 20));
}
if (randomBoolean()) {
query.queryName(randomAsciiOfLengthBetween(1, 10));
}
}
return query;
}
@ -219,48 +221,71 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
*/
@Test
public void testToQuery() throws IOException {
QB testQuery = createTestQueryBuilder();
QueryParseContext context = createContext();
context.setAllowUnmappedFields(true);
Query expectedQuery = createExpectedQuery(testQuery, context);
Query actualQuery = testQuery.toQuery(context);
// expectedQuery can be null, e.g. in case of BoostingQueryBuilder
// with inner clause that returns null itself
if (expectedQuery == null) {
assertNull("Expected a null query, saw some object.", actualQuery);
} else {
assertThat(actualQuery, instanceOf(expectedQuery.getClass()));
assertThat(actualQuery, equalTo(expectedQuery));
assertLuceneQuery(testQuery, actualQuery, context);
QB firstQuery = createTestQueryBuilder();
Query firstLuceneQuery = firstQuery.toQuery(context);
assertLuceneQuery(firstQuery, firstLuceneQuery, context);
try (BytesStreamOutput output = new BytesStreamOutput()) {
firstQuery.writeTo(output);
try (StreamInput in = new FilterStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends QueryBuilder> prototype = queryParserService.queryParser(firstQuery.getName()).getBuilderPrototype();
@SuppressWarnings("unchecked")
QB secondQuery = (QB)prototype.readFrom(in);
//query _name never should affect the result of toQuery, we randomly set it to make sure
if (randomBoolean()) {
secondQuery.queryName(secondQuery.queryName() == null ? randomAsciiOfLengthBetween(1, 30) : secondQuery.queryName() + randomAsciiOfLengthBetween(1, 10));
}
Query secondLuceneQuery = secondQuery.toQuery(context);
assertLuceneQuery(secondQuery, secondLuceneQuery, context);
assertThat("two equivalent query builders lead to different lucene queries", secondLuceneQuery, equalTo(firstLuceneQuery));
//if the initial lucene query is null, changing its boost won't have any effect, we shouldn't test that
//otherwise makes sure that boost is taken into account in toQuery
if (firstLuceneQuery != null) {
secondQuery.boost(firstQuery.boost() + 1f + randomFloat());
//some queries don't support boost, their setter is a no-op
if (supportsBoostAndQueryName()) {
Query thirdLuceneQuery = secondQuery.toQuery(context);
assertThat("modifying the boost doesn't affect the corresponding lucene query", firstLuceneQuery, not(equalTo(thirdLuceneQuery)));
}
}
}
}
}
protected final Query createExpectedQuery(QB queryBuilder, QueryParseContext context) throws IOException {
Query expectedQuery = doCreateExpectedQuery(queryBuilder, context);
if (expectedQuery != null) {
expectedQuery.setBoost(queryBuilder.boost());
}
return expectedQuery;
/**
* Few queries allow you to set the boost and queryName but don't do anything with it. This method allows
* to disable boost and queryName related tests for those queries.
*/
protected boolean supportsBoostAndQueryName() {
return true;
}
/**
* Creates the expected lucene query given the current {@link QueryBuilder} and {@link QueryParseContext}.
* The returned query will be compared with the result of {@link QueryBuilder#toQuery(QueryParseContext)} to test its behaviour.
* Checks the result of {@link QueryBuilder#toQuery(QueryParseContext)} given the original {@link QueryBuilder} and {@link QueryParseContext}.
* Verifies that named queries and boost are properly handled and delegates to {@link #doAssertLuceneQuery(AbstractQueryBuilder, Query, QueryParseContext)}
* for query specific checks.
*/
protected abstract Query doCreateExpectedQuery(QB queryBuilder, QueryParseContext context) throws IOException;
/**
* Run after default equality comparison between lucene expected query and result of {@link QueryBuilder#toQuery(QueryParseContext)}.
* Can contain additional assertions that are query specific. Default implementation verifies that names queries are properly handled.
*/
protected final void assertLuceneQuery(QB queryBuilder, Query query, QueryParseContext context) {
protected final void assertLuceneQuery(QB queryBuilder, Query query, QueryParseContext context) throws IOException {
if (queryBuilder.queryName() != null) {
Query namedQuery = context.copyNamedQueries().get(queryBuilder.queryName());
assertThat(namedQuery, equalTo(query));
}
if (query != null) {
assertThat(query.getBoost(), equalTo(queryBuilder.boost()));
}
doAssertLuceneQuery(queryBuilder, query, context);
}
/**
* Checks the result of {@link QueryBuilder#toQuery(QueryParseContext)} given the original {@link QueryBuilder} and {@link QueryParseContext}.
* Contains the query specific checks to be implemented by subclasses.
*/
protected abstract void doAssertLuceneQuery(QB queryBuilder, Query query, QueryParseContext context) throws IOException;
/**
* Test serialization and deserialization of the test query.
*/
@ -283,7 +308,9 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
* @return a new {@link QueryParseContext} based on the base test index and queryParserService
*/
protected static QueryParseContext createContext() {
return new QueryParseContext(index, queryParserService);
QueryParseContext queryParseContext = new QueryParseContext(index, queryParserService);
queryParseContext.parseFieldMatcher(ParseFieldMatcher.EMPTY);
return queryParseContext;
}
protected static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException {
@ -325,13 +352,13 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
protected static String getRandomRewriteMethod() {
String rewrite;
if (randomBoolean()) {
rewrite = randomFrom(new ParseField[]{QueryParsers.CONSTANT_SCORE,
rewrite = randomFrom(QueryParsers.CONSTANT_SCORE,
QueryParsers.SCORING_BOOLEAN,
QueryParsers.CONSTANT_SCORE_BOOLEAN}).getPreferredName();
QueryParsers.CONSTANT_SCORE_BOOLEAN).getPreferredName();
} else {
rewrite = randomFrom(new ParseField[]{QueryParsers.TOP_TERMS,
rewrite = randomFrom(QueryParsers.TOP_TERMS,
QueryParsers.TOP_TERMS_BOOST,
QueryParsers.TOP_TERMS_BLENDED_FREQS}).getPreferredName() + "1";
QueryParsers.TOP_TERMS_BLENDED_FREQS).getPreferredName() + "1";
}
return rewrite;
}

View File

@ -19,11 +19,6 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.junit.Ignore;
import org.junit.Test;
@ -79,15 +74,14 @@ public abstract class BaseTermQueryTestCase<QB extends BaseTermQueryBuilder<QB>>
@Test
public void testValidate() throws QueryParsingException {
QB queryBuilder = createQueryBuilder("all", "good");
QB queryBuilder = createQueryBuilder(randomAsciiOfLengthBetween(1, 30), randomAsciiOfLengthBetween(1, 30));
assertNull(queryBuilder.validate());
queryBuilder = createQueryBuilder(null, "Term");
queryBuilder = createQueryBuilder(null, randomAsciiOfLengthBetween(1, 30));
assertNotNull(queryBuilder.validate());
assertThat(queryBuilder.validate().validationErrors().size(), is(1));
queryBuilder = createQueryBuilder("", "Term");
queryBuilder = createQueryBuilder("", randomAsciiOfLengthBetween(1, 30));
assertNotNull(queryBuilder.validate());
assertThat(queryBuilder.validate().validationErrors().size(), is(1));
@ -95,21 +89,4 @@ public abstract class BaseTermQueryTestCase<QB extends BaseTermQueryBuilder<QB>>
assertNotNull(queryBuilder.validate());
assertThat(queryBuilder.validate().validationErrors().size(), is(2));
}
@Override
protected Query doCreateExpectedQuery(QB queryBuilder, QueryParseContext context) {
BytesRef value = null;
if (getCurrentTypes().length > 0) {
if (queryBuilder.fieldName().equals(BOOLEAN_FIELD_NAME) || queryBuilder.fieldName().equals(INT_FIELD_NAME) || queryBuilder.fieldName().equals(DOUBLE_FIELD_NAME)) {
MappedFieldType mapper = context.fieldMapper(queryBuilder.fieldName());
value = mapper.indexedValueForSearch(queryBuilder.value);
}
}
if (value == null) {
value = BytesRefs.toBytesRef(queryBuilder.value);
}
return createLuceneTermQuery(new Term(queryBuilder.fieldName(), value));
}
protected abstract Query createLuceneTermQuery(Term term);
}

View File

@ -20,17 +20,18 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.elasticsearch.common.lucene.search.Queries.fixNegativeQueryIfNeeded;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class BoolQueryBuilderTest extends BaseQueryTestCase<BoolQueryBuilder> {
@ -66,34 +67,53 @@ public class BoolQueryBuilderTest extends BaseQueryTestCase<BoolQueryBuilder> {
}
@Override
protected Query doCreateExpectedQuery(BoolQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
protected void doAssertLuceneQuery(BoolQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
if (!queryBuilder.hasClauses()) {
return new MatchAllDocsQuery();
}
assertThat(query, instanceOf(MatchAllDocsQuery.class));
} else {
List<BooleanClause> clauses = new ArrayList<>();
clauses.addAll(getBooleanClauses(queryBuilder.must(), BooleanClause.Occur.MUST, context));
clauses.addAll(getBooleanClauses(queryBuilder.mustNot(), BooleanClause.Occur.MUST_NOT, context));
clauses.addAll(getBooleanClauses(queryBuilder.should(), BooleanClause.Occur.SHOULD, context));
clauses.addAll(getBooleanClauses(queryBuilder.filter(), BooleanClause.Occur.FILTER, context));
BooleanQuery boolQuery = new BooleanQuery(queryBuilder.disableCoord());
addBooleanClauses(context, boolQuery, queryBuilder.must(), BooleanClause.Occur.MUST);
addBooleanClauses(context, boolQuery, queryBuilder.mustNot(), BooleanClause.Occur.MUST_NOT);
addBooleanClauses(context, boolQuery, queryBuilder.should(), BooleanClause.Occur.SHOULD);
addBooleanClauses(context, boolQuery, queryBuilder.filter(), BooleanClause.Occur.FILTER);
if (boolQuery.clauses().isEmpty()) {
return new MatchAllDocsQuery();
}
Queries.applyMinimumShouldMatch(boolQuery, queryBuilder.minimumNumberShouldMatch());
return queryBuilder.adjustPureNegative() ? fixNegativeQueryIfNeeded(boolQuery) : boolQuery;
}
private static void addBooleanClauses(QueryParseContext parseContext, BooleanQuery booleanQuery, List<QueryBuilder> clauses, Occur occurs)
throws IOException {
for (QueryBuilder query : clauses) {
Query innerQuery = query.toQuery(parseContext);
if (innerQuery != null) {
booleanQuery.add(new BooleanClause(innerQuery, occurs));
if (clauses.isEmpty()) {
assertThat(query, instanceOf(MatchAllDocsQuery.class));
} else {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
if (queryBuilder.adjustPureNegative()) {
boolean isNegative = true;
for (BooleanClause clause : clauses) {
if (clause.isProhibited() == false) {
isNegative = false;
break;
}
}
if (isNegative) {
clauses.add(new BooleanClause(new MatchAllDocsQuery(), BooleanClause.Occur.MUST));
}
}
assertThat(booleanQuery.clauses().size(), equalTo(clauses.size()));
Iterator<BooleanClause> clauseIterator = clauses.iterator();
for (BooleanClause booleanClause : booleanQuery.getClauses()) {
assertThat(booleanClause, equalTo(clauseIterator.next()));
}
}
}
}
private static List<BooleanClause> getBooleanClauses(List<QueryBuilder> queryBuilders, BooleanClause.Occur occur, QueryParseContext parseContext) throws IOException {
List<BooleanClause> clauses = new ArrayList<>();
for (QueryBuilder query : queryBuilders) {
Query innerQuery = query.toQuery(parseContext);
if (innerQuery != null) {
clauses.add(new BooleanClause(innerQuery, occur));
}
}
return clauses;
}
@Test
public void testValidate() {
BoolQueryBuilder booleanQuery = new BoolQueryBuilder();

View File

@ -25,6 +25,9 @@ import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
public class BoostingQueryBuilderTest extends BaseQueryTestCase<BoostingQueryBuilder> {
@Override
@ -35,13 +38,14 @@ public class BoostingQueryBuilderTest extends BaseQueryTestCase<BoostingQueryBui
}
@Override
protected Query doCreateExpectedQuery(BoostingQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
protected void doAssertLuceneQuery(BoostingQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query positive = queryBuilder.positive().toQuery(context);
Query negative = queryBuilder.negative().toQuery(context);
if (positive == null || negative == null) {
return null;
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(BoostingQuery.class));
}
return new BoostingQuery(positive, negative, queryBuilder.negativeBoost());
}
@Test

View File

@ -19,15 +19,14 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queries.ExtendedCommonTermsQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.Query;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class CommonTermsQueryBuilderTest extends BaseQueryTestCase<CommonTermsQueryBuilder> {
@ -77,30 +76,11 @@ public class CommonTermsQueryBuilderTest extends BaseQueryTestCase<CommonTermsQu
}
@Override
protected Query doCreateExpectedQuery(CommonTermsQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
String fieldName = queryBuilder.fieldName();
Analyzer analyzer = context.mapperService().searchAnalyzer();
// handle mapped field
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType != null) {
fieldName = fieldType.names().indexName();
analyzer = context.getSearchAnalyzer(fieldType);
}
// handle specified analyzer
if (queryBuilder.analyzer() != null) {
analyzer = context.analysisService().analyzer(queryBuilder.analyzer());
}
Occur highFreqOccur = queryBuilder.highFreqOperator().toBooleanClauseOccur();
Occur lowFreqOccur = queryBuilder.lowFreqOperator().toBooleanClauseOccur();
ExtendedCommonTermsQuery expectedQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, queryBuilder.cutoffFrequency(),
queryBuilder.disableCoord(), fieldType);
CommonTermsQueryBuilder.parseQueryString(expectedQuery, queryBuilder.text(), fieldName, analyzer,
queryBuilder.lowFreqMinimumShouldMatch(), queryBuilder.highFreqMinimumShouldMatch());
return expectedQuery;
protected void doAssertLuceneQuery(CommonTermsQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(ExtendedCommonTermsQuery.class));
ExtendedCommonTermsQuery extendedCommonTermsQuery = (ExtendedCommonTermsQuery) query;
assertThat(extendedCommonTermsQuery.getHighFreqMinimumNumberShouldMatchSpec(), equalTo(queryBuilder.highFreqMinimumShouldMatch()));
assertThat(extendedCommonTermsQuery.getLowFreqMinimumNumberShouldMatchSpec(), equalTo(queryBuilder.lowFreqMinimumShouldMatch()));
}
@Test

View File

@ -27,16 +27,11 @@ import org.junit.Test;
import java.io.IOException;
public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantScoreQueryBuilder> {
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
@Override
protected Query doCreateExpectedQuery(ConstantScoreQueryBuilder testBuilder, QueryParseContext context) throws QueryParsingException, IOException {
Query innerQuery = testBuilder.query().toQuery(context);
if (innerQuery != null) {
return new ConstantScoreQuery(innerQuery);
}
return null;
}
public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantScoreQueryBuilder> {
/**
* @return a {@link ConstantScoreQueryBuilder} with random boost between 0.1f and 2.0f
@ -46,6 +41,18 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
return new ConstantScoreQueryBuilder(RandomQueryBuilder.createQuery(random()));
}
@Override
protected void doAssertLuceneQuery(ConstantScoreQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query innerQuery = queryBuilder.query().toQuery(context);
if (innerQuery == null) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(ConstantScoreQuery.class));
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
assertThat(constantScoreQuery.getQuery(), equalTo(innerQuery));
}
}
/**
* test that missing "filter" element causes {@link QueryParsingException}
*/

View File

@ -27,18 +27,14 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(DisMaxQueryBuilder testBuilder, QueryParseContext context) throws QueryParsingException, IOException {
Collection<Query> queries = AbstractQueryBuilder.toQueries(testBuilder.queries(), context);
if (queries.isEmpty()) {
return null;
}
return new DisjunctionMaxQuery(queries, testBuilder.tieBreaker());
}
/**
* @return a {@link DisMaxQueryBuilder} with random inner queries
*/
@ -55,6 +51,23 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
return dismax;
}
@Override
protected void doAssertLuceneQuery(DisMaxQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Collection<Query> queries = AbstractQueryBuilder.toQueries(queryBuilder.queries(), context);
if (queries.isEmpty()) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(DisjunctionMaxQuery.class));
DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
assertThat(disjunctionMaxQuery.getTieBreakerMultiplier(), equalTo(queryBuilder.tieBreaker()));
assertThat(disjunctionMaxQuery.getDisjuncts().size(), equalTo(queries.size()));
Iterator<Query> queryIterator = queries.iterator();
for (int i = 0; i < disjunctionMaxQuery.getDisjuncts().size(); i++) {
assertThat(disjunctionMaxQuery.getDisjuncts().get(i), equalTo(queryIterator.next()));
}
}
}
/**
* test `null`return value for missing inner queries
* @throws IOException

View File

@ -19,52 +19,21 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;
import org.elasticsearch.index.mapper.object.ObjectMapper;
import java.io.IOException;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class ExistsQueryBuilderTest extends BaseQueryTestCase<ExistsQueryBuilder> {
private static Collection<String> getFieldNamePattern(String fieldName, QueryParseContext context) {
if (getCurrentTypes().length > 0 && fieldName.equals(BaseQueryTestCase.OBJECT_FIELD_NAME)) {
// "object" field has two inner fields (age, price), so if query hits that field, we
// extend field name with wildcard to match both nested fields. This is similar to what
// is done internally in ExistsQueryBuilder.toQuery()
fieldName = fieldName + ".*";
}
return context.simpleMatchToIndexNames(fieldName);
}
@Override
protected Query doCreateExpectedQuery(ExistsQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType)context.mapperService().fullName(FieldNamesFieldMapper.NAME);
Collection<String> fields = getFieldNamePattern(queryBuilder.name(), context);
if (fields.isEmpty() || fieldNamesFieldType == null) {
return Queries.newMatchNoDocsQuery();
}
BooleanQuery boolFilter = new BooleanQuery();
for (String field : fields) {
if (fieldNamesFieldType.isEnabled()) {
boolFilter.add(fieldNamesFieldType.termQuery(field, context), BooleanClause.Occur.SHOULD);
} else {
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType == null) {
boolFilter.add(new TermRangeQuery(field, null, null, true, true), BooleanClause.Occur.SHOULD);
} else {
boolFilter.add(fieldType.rangeQuery(null, null, true, true), BooleanClause.Occur.SHOULD);
}
}
}
return new ConstantScoreQuery(boolFilter);
}
@Override
protected ExistsQueryBuilder doCreateTestQueryBuilder() {
String fieldPattern;
@ -83,4 +52,30 @@ public class ExistsQueryBuilderTest extends BaseQueryTestCase<ExistsQueryBuilder
}
return new ExistsQueryBuilder(fieldPattern);
}
@Override
protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
String fieldPattern = queryBuilder.name();
ObjectMapper objectMapper = context.getObjectMapper(fieldPattern);
if (objectMapper != null) {
// automatic make the object mapper pattern
fieldPattern = fieldPattern + ".*";
}
Collection<String> fields = context.simpleMatchToIndexNames(fieldPattern);
if (getCurrentTypes().length == 0 || fields.size() == 0) {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.clauses().size(), equalTo(0));
} else {
assertThat(query, instanceOf(ConstantScoreQuery.class));
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) constantScoreQuery.getQuery();
assertThat(booleanQuery.clauses().size(), equalTo(fields.size()));
for (int i = 0; i < fields.size(); i++) {
BooleanClause booleanClause = booleanQuery.clauses().get(i);
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
}
}
}
}

View File

@ -27,18 +27,13 @@ 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;
@SuppressWarnings("deprecation")
public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuilder> {
@Override
protected Query doCreateExpectedQuery(FQueryFilterBuilder queryBuilder, QueryParseContext context) throws QueryParsingException, IOException {
Query query = queryBuilder.innerQuery().toQuery(context);
if (query != null) {
return new ConstantScoreQuery(query);
}
return null;
}
/**
* @return a FQueryFilterBuilder with random inner query
*/
@ -48,6 +43,18 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
return new FQueryFilterBuilder(innerQuery);
}
@Override
protected void doAssertLuceneQuery(FQueryFilterBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query innerQuery = queryBuilder.innerQuery().toQuery(context);
if (innerQuery == null) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(ConstantScoreQuery.class));
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
assertThat(constantScoreQuery.getQuery(), equalTo(innerQuery));
}
}
/**
* test corner case where no inner query exist
*/

View File

@ -21,26 +21,16 @@ package org.elasticsearch.index.query;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.FieldMaskingSpanQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class FieldMaskingSpanQueryBuilderTest extends BaseQueryTestCase<FieldMaskingSpanQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(FieldMaskingSpanQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
String fieldInQuery = testQueryBuilder.fieldName();
MappedFieldType fieldType = context.fieldMapper(fieldInQuery);
if (fieldType != null) {
fieldInQuery = fieldType.names().indexName();
}
SpanQuery innerQuery = (SpanQuery) testQueryBuilder.innerQuery().toQuery(context);
return new FieldMaskingSpanQuery(innerQuery, fieldInQuery);
}
@Override
protected FieldMaskingSpanQueryBuilder doCreateTestQueryBuilder() {
String fieldName;
@ -53,6 +43,19 @@ public class FieldMaskingSpanQueryBuilderTest extends BaseQueryTestCase<FieldMas
return new FieldMaskingSpanQueryBuilder(innerQuery, fieldName);
}
@Override
protected void doAssertLuceneQuery(FieldMaskingSpanQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
String fieldInQuery = queryBuilder.fieldName();
MappedFieldType fieldType = context.fieldMapper(fieldInQuery);
if (fieldType != null) {
fieldInQuery = fieldType.names().indexName();
}
assertThat(query, instanceOf(FieldMaskingSpanQuery.class));
FieldMaskingSpanQuery fieldMaskingSpanQuery = (FieldMaskingSpanQuery) query;
assertThat(fieldMaskingSpanQuery.getField(), equalTo(fieldInQuery));
assertThat(fieldMaskingSpanQuery.getMaskedQuery(), equalTo(queryBuilder.innerQuery().toQuery(context)));
}
@Test
public void testValidate() {
String fieldName = null;

View File

@ -19,6 +19,8 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;
@ -26,6 +28,10 @@ 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;
@SuppressWarnings("deprecation")
public class FilteredQueryBuilderTest extends BaseQueryTestCase<FilteredQueryBuilder> {
@ -33,30 +39,32 @@ public class FilteredQueryBuilderTest extends BaseQueryTestCase<FilteredQueryBui
protected FilteredQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder queryBuilder = RandomQueryBuilder.createQuery(random());
QueryBuilder filterBuilder = RandomQueryBuilder.createQuery(random());
FilteredQueryBuilder query = new FilteredQueryBuilder(queryBuilder, filterBuilder);
return query;
return new FilteredQueryBuilder(queryBuilder, filterBuilder);
}
@Override
protected Query doCreateExpectedQuery(FilteredQueryBuilder qb, QueryParseContext context) throws IOException {
Query query = qb.query().toQuery(context);
Query filter = qb.filter().toQuery(context);
if (query == null) {
return null;
}
Query result;
if (filter == null || Queries.isConstantMatchAllQuery(filter)) {
result = qb.query().toQuery(context);
} else if (Queries.isConstantMatchAllQuery(query)) {
result = new ConstantScoreQuery(filter);
protected void doAssertLuceneQuery(FilteredQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query innerQuery = queryBuilder.query().toQuery(context);
if (innerQuery == null) {
assertThat(query, nullValue());
} else {
result = Queries.filtered(qb.query().toQuery(context), filter);
Query innerFilter = queryBuilder.filter().toQuery(context);
if (innerFilter == null || Queries.isConstantMatchAllQuery(innerFilter)) {
innerQuery.setBoost(queryBuilder.boost());
assertThat(query, equalTo(innerQuery));
} else if (Queries.isConstantMatchAllQuery(innerQuery)) {
assertThat(query, instanceOf(ConstantScoreQuery.class));
assertThat(((ConstantScoreQuery)query).getQuery(), equalTo(innerFilter));
} else {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.clauses().size(), equalTo(2));
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanQuery.clauses().get(0).getQuery(), equalTo(innerQuery));
assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
assertThat(booleanQuery.clauses().get(1).getQuery(), equalTo(innerFilter));
}
}
result.setBoost(qb.boost());
return result;
}
@Test

View File

@ -20,19 +20,18 @@
package org.elasticsearch.index.query;
import com.google.common.collect.Sets;
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.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class IdsQueryBuilderTest extends BaseQueryTestCase<IdsQueryBuilder> {
@ -50,26 +49,6 @@ public class IdsQueryBuilderTest extends BaseQueryTestCase<IdsQueryBuilder> {
context.indexQueryParserService().queryParser("ids").fromXContent(context);
}
@Override
protected Query doCreateExpectedQuery(IdsQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
Query expectedQuery;
if (queryBuilder.ids().size() == 0) {
expectedQuery = Queries.newMatchNoDocsQuery();
} else {
String[] typesForQuery;
if (queryBuilder.types() == null || queryBuilder.types().length == 0) {
Collection<String> queryTypes = context.queryTypes();
typesForQuery = queryTypes.toArray(new String[queryTypes.size()]);
} else if (queryBuilder.types().length == 1 && MetaData.ALL.equals(queryBuilder.types()[0])) {
typesForQuery = getCurrentTypes();
} else {
typesForQuery = queryBuilder.types();
}
expectedQuery = new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(Sets.newHashSet(typesForQuery), queryBuilder.ids()));
}
return expectedQuery;
}
@Override
protected IdsQueryBuilder doCreateTestQueryBuilder() {
String[] types;
@ -105,4 +84,14 @@ public class IdsQueryBuilderTest extends BaseQueryTestCase<IdsQueryBuilder> {
}
return query;
}
@Override
protected void doAssertLuceneQuery(IdsQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
if (queryBuilder.ids().size() == 0) {
assertThat(query, instanceOf(BooleanQuery.class));
assertThat(((BooleanQuery)query).clauses().size(), equalTo(0));
} else {
assertThat(query, instanceOf(TermsQuery.class));
}
}
}

View File

@ -19,17 +19,15 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.instanceOf;
public class LimitQueryBuilderTest extends BaseQueryTestCase<LimitQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(LimitQueryBuilder queryBuilder, QueryParseContext context) {
// this filter is deprecated and parses to a filter that matches everything
return Queries.newMatchAllQuery();
}
/**
* @return a LimitQueryBuilder with random limit between 0 and 20
*/
@ -37,4 +35,9 @@ public class LimitQueryBuilderTest extends BaseQueryTestCase<LimitQueryBuilder>
protected LimitQueryBuilder doCreateTestQueryBuilder() {
return new LimitQueryBuilder(randomIntBetween(0, 20));
}
@Override
protected void doAssertLuceneQuery(LimitQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(MatchAllDocsQuery.class));
}
}

View File

@ -22,16 +22,19 @@ package org.elasticsearch.index.query;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
public class MatchAllQueryBuilderTest extends BaseQueryTestCase<MatchAllQueryBuilder> {
import java.io.IOException;
@Override
protected Query doCreateExpectedQuery(MatchAllQueryBuilder queryBuilder, QueryParseContext context) {
return new MatchAllDocsQuery();
}
import static org.hamcrest.CoreMatchers.instanceOf;
public class MatchAllQueryBuilderTest extends BaseQueryTestCase<MatchAllQueryBuilder> {
@Override
protected MatchAllQueryBuilder doCreateTestQueryBuilder() {
return new MatchAllQueryBuilder();
}
@Override
protected void doAssertLuceneQuery(MatchAllQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(MatchAllDocsQuery.class));
}
}

View File

@ -19,15 +19,10 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.*;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;
import org.elasticsearch.index.mapper.object.ObjectMapper;
import org.apache.lucene.search.Query;
import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
import static org.hamcrest.Matchers.is;
@ -35,7 +30,7 @@ public class MissingQueryBuilderTest extends BaseQueryTestCase<MissingQueryBuild
@Override
protected MissingQueryBuilder doCreateTestQueryBuilder() {
MissingQueryBuilder query = new MissingQueryBuilder(getRandomFieldName());
MissingQueryBuilder query = new MissingQueryBuilder(randomBoolean() ? randomFrom(mappedFieldNames) : randomAsciiOfLengthBetween(1, 10));
if (randomBoolean()) {
query.nullValue(randomBoolean());
}
@ -49,115 +44,9 @@ public class MissingQueryBuilderTest extends BaseQueryTestCase<MissingQueryBuild
return query;
}
private String getRandomFieldName() {
if (randomBoolean()) {
return randomAsciiOfLengthBetween(1, 10);
}
switch (randomIntBetween(0, 3)) {
case 0:
return BOOLEAN_FIELD_NAME;
case 1:
return STRING_FIELD_NAME;
case 2:
return INT_FIELD_NAME;
case 3:
return DOUBLE_FIELD_NAME;
default:
throw new UnsupportedOperationException();
}
}
@Override
protected Query doCreateExpectedQuery(MissingQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
final boolean existence = queryBuilder.existence();
final boolean nullValue = queryBuilder.nullValue();
String fieldPattern = queryBuilder.fieldPattern();
if (!existence && !nullValue) {
throw new QueryParsingException(context, "missing must have either existence, or null_value, or both set to true");
}
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType) context.mapperService().fullName(FieldNamesFieldMapper.NAME);
if (fieldNamesFieldType == null) {
// can only happen when no types exist, so no docs exist either
return Queries.newMatchNoDocsQuery();
}
ObjectMapper objectMapper = context.getObjectMapper(fieldPattern);
if (objectMapper != null) {
// automatic make the object mapper pattern
fieldPattern = fieldPattern + ".*";
}
Collection<String> fields = context.simpleMatchToIndexNames(fieldPattern);
if (fields.isEmpty()) {
if (existence) {
// if we ask for existence of fields, and we found none, then we should match on all
return Queries.newMatchAllQuery();
}
return null;
}
Query existenceFilter = null;
Query nullFilter = null;
if (existence) {
BooleanQuery boolFilter = new BooleanQuery();
for (String field : fields) {
MappedFieldType fieldType = context.fieldMapper(field);
Query filter = null;
if (fieldNamesFieldType.isEnabled()) {
final String f;
if (fieldType != null) {
f = fieldType.names().indexName();
} else {
f = field;
}
filter = fieldNamesFieldType.termQuery(f, context);
}
// if _field_names are not indexed, we need to go the slow way
if (filter == null && fieldType != null) {
filter = fieldType.rangeQuery(null, null, true, true);
}
if (filter == null) {
filter = new TermRangeQuery(field, null, null, true, true);
}
boolFilter.add(filter, BooleanClause.Occur.SHOULD);
}
existenceFilter = boolFilter;
existenceFilter = Queries.not(existenceFilter);;
}
if (nullValue) {
for (String field : fields) {
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType != null) {
nullFilter = fieldType.nullValueQuery();
}
}
}
Query filter;
if (nullFilter != null) {
if (existenceFilter != null) {
BooleanQuery combined = new BooleanQuery();
combined.add(existenceFilter, BooleanClause.Occur.SHOULD);
combined.add(nullFilter, BooleanClause.Occur.SHOULD);
// cache the not filter as well, so it will be faster
filter = combined;
} else {
filter = nullFilter;
}
} else {
filter = existenceFilter;
}
if (filter == null) {
return null;
}
return new ConstantScoreQuery(filter);
protected void doAssertLuceneQuery(MissingQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
//too many mapping dependent cases to test, we don't want to end up duplication the toQuery method
}
@Test

View File

@ -19,23 +19,21 @@
package org.elasticsearch.index.query;
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.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.junit.Test;
import java.io.IOException;
public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
@Override
protected Query doCreateExpectedQuery(NotQueryBuilder queryBuilder, QueryParseContext context) throws QueryParsingException, IOException {
if (queryBuilder.filter().toQuery(context) == null) {
return null;
}
return Queries.not(queryBuilder.filter().toQuery(context));
}
public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
/**
* @return a NotQueryBuilder with random limit between 0 and 20
@ -45,6 +43,22 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
return new NotQueryBuilder(RandomQueryBuilder.createQuery(random()));
}
@Override
protected void doAssertLuceneQuery(NotQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query filter = queryBuilder.filter().toQuery(context);
if (filter == null) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.clauses().size(), equalTo(2));
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(MatchAllDocsQuery.class));
assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
assertThat(booleanQuery.clauses().get(1).getQuery(), equalTo(filter));
}
}
/**
* @throws IOException
*/

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -27,10 +27,18 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
@SuppressWarnings("deprecation")
public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
/*
@Override
protected Query doCreateExpectedQuery(OrQueryBuilder queryBuilder, QueryParseContext context) throws QueryParsingException, IOException {
if (queryBuilder.filters().isEmpty()) {
@ -49,6 +57,7 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
}
return query;
}
*/
/**
* @return an OrQueryBuilder with random limit between 0 and 20
@ -63,6 +72,34 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
return query;
}
@Override
protected void doAssertLuceneQuery(OrQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
if (queryBuilder.filters().isEmpty()) {
assertThat(query, nullValue());
} else {
List<Query> innerQueries = new ArrayList<>();
for (QueryBuilder subQuery : queryBuilder.filters()) {
Query innerQuery = subQuery.toQuery(context);
// ignore queries that are null
if (innerQuery != null) {
innerQueries.add(innerQuery);
}
}
if (innerQueries.isEmpty()) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) query;
assertThat(booleanQuery.clauses().size(), equalTo(innerQueries.size()));
Iterator<Query> queryIterator = innerQueries.iterator();
for (BooleanClause booleanClause : booleanQuery) {
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
assertThat(booleanClause.getQuery(), equalTo(queryIterator.next()));
}
}
}
}
/**
* test corner case where no inner queries exist
*/

View File

@ -19,18 +19,14 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class PrefixQueryBuilderTest extends BaseQueryTestCase<PrefixQueryBuilder> {
@ -48,26 +44,10 @@ public class PrefixQueryBuilderTest extends BaseQueryTestCase<PrefixQueryBuilder
}
@Override
protected Query doCreateExpectedQuery(PrefixQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
//norelease fix to be removed to avoid NPE on unmapped fields (Dtests.seed=BF5D7566DECBC5B1)
context.parseFieldMatcher(randomBoolean() ? ParseFieldMatcher.EMPTY : ParseFieldMatcher.STRICT);
MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(context.parseFieldMatcher(), queryBuilder.rewrite(), null);
Query query = null;
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (fieldType != null) {
query = fieldType.prefixQuery(queryBuilder.value(), method, context);
}
if (query == null) {
PrefixQuery prefixQuery = new PrefixQuery(new Term(queryBuilder.fieldName(), BytesRefs.toBytesRef(queryBuilder.value())));
if (method != null) {
prefixQuery.setRewriteMethod(method);
}
query = prefixQuery;
}
return query;
protected void doAssertLuceneQuery(PrefixQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(PrefixQuery.class));
PrefixQuery prefixQuery = (PrefixQuery) query;
assertThat(prefixQuery.getPrefix().field(), equalTo(queryBuilder.fieldName()));
}
@Test

View File

@ -21,28 +21,61 @@ 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;
@SuppressWarnings("deprecation")
public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder> {
@Override
protected Query doCreateExpectedQuery(QueryFilterBuilder queryBuilder, QueryParseContext context) throws QueryParsingException, IOException {
Query query = queryBuilder.innerQuery().toQuery(context);
return query != null ? new ConstantScoreQuery(query) : query;
}
/**
* @return a QueryFilterBuilder with random inner query
*/
@Override
protected QueryFilterBuilder doCreateTestQueryBuilder() {
QueryBuilder innerQuery = RandomQueryBuilder.createQuery(random());
return new QueryFilterBuilder(innerQuery);
}
@Override
protected void doAssertLuceneQuery(QueryFilterBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
Query innerQuery = queryBuilder.innerQuery().toQuery(context);
if (innerQuery == null) {
assertThat(query, nullValue());
} else {
assertThat(query, instanceOf(ConstantScoreQuery.class));
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
assertThat(constantScoreQuery.getQuery(), equalTo(innerQuery));
}
}
@Override
protected boolean supportsBoostAndQueryName() {
return false;
}
/**
* test wrapping an inner filter that returns null also returns <tt>null</null> to pass on upwards
*/
@Test
public void testInnerQueryReturnsNull() throws IOException {
QueryParseContext context = createContext();
// create inner filter
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
context.reset(parser);
assertQueryHeader(parser, ConstantScoreQueryBuilder.PROTOTYPE.getName());
QueryBuilder innerQuery = context.indexQueryParserService().queryParser(ConstantScoreQueryBuilder.PROTOTYPE.getName()).fromXContent(context);
// check that when wrapping this filter, toQuery() returns null
QueryFilterBuilder queryFilterQuery = new QueryFilterBuilder(innerQuery);
assertNull(queryFilterQuery.toQuery(createContext()));
}
@Test
public void testValidate() {
QueryBuilder innerQuery = null;

View File

@ -22,11 +22,6 @@ package org.elasticsearch.index.query;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.elasticsearch.common.joda.DateMathParser;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
@ -35,6 +30,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class RangeQueryBuilderTest extends BaseQueryTestCase<RangeQueryBuilder> {
@ -93,32 +89,16 @@ public class RangeQueryBuilderTest extends BaseQueryTestCase<RangeQueryBuilder>
}
@Override
protected Query doCreateExpectedQuery(RangeQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
Query expectedQuery;
String fieldName = queryBuilder.fieldName();
if (getCurrentTypes().length == 0 || (fieldName.equals(DATE_FIELD_NAME) == false && fieldName.equals(INT_FIELD_NAME) == false)) {
expectedQuery = new TermRangeQuery(fieldName, BytesRefs.toBytesRef(queryBuilder.from()),
BytesRefs.toBytesRef(queryBuilder.to()), queryBuilder.includeLower(), queryBuilder.includeUpper());
} else if (fieldName.equals(DATE_FIELD_NAME)) {
DateMathParser forcedDateParser = null;
if (queryBuilder.format() != null) {
forcedDateParser = new DateMathParser(Joda.forPattern(queryBuilder.format()));
}
DateTimeZone dateTimeZone = null;
if (queryBuilder.timeZone() != null) {
dateTimeZone = DateTimeZone.forID(queryBuilder.timeZone());
}
MappedFieldType mapper = context.fieldMapper(queryBuilder.fieldName());
expectedQuery = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(BytesRefs.toBytesRef(queryBuilder.from()), BytesRefs.toBytesRef(queryBuilder.to()),
queryBuilder.includeLower(), queryBuilder.includeUpper(), dateTimeZone, forcedDateParser);
protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false)) {
assertThat(query, instanceOf(TermRangeQuery.class));
} else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
//we can't properly test unmapped dates because LateParsingQuery is package private
} else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
expectedQuery = NumericRangeQuery.newIntRange(INT_FIELD_NAME, (Integer) queryBuilder.from(), (Integer) queryBuilder.to(),
queryBuilder.includeLower(), queryBuilder.includeUpper());
assertThat(query, instanceOf(NumericRangeQuery.class));
} else {
throw new UnsupportedOperationException();
}
return expectedQuery;
}
@Test

View File

@ -19,20 +19,15 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class RegexpQueryBuilderTest extends BaseQueryTestCase<RegexpQueryBuilder> {
@ -62,25 +57,8 @@ public class RegexpQueryBuilderTest extends BaseQueryTestCase<RegexpQueryBuilder
}
@Override
protected Query doCreateExpectedQuery(RegexpQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
//norelease fix to be removed to avoid NPE on unmapped fields
context.parseFieldMatcher(randomBoolean() ? ParseFieldMatcher.EMPTY : ParseFieldMatcher.STRICT);
MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(context.parseFieldMatcher(), queryBuilder.rewrite(), null);
Query query = null;
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (fieldType != null) {
query = fieldType.regexpQuery(queryBuilder.value(), queryBuilder.flags(), queryBuilder.maxDeterminizedStates(), method, context);
}
if (query == null) {
RegexpQuery regexpQuery = new RegexpQuery(new Term(queryBuilder.fieldName(), BytesRefs.toBytesRef(queryBuilder.value())),
queryBuilder.flags(), queryBuilder.maxDeterminizedStates());
if (method != null) {
regexpQuery.setRewriteMethod(method);
}
query = regexpQuery;
}
return query;
protected void doAssertLuceneQuery(RegexpQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(RegexpQuery.class));
}
@Test

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class ScriptQueryBuilderTest extends BaseQueryTestCase<ScriptQueryBuilder> {
@ -47,8 +48,8 @@ public class ScriptQueryBuilderTest extends BaseQueryTestCase<ScriptQueryBuilder
}
@Override
protected Query doCreateExpectedQuery(ScriptQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
return new ScriptQueryBuilder.ScriptQuery(queryBuilder.script(), context.scriptService(), context.lookup());
protected void doAssertLuceneQuery(ScriptQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(ScriptQueryBuilder.ScriptQuery.class));
}
@Test

View File

@ -19,13 +19,9 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.index.query.SimpleQueryParser.Settings;
import org.junit.Test;
import java.io.IOException;
@ -87,6 +83,11 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase<SimpleQueryS
return result;
}
@Override
protected void doAssertLuceneQuery(SimpleQueryStringBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, notNullValue());
}
@Test
public void testDefaults() {
SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder("The quick brown fox.");
@ -177,15 +178,6 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase<SimpleQueryS
assertThat(exception, notNullValue());
}
@Test
public void testHandlingDefaults() throws IOException {
SimpleQueryStringBuilder qb = createTestQueryBuilder();
qb.analyzer(null);
qb.minimumShouldMatch(null);
qb.queryName(null);
assertEquals(qb.toQuery(createContext()), createExpectedQuery(qb, createContext()));
}
@Test(expected = IllegalArgumentException.class)
public void testFieldCannotBeNull() {
SimpleQueryStringBuilder qb = createTestQueryBuilder();
@ -229,45 +221,5 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase<SimpleQueryS
}
return result;
}
@Override
protected Query doCreateExpectedQuery(SimpleQueryStringBuilder queryBuilder, QueryParseContext context) throws IOException {
Map<String, Float> fields = new TreeMap<>();
// Use the default field (_all) if no fields specified
if (queryBuilder.fields().isEmpty()) {
String field = context.defaultField();
fields.put(field, AbstractQueryBuilder.DEFAULT_BOOST);
} else {
fields.putAll(queryBuilder.fields());
}
// Use standard analyzer by default if none specified
Analyzer luceneAnalyzer;
if (queryBuilder.analyzer() == null) {
luceneAnalyzer = context.mapperService().searchAnalyzer();
} else {
luceneAnalyzer = context.analysisService().analyzer(queryBuilder.analyzer());
}
SimpleQueryParser sqp = new SimpleQueryParser(luceneAnalyzer, fields, queryBuilder.flags(), new Settings(queryBuilder.locale(),
queryBuilder.lowercaseExpandedTerms(), queryBuilder.lenient(), queryBuilder.analyzeWildcard()));
if (queryBuilder.defaultOperator() != null) {
switch (queryBuilder.defaultOperator()) {
case OR:
sqp.setDefaultOperator(Occur.SHOULD);
break;
case AND:
sqp.setDefaultOperator(Occur.MUST);
break;
}
}
Query query = sqp.parse(queryBuilder.text());
if (queryBuilder.minimumShouldMatch() != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, queryBuilder.minimumShouldMatch());
}
return query;
}
}

View File

@ -21,19 +21,13 @@ package org.elasticsearch.index.query;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanContainingQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.junit.Test;
import java.io.IOException;
public class SpanContainingQueryBuilderTest extends BaseQueryTestCase<SpanContainingQueryBuilder> {
import static org.hamcrest.CoreMatchers.instanceOf;
@Override
protected Query doCreateExpectedQuery(SpanContainingQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
SpanQuery big = (SpanQuery) testQueryBuilder.big().toQuery(context);
SpanQuery little = (SpanQuery) testQueryBuilder.little().toQuery(context);
return new SpanContainingQuery(big, little);
}
public class SpanContainingQueryBuilderTest extends BaseQueryTestCase<SpanContainingQueryBuilder> {
@Override
protected SpanContainingQueryBuilder doCreateTestQueryBuilder() {
@ -41,6 +35,11 @@ public class SpanContainingQueryBuilderTest extends BaseQueryTestCase<SpanContai
return new SpanContainingQueryBuilder(spanTermQueries[0], spanTermQueries[1]);
}
@Override
protected void doAssertLuceneQuery(SpanContainingQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanContainingQuery.class));
}
@Test
public void testValidate() {
int totalExpectedErrors = 0;

View File

@ -25,26 +25,27 @@ import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.io.IOException;
import static org.elasticsearch.index.query.QueryBuilders.spanTermQuery;
import static org.hamcrest.CoreMatchers.*;
public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(SpanFirstQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
SpanQuery innerQuery = (SpanQuery) testQueryBuilder.matchBuilder().toQuery(context);
return new SpanFirstQuery(innerQuery, testQueryBuilder.end());
}
@Override
protected SpanFirstQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(1);
return new SpanFirstQueryBuilder(spanTermQueries[0], randomIntBetween(0, 1000));
}
@Override
protected void doAssertLuceneQuery(SpanFirstQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanFirstQuery.class));
}
@Test
public void testValidate() {
int totalExpectedErrors = 0;

View File

@ -26,13 +26,10 @@ import org.junit.Test;
import java.io.IOException;
public class SpanMultiTermQueryBuilderTest extends BaseQueryTestCase<SpanMultiTermQueryBuilder> {
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
@Override
protected Query doCreateExpectedQuery(SpanMultiTermQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
Query multiTermQuery = testQueryBuilder.multiTermQueryBuilder().toQuery(context);
return new SpanMultiTermQueryWrapper<>((MultiTermQuery) multiTermQuery);
}
public class SpanMultiTermQueryBuilderTest extends BaseQueryTestCase<SpanMultiTermQueryBuilder> {
@Override
protected SpanMultiTermQueryBuilder doCreateTestQueryBuilder() {
@ -40,6 +37,15 @@ public class SpanMultiTermQueryBuilderTest extends BaseQueryTestCase<SpanMultiTe
return new SpanMultiTermQueryBuilder(multiTermQueryBuilder);
}
@Override
protected void doAssertLuceneQuery(SpanMultiTermQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanMultiTermQueryWrapper.class));
SpanMultiTermQueryWrapper spanMultiTermQueryWrapper = (SpanMultiTermQueryWrapper) query;
Query multiTermQuery = queryBuilder.multiTermQueryBuilder().toQuery(context);
assertThat(multiTermQuery, instanceOf(MultiTermQuery.class));
assertThat(spanMultiTermQueryWrapper.getWrappedQuery(), equalTo(new SpanMultiTermQueryWrapper<>((MultiTermQuery)multiTermQuery).getWrappedQuery()));
}
@Test
public void testValidate() {
int totalExpectedErrors = 0;

View File

@ -25,23 +25,13 @@ import org.apache.lucene.search.spans.SpanQuery;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class SpanNearQueryBuilderTest extends BaseQueryTestCase<SpanNearQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(SpanNearQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
List<SpanQueryBuilder> clauses = testQueryBuilder.clauses();
SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
for (int i = 0; i < clauses.size(); i++) {
Query query = clauses.get(i).toQuery(context);
assert query instanceof SpanQuery;
spanQueries[i] = (SpanQuery) query;
}
return new SpanNearQuery(spanQueries, testQueryBuilder.slop(), testQueryBuilder.inOrder(), testQueryBuilder.collectPayloads());
}
@Override
protected SpanNearQueryBuilder doCreateTestQueryBuilder() {
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(randomIntBetween(-10, 10));
@ -54,6 +44,19 @@ public class SpanNearQueryBuilderTest extends BaseQueryTestCase<SpanNearQueryBui
return queryBuilder;
}
@Override
protected void doAssertLuceneQuery(SpanNearQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanNearQuery.class));
SpanNearQuery spanNearQuery = (SpanNearQuery) query;
assertThat(spanNearQuery.getSlop(), equalTo(queryBuilder.slop()));
assertThat(spanNearQuery.isInOrder(), equalTo(queryBuilder.inOrder()));
assertThat(spanNearQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
for (SpanQuery spanQuery : spanNearQuery.getClauses()) {
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context)));
}
}
@Test
public void testValidate() {
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(1);

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.query;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanNotQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
@ -33,16 +32,10 @@ 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;
public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(SpanNotQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
SpanQuery include = (SpanQuery) testQueryBuilder.include().toQuery(context);
SpanQuery exclude = (SpanQuery) testQueryBuilder.exclude().toQuery(context);
return new SpanNotQuery(include, exclude, testQueryBuilder.pre(), testQueryBuilder.post());
}
@Override
protected SpanNotQueryBuilder doCreateTestQueryBuilder() {
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTest().createSpanTermQueryBuilders(2);
@ -61,6 +54,14 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
return queryBuilder;
}
@Override
protected void doAssertLuceneQuery(SpanNotQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanNotQuery.class));
SpanNotQuery spanNotQuery = (SpanNotQuery) query;
assertThat(spanNotQuery.getExclude(), equalTo(queryBuilder.exclude().toQuery(context)));
assertThat(spanNotQuery.getInclude(), equalTo(queryBuilder.include().toQuery(context)));
}
@Test
public void testValidate() {
int totalExpectedErrors = 0;

View File

@ -25,22 +25,13 @@ import org.apache.lucene.search.spans.SpanQuery;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class SpanOrQueryBuilderTest extends BaseQueryTestCase<SpanOrQueryBuilder> {
@Override
protected Query doCreateExpectedQuery(SpanOrQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
List<SpanQueryBuilder> clauses = testQueryBuilder.clauses();
SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
for (int i = 0; i < clauses.size(); i++) {
Query query = clauses.get(i).toQuery(context);
assert query instanceof SpanQuery;
spanQueries[i] = (SpanQuery) query;
}
return new SpanOrQuery(spanQueries);
}
@Override
protected SpanOrQueryBuilder doCreateTestQueryBuilder() {
SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder();
@ -51,6 +42,17 @@ public class SpanOrQueryBuilderTest extends BaseQueryTestCase<SpanOrQueryBuilder
return queryBuilder;
}
@Override
protected void doAssertLuceneQuery(SpanOrQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanOrQuery.class));
SpanOrQuery spanOrQuery = (SpanOrQuery) query;
assertThat(spanOrQuery.getClauses().length, equalTo(queryBuilder.clauses().size()));
Iterator<SpanQueryBuilder> spanQueryBuilderIterator = queryBuilder.clauses().iterator();
for (SpanQuery spanQuery : spanOrQuery.getClauses()) {
assertThat(spanQuery, equalTo(spanQueryBuilderIterator.next().toQuery(context)));
}
}
@Test
public void testValidate() {
SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder();

View File

@ -19,9 +19,16 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class SpanTermQueryBuilderTest extends BaseTermQueryTestCase<SpanTermQueryBuilder> {
@ -31,8 +38,17 @@ public class SpanTermQueryBuilderTest extends BaseTermQueryTestCase<SpanTermQuer
}
@Override
protected Query createLuceneTermQuery(Term term) {
return new SpanTermQuery(term);
protected void doAssertLuceneQuery(SpanTermQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanTermQuery.class));
SpanTermQuery spanTermQuery = (SpanTermQuery) query;
assertThat(spanTermQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
MappedFieldType mapper = context.fieldMapper(queryBuilder.fieldName());
if (mapper != null) {
BytesRef bytesRef = mapper.indexedValueForSearch(queryBuilder.value());
assertThat(spanTermQuery.getTerm().bytes(), equalTo(bytesRef));
} else {
assertThat(spanTermQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
}
}
/**

View File

@ -20,20 +20,14 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanWithinQuery;
import org.junit.Test;
import java.io.IOException;
public class SpanWithinQueryBuilderTest extends BaseQueryTestCase<SpanWithinQueryBuilder> {
import static org.hamcrest.CoreMatchers.instanceOf;
@Override
protected Query doCreateExpectedQuery(SpanWithinQueryBuilder testQueryBuilder, QueryParseContext context) throws IOException {
SpanQuery big = (SpanQuery) testQueryBuilder.big().toQuery(context);
SpanQuery little = (SpanQuery) testQueryBuilder.little().toQuery(context);
return new SpanWithinQuery(big, little);
}
public class SpanWithinQueryBuilderTest extends BaseQueryTestCase<SpanWithinQueryBuilder> {
@Override
protected SpanWithinQueryBuilder doCreateTestQueryBuilder() {
@ -41,6 +35,11 @@ public class SpanWithinQueryBuilderTest extends BaseQueryTestCase<SpanWithinQuer
return new SpanWithinQueryBuilder(spanTermQueries[0], spanTermQueries[1]);
}
@Override
protected void doAssertLuceneQuery(SpanWithinQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(SpanWithinQuery.class));
}
@Test
public void testValidate() {
int totalExpectedErrors = 0;

View File

@ -19,9 +19,16 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
public class TermQueryBuilderTest extends BaseTermQueryTestCase<TermQueryBuilder> {
@ -34,7 +41,16 @@ public class TermQueryBuilderTest extends BaseTermQueryTestCase<TermQueryBuilder
}
@Override
protected Query createLuceneTermQuery(Term term) {
return new TermQuery(term);
protected void doAssertLuceneQuery(TermQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(TermQuery.class));
TermQuery termQuery = (TermQuery) query;
assertThat(termQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
MappedFieldType mapper = context.fieldMapper(queryBuilder.fieldName());
if (mapper != null) {
BytesRef bytesRef = mapper.indexedValueForSearch(queryBuilder.value());
assertThat(termQuery.getTerm().bytes(), equalTo(bytesRef));
} else {
assertThat(termQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
}
}
}

View File

@ -19,16 +19,15 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
public class TypeQueryBuilderTest extends BaseQueryTestCase<TypeQueryBuilder> {
@ -38,16 +37,15 @@ public class TypeQueryBuilderTest extends BaseQueryTestCase<TypeQueryBuilder> {
}
@Override
protected Query doCreateExpectedQuery(TypeQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
Query expectedQuery;
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
DocumentMapper documentMapper = context.mapperService().documentMapper(queryBuilder.type().utf8ToString());
if (documentMapper == null) {
expectedQuery = new TermQuery(new Term(TypeFieldMapper.NAME, queryBuilder.type()));
} else {
expectedQuery = documentMapper.typeFilter();
protected void doAssertLuceneQuery(TypeQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, either(instanceOf(TermQuery.class)).or(instanceOf(ConstantScoreQuery.class)));
if (query instanceof ConstantScoreQuery) {
query = ((ConstantScoreQuery) query).getQuery();
assertThat(query, instanceOf(TermQuery.class));
}
return expectedQuery;
TermQuery termQuery = (TermQuery) query;
assertThat(termQuery.getTerm().field(), equalTo(TypeFieldMapper.NAME));
assertThat(termQuery.getTerm().bytes(), equalTo(queryBuilder.type()));
}
@Test

View File

@ -19,18 +19,13 @@
package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class WildcardQueryBuilderTest extends BaseQueryTestCase<WildcardQueryBuilder> {
@ -53,26 +48,8 @@ public class WildcardQueryBuilderTest extends BaseQueryTestCase<WildcardQueryBui
}
@Override
protected Query doCreateExpectedQuery(WildcardQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
String indexFieldName;
BytesRef valueBytes;
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (fieldType != null) {
indexFieldName = fieldType.names().indexName();
valueBytes = fieldType.indexedValueForSearch(queryBuilder.value());
} else {
indexFieldName = queryBuilder.fieldName();
valueBytes = new BytesRef(queryBuilder.value());
}
WildcardQuery expectedQuery = new WildcardQuery(new Term(indexFieldName, valueBytes));
//norelease fix to be removed to avoid NPE on unmapped fields
context.parseFieldMatcher(randomBoolean() ? ParseFieldMatcher.EMPTY : ParseFieldMatcher.STRICT);
MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(context.parseFieldMatcher(), queryBuilder.rewrite(), null);
QueryParsers.setRewriteMethod(expectedQuery, rewriteMethod);
return expectedQuery;
protected void doAssertLuceneQuery(WildcardQueryBuilder queryBuilder, Query query, QueryParseContext context) throws IOException {
assertThat(query, instanceOf(WildcardQuery.class));
}
@Test