Skip to content

Commit

Permalink
SimpleQ.S.B and QueryStringQ.S.B tests should avoid now in query (#…
Browse files Browse the repository at this point in the history
…43199)

Currently the randomization of the q.b. in these tests can create query strings
that can cause caching to be disabled for this query if we query all fields and
there is a date field present. This is pretty much an anomaly that we shouldn't
generally test for in the "testToQuery" tests where cache policies are checked.

This change makes sure we don't create offending query strings so the cache
checks never hit these cases and adds a special test method to check this edge
case.

Backport of #42199
Closes #54614
  • Loading branch information
Christoph Büscher committed Apr 2, 2020
1 parent 5293151 commit acb4224
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 89 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.elasticsearch.index.search.MatchQuery.Type;
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.hamcrest.Matcher;

import java.io.IOException;
Expand All @@ -63,19 +64,13 @@
import java.util.Locale;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

public class MatchQueryBuilderTests extends FullTextQueryTestCase<MatchQueryBuilder> {
@Override
protected boolean isCacheable(MatchQueryBuilder queryBuilder) {
return queryBuilder.fuzziness() != null
|| isCacheable(singletonList(queryBuilder.fieldName()), queryBuilder.value().toString());
}
public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuilder> {

@Override
protected MatchQueryBuilder doCreateTestQueryBuilder() {
Expand Down Expand Up @@ -512,4 +507,22 @@ private static CannedBinaryTokenStream.BinaryToken[] createGiantGraphMultiTerms(
}
return tokens.toArray(new CannedBinaryTokenStream.BinaryToken[0]);
}

/**
* "now" on date fields should make the query non-cachable.
*/
public void testCachingStrategiesWithNow() throws IOException {
// if we hit a date field with "now", this should diable cachability
MatchQueryBuilder queryBuilder = new MatchQueryBuilder(DATE_FIELD_NAME, "now");
QueryShardContext context = createShardContext();
assert context.isCacheable();
/*
* We use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not. We do it
* this way in SearchService where we first rewrite the query with a private context, then reset the context and then build the
* actual lucene query
*/
QueryBuilder rewritten = rewriteQuery(queryBuilder, new QueryShardContext(context));
assertNotNull(rewritten.toQuery(context));
assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.elasticsearch.index.query.MultiMatchQueryBuilder.Type;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -59,16 +60,10 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;

public class MultiMatchQueryBuilderTests extends FullTextQueryTestCase<MultiMatchQueryBuilder> {
public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatchQueryBuilder> {
private static final String MISSING_WILDCARD_FIELD_NAME = "missing_*";
private static final String MISSING_FIELD_NAME = "missing";

@Override
protected boolean isCacheable(MultiMatchQueryBuilder queryBuilder) {
return queryBuilder.fuzziness() != null
|| isCacheable(queryBuilder.fields().keySet(), queryBuilder.value().toString());
}

@Override
protected MultiMatchQueryBuilder doCreateTestQueryBuilder() {
String fieldName = randomFrom(STRING_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME,
Expand Down Expand Up @@ -515,4 +510,22 @@ private static IndexMetaData newIndexMeta(String name, Settings oldIndexSettings
.build();
return IndexMetaData.builder(name).settings(build).build();
}

/**
* "now" on date fields should make the query non-cachable.
*/
public void testCachingStrategiesWithNow() throws IOException {
// if we hit a date field with "now", this should diable cachability
MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder("now", DATE_FIELD_NAME);
QueryShardContext context = createShardContext();
assert context.isCacheable();
/*
* We use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not. We do it
* this way in SearchService where we first rewrite the query with a private context, then reset the context and then build the
* actual lucene query
*/
QueryBuilder rewritten = rewriteQuery(queryBuilder, new QueryShardContext(context));
assertNotNull(rewritten.toQuery(context));
assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.search.QueryStringQueryParser;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.hamcrest.Matchers;
import org.joda.time.DateTimeZone;

Expand All @@ -66,6 +67,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder;
Expand All @@ -76,20 +78,18 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

public class QueryStringQueryBuilderTests extends FullTextQueryTestCase<QueryStringQueryBuilder> {
@Override
protected boolean isCacheable(QueryStringQueryBuilder queryBuilder) {
return queryBuilder.fuzziness() != null
|| isCacheable(queryBuilder.fields().keySet(), queryBuilder.queryString());
}
public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStringQueryBuilder> {

@Override
protected QueryStringQueryBuilder doCreateTestQueryBuilder() {
int numTerms = randomIntBetween(0, 5);
String query = "";
for (int i = 0; i < numTerms; i++) {
//min length 4 makes sure that the text is not an operator (AND/OR) so toQuery won't break
query += (randomBoolean() ? STRING_FIELD_NAME + ":" : "") + randomAlphaOfLengthBetween(4, 10) + " ";
// min length 4 makes sure that the text is not an operator (AND/OR) so toQuery won't break
// also avoid "now" since we might hit dqte fields later and this complicates caching checks
String term = randomValueOtherThanMany(s -> s.toLowerCase(Locale.ROOT).contains("now"),
() -> randomAlphaOfLengthBetween(4, 10));
query += (randomBoolean() ? STRING_FIELD_NAME + ":" : "") + term + " ";
}
QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(query);
if (randomBoolean()) {
Expand Down Expand Up @@ -1543,4 +1543,39 @@ private static IndexMetaData newIndexMeta(String name, Settings oldIndexSettings
.build();
return IndexMetaData.builder(name).settings(build).build();
}

/**
* Query terms that contain "now" can trigger a query to not be cacheable.
* This test checks the search context cacheable flag is updated accordingly.
*/
public void testCachingStrategiesWithNow() throws IOException {
// if we hit all fields, this should contain a date field and should diable cachability
String query = "now " + randomAlphaOfLengthBetween(4, 10);
QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(query);
assertQueryCachability(queryStringQueryBuilder, false);

// if we hit a date field with "now", this should diable cachability
queryStringQueryBuilder = new QueryStringQueryBuilder("now");
queryStringQueryBuilder.field(DATE_FIELD_NAME);
assertQueryCachability(queryStringQueryBuilder, false);

// everything else is fine on all fields
query = randomFrom("NoW", "nOw", "NOW") + " " + randomAlphaOfLengthBetween(4, 10);
queryStringQueryBuilder = new QueryStringQueryBuilder(query);
assertQueryCachability(queryStringQueryBuilder, true);
}

private void assertQueryCachability(QueryStringQueryBuilder qb, boolean cachingExpected) throws IOException {
QueryShardContext context = createShardContext();
assert context.isCacheable();
/*
* We use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not. We do it
* this way in SearchService where we first rewrite the query with a private context, then reset the context and then build the
* actual lucene query
*/
QueryBuilder rewritten = rewriteQuery(qb, new QueryShardContext(context));
assertNotNull(rewritten.toQuery(context));
assertEquals("query should " + (cachingExpected ? "" : "not") + " be cacheable: " + qb.toString(), cachingExpected,
context.isCacheable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.search.SimpleQueryStringQueryParser;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
Expand All @@ -69,15 +70,13 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class SimpleQueryStringBuilderTests extends FullTextQueryTestCase<SimpleQueryStringBuilder> {
@Override
protected boolean isCacheable(SimpleQueryStringBuilder queryBuilder) {
return isCacheable(queryBuilder.fields().keySet(), queryBuilder.value());
}
public class SimpleQueryStringBuilderTests extends AbstractQueryTestCase<SimpleQueryStringBuilder> {

@Override
protected SimpleQueryStringBuilder doCreateTestQueryBuilder() {
String queryText = randomAlphaOfLengthBetween(1, 10);
// we avoid strings with "now" since those can have different caching policies that are checked elsewhere
String queryText = randomValueOtherThanMany(s -> s.toLowerCase(Locale.ROOT).contains("now"),
() -> randomAlphaOfLengthBetween(1, 10));
SimpleQueryStringBuilder result = new SimpleQueryStringBuilder(queryText);
if (randomBoolean()) {
result.analyzeWildcard(randomBoolean());
Expand Down Expand Up @@ -795,4 +794,39 @@ public void testReadFrom_5_6() throws IOException {
assertEquals("query", query.value());
}
}

/**
* Query terms that contain "now" can trigger a query to not be cacheable.
* This test checks the search context cacheable flag is updated accordingly.
*/
public void testCachingStrategiesWithNow() throws IOException {
// if we hit all fields, this should contain a date field and should diable cachability
String query = "now " + randomAlphaOfLengthBetween(4, 10);
SimpleQueryStringBuilder queryBuilder = new SimpleQueryStringBuilder(query);
assertQueryCachability(queryBuilder, false);

// if we hit a date field with "now", this should diable cachability
queryBuilder = new SimpleQueryStringBuilder("now");
queryBuilder.field(DATE_FIELD_NAME);
assertQueryCachability(queryBuilder, false);

// everything else is fine on all fields
query = randomFrom("NoW", "nOw", "NOW") + " " + randomAlphaOfLengthBetween(4, 10);
queryBuilder = new SimpleQueryStringBuilder(query);
assertQueryCachability(queryBuilder, true);
}

private void assertQueryCachability(SimpleQueryStringBuilder qb, boolean cachingExpected) throws IOException {
QueryShardContext context = createShardContext();
assert context.isCacheable();
/*
* We use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not. We do it
* this way in SearchService where we first rewrite the query with a private context, then reset the context and then build the
* actual lucene query
*/
QueryBuilder rewritten = rewriteQuery(qb, new QueryShardContext(context));
assertNotNull(rewritten.toQuery(context));
assertEquals("query should " + (cachingExpected ? "" : "not") + " be cacheable: " + qb.toString(), cachingExpected,
context.isCacheable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.test;

import com.fasterxml.jackson.core.io.JsonStringEncoder;

import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -485,7 +486,7 @@ public void testToQuery() throws IOException {
}
}

private QueryBuilder rewriteQuery(QB queryBuilder, QueryRewriteContext rewriteContext) throws IOException {
protected QueryBuilder rewriteQuery(QB queryBuilder, QueryRewriteContext rewriteContext) throws IOException {
QueryBuilder rewritten = rewriteAndFetch(queryBuilder, rewriteContext);
// extra safety to fail fast - serialize the rewritten version to ensure it's serializable.
assertSerialization(rewritten);
Expand Down

0 comments on commit acb4224

Please sign in to comment.