Skip to content

Fix full text queries test that start with now #41854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.index.query;

import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.AbstractQueryTestCase;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public abstract class FullTextQueryTestCase<QB extends AbstractQueryBuilder<QB>> extends AbstractQueryTestCase<QB> {
protected abstract boolean isCacheable(QB queryBuilder);

/**
* Full text queries that start with "now" are not cacheable if they
* target a {@link DateFieldMapper.DateFieldType} field.
*/
protected final boolean isCacheable(Collection<String> fields, String value) {
if (value.length() < 3
|| value.substring(0, 3).equalsIgnoreCase("now") == false) {
return true;
}
Set<String> dateFields = new HashSet<>();
getMapping().forEach(ft -> {
if (ft instanceof DateFieldMapper.DateFieldType) {
dateFields.add(ft.name());
}
});
for (MappedFieldType ft : getMapping()) {
if (ft instanceof DateFieldMapper.DateFieldType) {
dateFields.add(ft.name());
}
}
if (fields.isEmpty()) {
// special case: all fields are requested
return dateFields.isEmpty();
}
return fields.stream()
.anyMatch(dateFields::contains) == false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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 @@ -56,13 +55,19 @@
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 AbstractQueryTestCase<MatchQueryBuilder> {
public class MatchQueryBuilderTests extends FullTextQueryTestCase<MatchQueryBuilder> {
@Override
protected boolean isCacheable(MatchQueryBuilder queryBuilder) {
return queryBuilder.fuzziness() != null
|| isCacheable(singletonList(queryBuilder.fieldName()), queryBuilder.value().toString());
}

@Override
protected MatchQueryBuilder doCreateTestQueryBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
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,11 +58,16 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;

public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatchQueryBuilder> {

public class MultiMatchQueryBuilderTests extends FullTextQueryTestCase<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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
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.CoreMatchers;
import org.hamcrest.Matchers;

Expand All @@ -84,7 +83,12 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

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

@Override
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.search.SimpleQueryStringQueryParser;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -65,7 +64,11 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

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

@Override
protected SimpleQueryStringBuilder doCreateTestQueryBuilder() {
Expand Down Expand Up @@ -107,11 +110,6 @@ protected SimpleQueryStringBuilder doCreateTestQueryBuilder() {
fields.put(STRING_FIELD_NAME_2, 2.0f / randomIntBetween(1, 20));
}
}
// special handling if query start with "now" and no field specified. This hits the "mapped_date" field which leads to the query not
// being cacheable and trigger later test failures (see https://github.com/elastic/elasticsearch/issues/35183)
if (fieldCount == 0 && queryText.length() >= 3 && queryText.substring(0,3).equalsIgnoreCase("now")) {
fields.put(STRING_FIELD_NAME_2, 2.0f / randomIntBetween(1, 20));
}

result.fields(fields);
if (randomBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ protected static String expectedFieldName(String builderFieldName) {
return ALIAS_TO_CONCRETE_FIELD_NAME.getOrDefault(builderFieldName, builderFieldName);
}

protected Iterable<MappedFieldType> getMapping() {
return serviceHolder.mapperService.fieldTypes();
}

@AfterClass
public static void afterClass() throws Exception {
IOUtils.close(serviceHolder);
Expand Down