-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Mask wildcard query special characters on keyword queries #53127
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 8 commits
ec7ecf7
1e3fc34
8cf6a71
f0da91d
c4529d1
e727b91
a59ea3e
ddf828c
c555c43
db5ea54
4dfe63d
e466513
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 |
|---|---|---|
|
|
@@ -30,13 +30,12 @@ | |
| import org.apache.lucene.search.BooleanQuery; | ||
| import org.apache.lucene.search.ConstantScoreQuery; | ||
| import org.apache.lucene.search.MatchAllDocsQuery; | ||
| import org.apache.lucene.search.MatchNoDocsQuery; | ||
| import org.apache.lucene.search.Query; | ||
| import org.apache.lucene.search.TermInSetQuery; | ||
| import org.apache.lucene.search.TermQuery; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.common.lucene.Lucene; | ||
| import org.elasticsearch.common.lucene.search.Queries; | ||
| import org.elasticsearch.common.regex.Regex; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.index.IndexSettings; | ||
| import org.elasticsearch.index.fielddata.IndexFieldData; | ||
|
|
@@ -52,6 +51,7 @@ | |
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class TypeFieldMapper extends MetadataFieldMapper { | ||
|
|
||
|
|
@@ -90,7 +90,7 @@ public MetadataFieldMapper getDefault(ParserContext context) { | |
| } | ||
| } | ||
|
|
||
| public static final class TypeFieldType extends StringFieldType { | ||
| public static final class TypeFieldType extends ConstantFieldType { | ||
|
|
||
| TypeFieldType() { | ||
| } | ||
|
|
@@ -121,61 +121,12 @@ public ValuesSourceType getValuesSourceType() { | |
| } | ||
|
|
||
| @Override | ||
| public boolean isSearchable() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Query existsQuery(QueryShardContext context) { | ||
| return new MatchAllDocsQuery(); | ||
| } | ||
|
|
||
| @Override | ||
| public Query termQuery(Object value, QueryShardContext context) { | ||
| return termsQuery(Arrays.asList(value), context); | ||
| } | ||
|
|
||
| @Override | ||
| public Query termsQuery(List<?> values, QueryShardContext context) { | ||
| DocumentMapper mapper = context.getMapperService().documentMapper(); | ||
| if (mapper == null) { | ||
| return new MatchNoDocsQuery("No types"); | ||
| } | ||
| BytesRef indexType = indexedValueForSearch(mapper.type()); | ||
| if (values.stream() | ||
| .map(this::indexedValueForSearch) | ||
| .anyMatch(indexType::equals)) { | ||
| if (context.getMapperService().hasNested()) { | ||
| // type filters are expected not to match nested docs | ||
| return Queries.newNonNestedFilter(); | ||
| } else { | ||
| return new MatchAllDocsQuery(); | ||
| } | ||
| } else { | ||
| return new MatchNoDocsQuery("Type list does not contain the index type"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { | ||
| Query result = new MatchAllDocsQuery(); | ||
| String type = context.getMapperService().documentMapper().type(); | ||
| if (type != null) { | ||
| BytesRef typeBytes = new BytesRef(type); | ||
| if (lowerTerm != null) { | ||
| int comp = indexedValueForSearch(lowerTerm).compareTo(typeBytes); | ||
| if (comp > 0 || (comp == 0 && includeLower == false)) { | ||
| result = new MatchNoDocsQuery("[_type] was lexicographically smaller than lower bound of range"); | ||
| } | ||
| } | ||
| if (upperTerm != null) { | ||
| int comp = indexedValueForSearch(upperTerm).compareTo(typeBytes); | ||
| if (comp < 0 || (comp == 0 && includeUpper == false)) { | ||
| result = new MatchNoDocsQuery("[_type] was lexicographically greater than upper bound of range"); | ||
| } | ||
| } | ||
| protected boolean matches(String pattern, QueryShardContext context) { | ||
|
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. I don't think we need to handle wildcard and prefixes here ? We don't support prefix and wildcard queries on the |
||
| if (pattern.contains("?") == false) { | ||
| return Regex.simpleMatch(pattern, MapperService.SINGLE_MAPPING_NAME); | ||
cbuescher marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return result; | ||
| boolean matches = Pattern.matches(pattern.replace("?", "."), MapperService.SINGLE_MAPPING_NAME); | ||
| return matches; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.