-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Delegate wildcard query creation to MappedFieldType. #34062
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,6 @@ | |
| package org.elasticsearch.index.query; | ||
|
|
||
| import org.apache.lucene.index.Term; | ||
| 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.search.WildcardQuery; | ||
|
|
@@ -185,20 +183,20 @@ public static WildcardQueryBuilder fromXContent(XContentParser parser) throws IO | |
| @Override | ||
| protected Query doToQuery(QueryShardContext context) throws IOException { | ||
| MappedFieldType fieldType = context.fieldMapper(fieldName); | ||
| Term term; | ||
|
|
||
| Query query; | ||
| if (fieldType == null) { | ||
| term = new Term(fieldName, BytesRefs.toBytesRef(value)); | ||
| Term term = new Term(fieldName, BytesRefs.toBytesRef(value)); | ||
| query = new WildcardQuery(term); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if the field does not exist we could return a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one thing that has confused me in the past: if a field type doesn't exist, we typically still create a query of the same form (see In any case, maybe I could make this change in a follow-up PR, as I was just hoping for a straight refactor here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes we don't have a clear policy for this. The reason I prefer the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } else { | ||
| Query termQuery = fieldType.termQuery(value, context); | ||
| if (termQuery instanceof MatchNoDocsQuery || termQuery instanceof MatchAllDocsQuery) { | ||
| return termQuery; | ||
| } | ||
| term = MappedFieldType.extractTerm(termQuery); | ||
| query = fieldType.wildcardQuery(value, context); | ||
| } | ||
|
|
||
| WildcardQuery query = new WildcardQuery(term); | ||
| MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE); | ||
| QueryParsers.setRewriteMethod(query, rewriteMethod); | ||
| if (query instanceof MultiTermQuery) { | ||
| MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod( | ||
| rewrite, null, LoggingDeprecationHandler.INSTANCE); | ||
| QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod); | ||
| } | ||
| return query; | ||
| } | ||
|
|
||
|
|
||
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 think the same reasoning can apply for
wildcard,prefixandregexqueries so the default impl should throw aQueryShardException? OnlyStringFieldTypefields should be able to build awildcardquery.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.
The
keywordfield applies the normalizer ontermQuery. Depending on the normalizer the wildcard and escaped characters could be removed/replaced so I wonder if we should apply the same logic thanQueryParserBase#analyzeWildcardforkeywordfields. This is out of scope for this pr but it made me realize that we might have a bug here.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 missed this part sorry. I think we should explicitly add the support in the
_indexfield type rather than supporting this query on all fields. Currently the support forprefixqueries is also broken so we don't really use this ability.Uh oh!
There was an error while loading. Please reload this page.
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.
Okay, I'll make sure only string fields support wildcards by default. Maybe I'll add an upgrade note too in case this breaks some types we don't have test coverage for (will make it easier for users to debug + file issues)?
Makes sense, I'll make a note to follow-up on this.
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.
Looking through the non-string field types, what do you think should be done with metadata types like
IdFieldType,IgnoredFieldType, andRoutingFieldType? My intuition is we should switch them to being string fields to avoid breaking any queries.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.
This change would only break
wildcardquery on these fields, right ? +1 to make them string fields,prefixandregexquery do not work currently because of this so it would be a bug fix. I am also ok to do that in a follow up, the changes in this pr have a different scope.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.
👍