Skip to content

Fix regex query to work with field alias #18215

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Use Bad Request status for InputCoercionException ([#18161](https://github.com/opensearch-project/OpenSearch/pull/18161))
- Null check field names in QueryStringQueryBuilder ([#18194](https://github.com/opensearch-project/OpenSearch/pull/18194))
- Avoid NPE if on SnapshotInfo if 'shallow' boolean not present ([#18187](https://github.com/opensearch-project/OpenSearch/issues/18187))
- Fix regex query from query string query to work with field alias ([#18215](https://github.com/opensearch-project/OpenSearch/issues/18215))

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
setup:
- skip:
version: " - 3.0.99"
reason: "regex query over field alias support starts 3.1"

- do:
indices.create:
index: test_index
body:
settings:
number_of_shards: 1
number_of_replicas: 0
mappings:
properties:
test:
type: text
test_alias:
type: alias
path: test

- do:
bulk:
refresh: true
body: |
{"index":{"_index":"test_index","_id":"1"}}
{"test":"hello"}
{"index":{"_index":"test_index","_id":"2"}}
{"test":"world"}

---
"regex search on normal field":
- do:
search:
rest_total_hits_as_int: true
index: test_index
body:
query:
query_string:
query: "test: /h[a-z].*/"

- match: {hits.total: 1}
- match: {hits.hits.0._id: "1"}

---
"regex search on alias field":
- do:
search:
rest_total_hits_as_int: true
index: test_index
body:
query:
query_string:
query: "test_alias: /h[a-z].*/"

- match: {hits.total: 1}
- match: {hits.hits.0._id: "1"}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.automaton.RegExp;
import org.opensearch.common.lucene.search.Queries;
import org.opensearch.common.regex.Regex;
import org.opensearch.common.unit.Fuzziness;
Expand Down Expand Up @@ -787,8 +788,12 @@
if (currentFieldType == null) {
return newUnmappedFieldQuery(field);
}
setAnalyzer(getSearchAnalyzer(currentFieldType));
return super.getRegexpQuery(field, termStr);
if (forceAnalyzer != null) {
setAnalyzer(forceAnalyzer);

Check warning on line 792 in server/src/main/java/org/opensearch/index/search/QueryStringQueryParser.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L792 was not covered by tests
}
// query string query normalizes search value
termStr = getAnalyzer().normalize(currentFieldType.name(), termStr).utf8ToString();
return currentFieldType.regexpQuery(termStr, RegExp.ALL, 0, getDeterminizeWorkLimit(), getMultiTermRewriteMethod(), context);
} catch (RuntimeException e) {
if (lenient) {
return newLenientFieldQuery(field, e);
Expand Down
Loading