Skip to content
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

Generalize how queries on _index are handled at rewrite time #52486

Merged
merged 37 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
db1a079
Introduce a `singleton_keyword` field.
jpountz Nov 29, 2019
c253ce7
iter
jpountz Nov 29, 2019
abf27f1
Rename to `constant_keyword`.
jpountz Nov 29, 2019
002fe96
iter
jpountz Nov 29, 2019
c6738d8
iter
jpountz Nov 29, 2019
b7b894e
iter
jpountz Nov 29, 2019
f5f288e
Merge branch 'master' into feature/singleton_keyword_field
jpountz Nov 29, 2019
a86ad8a
iter
jpountz Nov 29, 2019
a5949d6
iter
jpountz Dec 2, 2019
c9c43aa
iter
jpountz Dec 2, 2019
6b907c2
iter
jpountz Dec 2, 2019
779d128
unused imports
jpountz Dec 2, 2019
51dc118
add javadocs
jpountz Dec 2, 2019
8da5b6c
iter
jpountz Dec 2, 2019
af4a55a
Merge branch 'master' into feature/singleton_keyword_field
jpountz Dec 3, 2019
d1b71f4
More docs.
jpountz Dec 3, 2019
f8ddbf7
Merge branch 'master' into feature/singleton_keyword_field
jpountz Jan 8, 2020
d346303
address review comments
jpountz Jan 8, 2020
c378cdf
Fix failures.
jpountz Jan 10, 2020
59fd6b0
Merge branch 'master' into feature/singleton_keyword_field
jpountz Feb 14, 2020
4592af2
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz Feb 14, 2020
ab70f10
iter
jpountz Feb 14, 2020
0b29a43
iter
jpountz Feb 14, 2020
936f883
iter
jpountz Feb 18, 2020
44a7170
Undo renames.
jpountz Feb 18, 2020
7639d34
iter
jpountz Feb 18, 2020
fba9ca3
iter
jpountz Feb 18, 2020
a284be9
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz Feb 19, 2020
f9a23e2
iter
jpountz Feb 21, 2020
b312407
iter
jpountz Feb 21, 2020
437315d
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz Feb 21, 2020
3f94930
iter
jpountz Feb 21, 2020
7cd5c6d
iter
jpountz Feb 21, 2020
51f7b3c
iter
jpountz Feb 21, 2020
dc0bf44
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz Feb 25, 2020
15af22f
Review comments.
jpountz Feb 25, 2020
2b00751
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz Feb 26, 2020
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
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.analysis.DelegatingAnalyzerWrapper;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
Expand Down Expand Up @@ -74,6 +75,7 @@
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.index.query.Rewriteable;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;

Expand Down Expand Up @@ -561,9 +563,9 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
PercolatorFieldMapper.FieldType pft = (PercolatorFieldMapper.FieldType) fieldType;
String name = this.name != null ? this.name : pft.name();
QueryShardContext percolateShardContext = wrap(context);
PercolatorFieldMapper.configureContext(percolateShardContext, pft.mapUnmappedFieldsAsText);;
PercolateQuery.QueryStore queryStore = createStore(pft.queryBuilderField,
percolateShardContext,
pft.mapUnmappedFieldsAsText);
percolateShardContext);

return pft.percolateQuery(name, queryStore, documents, docSearcher, excludeNestedDocuments, context.indexVersionCreated());
}
Expand Down Expand Up @@ -606,8 +608,7 @@ static IndexSearcher createMultiDocumentSearcher(Analyzer analyzer, Collection<P
}

static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldType,
QueryShardContext context,
boolean mapUnmappedFieldsAsString) {
QueryShardContext context) {
Version indexVersion = context.indexVersionCreated();
NamedWriteableRegistry registry = context.getWriteableRegistry();
return ctx -> {
Expand All @@ -633,7 +634,8 @@ static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldTy
assert valueLength > 0;
QueryBuilder queryBuilder = input.readNamedWriteable(QueryBuilder.class);
assert in.read() == -1;
return PercolatorFieldMapper.toQuery(context, mapUnmappedFieldsAsString, queryBuilder);
queryBuilder = Rewriteable.rewrite(queryBuilder, context);
return queryBuilder.toQuery(context);
}
}
} else {
Expand All @@ -646,6 +648,13 @@ static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldTy
static QueryShardContext wrap(QueryShardContext shardContext) {
return new QueryShardContext(shardContext) {

@Override
public IndexReader getIndexReader() {
// The reader that matters in this context is not the reader of the shard but
// the reader of the MemoryIndex. We just use `null` for simplicity.
return null;
}

@Override
public BitSetProducer bitsetFilter(Query query) {
return context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ public void parse(ParseContext context) throws IOException {
throw new IllegalArgumentException("a document can only contain one percolator query");
}

configureContext(queryShardContext, isMapUnmappedFieldAsText());

XContentParser parser = context.parser();
QueryBuilder queryBuilder = parseQueryBuilder(
parser, parser.getTokenLocation()
Expand All @@ -408,13 +410,12 @@ public void parse(ParseContext context) throws IOException {
createQueryBuilderField(indexVersion, queryBuilderField, queryBuilder, context);

QueryBuilder queryBuilderForProcessing = queryBuilder.rewrite(new QueryShardContext(queryShardContext) {

@Override
public boolean convertNowRangeToMatchAll() {
return true;
}
});
Query query = toQuery(queryShardContext, isMapUnmappedFieldAsText(), queryBuilderForProcessing);
Query query = queryBuilderForProcessing.toQuery(queryShardContext);
processQuery(query, context);
}

Expand Down Expand Up @@ -472,11 +473,7 @@ void processQuery(Query query, ParseContext context) {
doc.add(new NumericDocValuesField(minimumShouldMatchFieldMapper.name(), result.minimumShouldMatch));
}

static Query parseQuery(QueryShardContext context, boolean mapUnmappedFieldsAsString, XContentParser parser) throws IOException {
return toQuery(context, mapUnmappedFieldsAsString, parseQueryBuilder(parser, parser.getTokenLocation()));
}

static Query toQuery(QueryShardContext context, boolean mapUnmappedFieldsAsString, QueryBuilder queryBuilder) throws IOException {
static void configureContext(QueryShardContext context, boolean mapUnmappedFieldsAsString) {
// This means that fields in the query need to exist in the mapping prior to registering this query
// The reason that this is required, is that if a field doesn't exist then the query assumes defaults, which may be undesired.
//
Expand All @@ -491,7 +488,6 @@ static Query toQuery(QueryShardContext context, boolean mapUnmappedFieldsAsStrin
// as an analyzed string.
context.setAllowUnmappedFields(false);
context.setMapUnmappedFieldAsString(mapUnmappedFieldsAsString);
return queryBuilder.toQuery(context);
}

private static QueryBuilder parseQueryBuilder(XContentParser parser, XContentLocation location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import org.elasticsearch.index.fielddata.plain.BytesBinaryDVIndexFieldData;
import org.elasticsearch.index.mapper.BinaryFieldMapper;
import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.mock.orig.Mockito;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -93,7 +95,14 @@ public void testStoringQueryBuilders() throws IOException {
when(queryShardContext.getXContentRegistry()).thenReturn(xContentRegistry());
when(queryShardContext.getForField(fieldMapper.fieldType()))
.thenReturn(new BytesBinaryDVIndexFieldData(new Index("index", "uuid"), fieldMapper.name()));
PercolateQuery.QueryStore queryStore = PercolateQueryBuilder.createStore(fieldMapper.fieldType(), queryShardContext, false);
when(queryShardContext.fieldMapper(Mockito.anyString())).thenAnswer(invocation -> {
final String fieldName = (String) invocation.getArguments()[0];
KeywordFieldMapper.KeywordFieldType ft = new KeywordFieldMapper.KeywordFieldType();
ft.setName(fieldName);
ft.freeze();
return ft;
});
PercolateQuery.QueryStore queryStore = PercolateQueryBuilder.createStore(fieldMapper.fieldType(), queryShardContext);

try (IndexReader indexReader = DirectoryReader.open(directory)) {
LeafReaderContext leafContext = indexReader.leaves().get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.mapper;

import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.index.query.QueryShardContext;

import java.util.List;

/**
* A {@link MappedFieldType} that has the same value for all documents.
* Factory methods for queries are called at rewrite time so they should be
* cheap. In particular they should not read data from disk or perform a
* network call. Furthermore they may only return a {@link MatchAllDocsQuery}
* or a {@link MatchNoDocsQuery}.
*/
public abstract class ConstantFieldType extends MappedFieldType {

public ConstantFieldType() {
super();
}

public ConstantFieldType(ConstantFieldType other) {
super(other);
}

@Override
public final boolean isSearchable() {
return true;
}

@Override
public final boolean isAggregatable() {
return true;
}

@Override
public final Query existsQuery(QueryShardContext context) {
return new MatchAllDocsQuery();
}

/**
* Return whether the constant value of this field matches the provided {@code pattern}
* as documented in {@link Regex#simpleMatch}.
*/
protected abstract boolean matches(String pattern, QueryShardContext context);

private static String valueToString(Object value) {
return value instanceof BytesRef
? ((BytesRef) value).utf8ToString()
: value.toString();
}

@Override
public final Query termQuery(Object value, QueryShardContext context) {
String pattern = valueToString(value);
if (matches(pattern, context)) {
return Queries.newMatchAllQuery();
} else {
return new MatchNoDocsQuery();
}
}

@Override
public final Query termsQuery(List<?> values, QueryShardContext context) {
for (Object value : values) {
String pattern = valueToString(value);
if (matches(pattern, context)) {
// `terms` queries are a disjunction, so one matching term is enough
return Queries.newMatchAllQuery();
}
}
return new MatchNoDocsQuery();
}

@Override
public final Query prefixQuery(String prefix,
@Nullable MultiTermQuery.RewriteMethod method,
QueryShardContext context) {
String pattern = prefix + "*";
if (matches(pattern, context)) {
return Queries.newMatchAllQuery();
} else {
return new MatchNoDocsQuery();
}
}

@Override
public final Query wildcardQuery(String value,
@Nullable MultiTermQuery.RewriteMethod method,
QueryShardContext context) {
if (matches(value, context)) {
return Queries.newMatchAllQuery();
} else {
return new MatchNoDocsQuery();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@

import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.fielddata.IndexFieldData;
Expand Down Expand Up @@ -91,7 +85,7 @@ public MetadataFieldMapper getDefault(ParserContext context) {
}
}

static final class IndexFieldType extends MappedFieldType {
static final class IndexFieldType extends ConstantFieldType {

IndexFieldType() {}

Expand All @@ -110,81 +104,8 @@ public String typeName() {
}

@Override
public boolean isSearchable() {
// The _index field is always searchable.
return true;
}

@Override
public Query existsQuery(QueryShardContext context) {
return new MatchAllDocsQuery();
}

/**
* This termQuery impl looks at the context to determine the index that
* is being queried and then returns a MATCH_ALL_QUERY or MATCH_NO_QUERY
* if the value matches this index. This can be useful if aliases or
* wildcards are used but the aim is to restrict the query to specific
* indices
*/
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
String pattern = value instanceof BytesRef
? ((BytesRef) value).utf8ToString()
: value.toString();
if (context.indexMatches(pattern)) {
// No need to OR these clauses - we can only logically be
// running in the context of just one of these index names.
return Queries.newMatchAllQuery();
} else {
return Queries.newMatchNoDocsQuery("The index [" + context.getFullyQualifiedIndex().getName() +
"] doesn't match the provided value [" + value + "].");
}
}

@Override
public Query termsQuery(List values, QueryShardContext context) {
if (context == null) {
return super.termsQuery(values, context);
}
for (Object value : values) {
String pattern = value instanceof BytesRef
? ((BytesRef) value).utf8ToString()
: value.toString();
if (context.indexMatches(pattern)) {
// No need to OR these clauses - we can only logically be
// running in the context of just one of these index names.
return Queries.newMatchAllQuery();
}
}
// None of the listed index names are this one
return Queries.newMatchNoDocsQuery("The index [" + context.getFullyQualifiedIndex().getName() +
"] doesn't match the provided values [" + values + "].");
}

@Override
public Query prefixQuery(String value,
@Nullable MultiTermQuery.RewriteMethod method,
QueryShardContext context) {
String pattern = value + "*";
if (context.indexMatches(pattern)) {
return Queries.newMatchAllQuery();
} else {
return Queries.newMatchNoDocsQuery("The index [" + context.getFullyQualifiedIndex().getName() +
"] doesn't match the provided prefix [" + value + "].");
}
}

@Override
public Query wildcardQuery(String value,
@Nullable MultiTermQuery.RewriteMethod method,
QueryShardContext context) {
if (context.indexMatches(value)) {
return Queries.newMatchAllQuery();
} else {
return Queries.newMatchNoDocsQuery("The index [" + context.getFullyQualifiedIndex().getName()
+ "] doesn't match the provided pattern [" + value + "].");
}
protected boolean matches(String pattern, QueryShardContext context) {
return context.indexMatches(pattern);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.query;

import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanBoostQuery;
import org.apache.lucene.search.spans.SpanQuery;
Expand Down Expand Up @@ -103,7 +104,7 @@ public final Query toQuery(QueryShardContext context) throws IOException {
if (boost != DEFAULT_BOOST) {
if (query instanceof SpanQuery) {
query = new SpanBoostQuery((SpanQuery) query, boost);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my knowledge, I'm wondering why this change (and the ones to AbstractQueryTestCase) were necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change as a way to keep tests simple. For instance here is what ConstantScoreQueryBuilderTests#doAssertLuceneQuery looks like in master today.

    @Override
    protected void doAssertLuceneQuery(ConstantScoreQueryBuilder queryBuilder, Query query, QueryShardContext 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(), instanceOf(innerQuery.getClass()));
        }
    }

This test only worked because most queries on unmapped fields would create the same query as on a keyword field. But with this change, queries on unmapped fields get rewritten as a MatchNoneQueryBuilder. And when its inner query rewrites to a MatchNoneQueryBuilder, ConstantScoreQueryBuilder itself rewrites to a MatchNoneQueryBuilder. So I updated the logic this way:

    @Override
    protected void doAssertLuceneQuery(ConstantScoreQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
        Query innerQuery = queryBuilder.innerQuery().rewrite(context).toQuery(context);
        if (innerQuery == null) {
            assertThat(query, nullValue());
        } else if (innerQuery instanceof MatchNoDocsQuery) {
            assertThat(query, instanceOf(MatchNoDocsQuery.class));
        } else {
            assertThat(query, instanceOf(ConstantScoreQuery.class));
            ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
            assertThat(constantScoreQuery.getQuery(), instanceOf(innerQuery.getClass()));
        }
    }

But this fails if the inner query is wrapped in a BoostQuery. So I had to either change AbstractQueryBuilder to no longer wrap MatchNoDocsQuery with a boost, or change this test (and a couple other ones IIRC) to check whether the inner query might be wrapped inside a BoostQuery. I chose the former, which sounded simpler to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear explanation!

} else if (query instanceof MatchNoDocsQuery == false) {
query = new BoostQuery(query, boost);
}
}
Expand Down Expand Up @@ -232,7 +233,7 @@ static Collection<Query> toQueries(Collection<QueryBuilder> queryBuilders, Query
IOException {
List<Query> queries = new ArrayList<>(queryBuilders.size());
for (QueryBuilder queryBuilder : queryBuilders) {
Query query = queryBuilder.toQuery(context);
Query query = queryBuilder.rewrite(context).toQuery(context);
if (query != null) {
queries.add(query);
}
Expand Down
Loading