Skip to content

Commit 633e00a

Browse files
authored
Search fix: query_string regex/wildcard searches not working on wildcard fields (#60959) (#61013)
The Query string parser was not delegating the construction of wildcard/regex queries to the underlying field type. The wildcard field has special data structures and queries that operate on them so cannot rely on the basic regex/wildcard queries that were being used for other fields. Closes #60957
1 parent 82a90de commit 633e00a

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.lucene.search.spans.SpanOrQuery;
4444
import org.apache.lucene.search.spans.SpanQuery;
4545
import org.apache.lucene.util.BytesRef;
46+
import org.apache.lucene.util.automaton.RegExp;
4647
import org.elasticsearch.common.lucene.search.Queries;
4748
import org.elasticsearch.common.regex.Regex;
4849
import org.elasticsearch.common.unit.Fuzziness;
@@ -669,15 +670,19 @@ private Query getWildcardQuerySingle(String field, String termStr) throws ParseE
669670
// effectively, we check if a field exists or not
670671
return existsQuery(field);
671672
}
672-
String indexedNameField = field;
673673
Analyzer oldAnalyzer = getAnalyzer();
674674
try {
675675
MappedFieldType currentFieldType = queryBuilder.context.fieldMapper(field);
676-
if (currentFieldType != null) {
677-
setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer);
678-
indexedNameField = currentFieldType.name();
676+
if (currentFieldType == null) {
677+
return newUnmappedFieldQuery(field);
678+
}
679+
if (forceAnalyzer != null &&
680+
(analyzeWildcard || currentFieldType.getTextSearchInfo().isTokenized())) {
681+
setAnalyzer(forceAnalyzer);
682+
return super.getWildcardQuery(currentFieldType.name(), termStr);
679683
}
680-
return super.getWildcardQuery(indexedNameField, termStr);
684+
685+
return currentFieldType.wildcardQuery(termStr, getMultiTermRewriteMethod(), context);
681686
} catch (RuntimeException e) {
682687
if (lenient) {
683688
return newLenientFieldQuery(field, e);
@@ -722,9 +727,12 @@ private Query getRegexpQuerySingle(String field, String termStr) throws ParseExc
722727
if (currentFieldType == null) {
723728
return newUnmappedFieldQuery(field);
724729
}
725-
setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer);
726-
Query query = super.getRegexpQuery(field, termStr);
727-
return query;
730+
if (forceAnalyzer != null) {
731+
setAnalyzer(forceAnalyzer);
732+
return super.getRegexpQuery(field, termStr);
733+
}
734+
return currentFieldType.regexpQuery(termStr, RegExp.ALL, getMaxDeterminizedStates(),
735+
getMultiTermRewriteMethod(), context);
728736
} catch (RuntimeException e) {
729737
if (lenient) {
730738
return newLenientFieldQuery(field, e);

x-pack/plugin/src/test/resources/rest-api-spec/test/wildcard/10_wildcard_basic.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,46 @@ setup:
158158

159159
- match: {hits.total.value: 1}
160160

161+
---
162+
"Query_string prefix query":
163+
- do:
164+
search:
165+
body:
166+
track_total_hits: true
167+
query:
168+
query_string:
169+
query: "hello*"
170+
default_field: "my_wildcard"
171+
172+
173+
- match: {hits.total.value: 1}
174+
---
175+
"Query_string wildcard query":
176+
- do:
177+
search:
178+
body:
179+
track_total_hits: true
180+
query:
181+
query_string:
182+
query: "*orld"
183+
default_field: "my_wildcard"
184+
185+
186+
- match: {hits.total.value: 3}
187+
---
188+
"Query_string regex query":
189+
- do:
190+
search:
191+
body:
192+
track_total_hits: true
193+
query:
194+
query_string:
195+
query: "/hello.*/"
196+
default_field: "my_wildcard"
197+
198+
199+
- match: {hits.total.value: 1}
200+
161201
---
162202
"Term query on wildcard field":
163203
- do:

0 commit comments

Comments
 (0)