Skip to content

Commit

Permalink
Set analyzer to regex query string search (#3967)
Browse files Browse the repository at this point in the history
Sets analyzer to regex query string search

Signed-off-by: yogev mets <[email protected]>
(cherry picked from commit ea4cfcc)
  • Loading branch information
yyyogev authored and nknize committed Oct 12, 2022
1 parent 51678a8 commit d675e6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ setup:
id: 1
body: { text: some short words with a stupendously long one }

- do:
index:
index: test
id: 2
body: { text: sentence with UPPERCASE WORDS }

- do:
indices.refresh:
index: [test]
Expand Down Expand Up @@ -76,6 +82,22 @@ setup:
- match: {hits.max_score: 1}
- match: {hits.hits.0._score: 1}

---
"search with uppercase regex":
- do:
search:
rest_total_hits_as_int: true
index: test
body:
query:
query_string:
default_field: text
query: /UPPERCASE/

- match: {hits.total: 1}
- match: {hits.max_score: 1}
- match: {hits.hits.0._score: 1}

---
"search index prefixes with span_multi":
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
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 @@ -565,7 +564,7 @@ private Query getPrefixQuerySingle(String field, String termStr) throws ParseExc
if (currentFieldType == null || currentFieldType.getTextSearchInfo() == TextSearchInfo.NONE) {
return newUnmappedFieldQuery(field);
}
setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer);
setAnalyzer(getSearchAnalyzer(currentFieldType));
Query query = null;
if (currentFieldType.getTextSearchInfo().isTokenized() == false) {
query = currentFieldType.prefixQuery(termStr, getMultiTermRewriteMethod(), context);
Expand Down Expand Up @@ -741,6 +740,13 @@ private Query getWildcardQuerySingle(String field, String termStr) throws ParseE
}
}

private Analyzer getSearchAnalyzer(MappedFieldType currentFieldType) {
if (forceAnalyzer == null) {
return queryBuilder.context.getSearchAnalyzer(currentFieldType);
}
return forceAnalyzer;
}

@Override
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
final int maxAllowedRegexLength = context.getIndexSettings().getMaxRegexLength();
Expand Down Expand Up @@ -781,11 +787,8 @@ private Query getRegexpQuerySingle(String field, String termStr) throws ParseExc
if (currentFieldType == null) {
return newUnmappedFieldQuery(field);
}
if (forceAnalyzer != null) {
setAnalyzer(forceAnalyzer);
return super.getRegexpQuery(field, termStr);
}
return currentFieldType.regexpQuery(termStr, RegExp.ALL, 0, getDeterminizeWorkLimit(), getMultiTermRewriteMethod(), context);
setAnalyzer(getSearchAnalyzer(currentFieldType));
return super.getRegexpQuery(field, termStr);
} catch (RuntimeException e) {
if (lenient) {
return newLenientFieldQuery(field, e);
Expand Down

0 comments on commit d675e6b

Please sign in to comment.