Skip to content

Commit 50a0604

Browse files
committed
fix regex query to work with field alias
Signed-off-by: bowenlan-amzn <[email protected]>
1 parent 70e890a commit 50a0604

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"field alias with regex pattern search":
2+
- do:
3+
indices.create:
4+
index: test_index
5+
body:
6+
settings:
7+
number_of_shards: 1
8+
number_of_replicas: 0
9+
mappings:
10+
properties:
11+
test:
12+
type: text
13+
test_alias:
14+
type: alias
15+
path: test
16+
17+
- do:
18+
bulk:
19+
refresh: true
20+
body: |
21+
{"index":{"_index":"test_index","_id":"1"}}
22+
{"test":"hello"}
23+
{"index":{"_index":"test_index","_id":"2"}}
24+
{"test":"world"}
25+
26+
- do:
27+
search:
28+
rest_total_hits_as_int: true
29+
index: test_index
30+
body:
31+
query:
32+
query_string:
33+
query: "test: /h[a-z].*/"
34+
35+
- match: {hits.total: 1}
36+
- match: {hits.hits.0._id: "1"}
37+
38+
- do:
39+
search:
40+
rest_total_hits_as_int: true
41+
index: test_index
42+
body:
43+
query:
44+
query_string:
45+
query: "test_alias: /h[a-z].*/"
46+
47+
- match: {hits.total: 1}
48+
- match: {hits.hits.0._id: "1"}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.apache.lucene.search.SynonymQuery;
5757
import org.apache.lucene.search.WildcardQuery;
5858
import org.apache.lucene.util.BytesRef;
59+
import org.apache.lucene.util.automaton.RegExp;
5960
import org.opensearch.common.lucene.search.Queries;
6061
import org.opensearch.common.regex.Regex;
6162
import org.opensearch.common.unit.Fuzziness;
@@ -787,8 +788,12 @@ private Query getRegexpQuerySingle(String field, String termStr) throws ParseExc
787788
if (currentFieldType == null) {
788789
return newUnmappedFieldQuery(field);
789790
}
790-
setAnalyzer(getSearchAnalyzer(currentFieldType));
791-
return super.getRegexpQuery(field, termStr);
791+
if (forceAnalyzer != null) {
792+
setAnalyzer(forceAnalyzer);
793+
return super.getRegexpQuery(field, termStr);
794+
}
795+
termStr = getAnalyzer().normalize(field, termStr).utf8ToString();
796+
return currentFieldType.regexpQuery(termStr, RegExp.ALL, 0, getDeterminizeWorkLimit(), getMultiTermRewriteMethod(), context);
792797
} catch (RuntimeException e) {
793798
if (lenient) {
794799
return newLenientFieldQuery(field, e);

0 commit comments

Comments
 (0)