-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
jpountz
merged 37 commits into
elastic:master
from
jpountz:enhancement/simplify_in_rewrite
Feb 26, 2020
Merged
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 c253ce7
iter
jpountz abf27f1
Rename to `constant_keyword`.
jpountz 002fe96
iter
jpountz c6738d8
iter
jpountz b7b894e
iter
jpountz f5f288e
Merge branch 'master' into feature/singleton_keyword_field
jpountz a86ad8a
iter
jpountz a5949d6
iter
jpountz c9c43aa
iter
jpountz 6b907c2
iter
jpountz 779d128
unused imports
jpountz 51dc118
add javadocs
jpountz 8da5b6c
iter
jpountz af4a55a
Merge branch 'master' into feature/singleton_keyword_field
jpountz d1b71f4
More docs.
jpountz f8ddbf7
Merge branch 'master' into feature/singleton_keyword_field
jpountz d346303
address review comments
jpountz c378cdf
Fix failures.
jpountz 59fd6b0
Merge branch 'master' into feature/singleton_keyword_field
jpountz 4592af2
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz ab70f10
iter
jpountz 0b29a43
iter
jpountz 936f883
iter
jpountz 44a7170
Undo renames.
jpountz 7639d34
iter
jpountz fba9ca3
iter
jpountz a284be9
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz f9a23e2
iter
jpountz b312407
iter
jpountz 437315d
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz 3f94930
iter
jpountz 7cd5c6d
iter
jpountz 51f7b3c
iter
jpountz dc0bf44
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz 15af22f
Review comments.
jpountz 2b00751
Merge branch 'master' into enhancement/simplify_in_rewrite
jpountz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.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 aMatchNoneQueryBuilder
. And when its inner query rewrites to aMatchNoneQueryBuilder
,ConstantScoreQueryBuilder
itself rewrites to aMatchNoneQueryBuilder
. So I updated the logic this way:But this fails if the inner query is wrapped in a
BoostQuery
. So I had to either changeAbstractQueryBuilder
to no longer wrapMatchNoDocsQuery
with a boost, or change this test (and a couple other ones IIRC) to check whether the inner query might be wrapped inside aBoostQuery
. I chose the former, which sounded simpler to me.There was a problem hiding this comment.
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!