Skip to content

Commit

Permalink
Add tests for wildcard + regexp match-all cases
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Froh <[email protected]>
  • Loading branch information
msfroh committed Jun 10, 2024
1 parent 3ee80ee commit 67584e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ setup:
id: 5
body:
my_field: "AbCd"
- do:
index:
index: test
id: 6
- do:
indices.refresh: {}

Expand Down Expand Up @@ -198,3 +202,26 @@ setup:
my_field:
value: ".*06-08.*Cluster-Manager Node.*"
- match: { hits.total.value: 0 }

---
"wildcard match-all works":
- do:
search:
index: test
body:
query:
wildcard:
my_field:
value: "*"
- match: { hits.total.value: 5 }
---
"regexp match-all works":
- do:
search:
index: test
body:
query:
wildcard:
my_field:
value: ".*"
- match: { hits.total.value: 5 }
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, bo
};
}

return new WildcardMatchingQuery(
name(),
matchAllTermsQuery(name(), getRequiredNGrams(finalValue)),
matchPredicate,
value,
context,
this
);
Set<String> requiredNGrams = getRequiredNGrams(finalValue);
Query approximation;
if (requiredNGrams.isEmpty()) {
approximation = this.existsQuery(context);
} else {
approximation = matchAllTermsQuery(name(), requiredNGrams);
}
return new WildcardMatchingQuery(name(), approximation, matchPredicate, value, context, this);
}

// Package-private for testing
Expand Down Expand Up @@ -540,7 +540,11 @@ public Query regexpQuery(
Automaton automaton = regExp.toAutomaton(maxDeterminizedStates);
CompiledAutomaton compiledAutomaton = new CompiledAutomaton(automaton);

return new WildcardMatchingQuery(name(), regexpToQuery(name(), regExp), s -> {
Query approximation = regexpToQuery(name(), regExp);
if (approximation instanceof MatchAllDocsQuery) {
approximation = existsQuery(context);
}
return new WildcardMatchingQuery(name(), approximation, s -> {
BytesRef valueBytes = BytesRefs.toBytesRef(s);
return compiledAutomaton.runAutomaton.run(valueBytes.bytes, valueBytes.offset, valueBytes.length);
}, "/" + value + "/", context, this);
Expand Down Expand Up @@ -704,7 +708,7 @@ private WildcardMatchingQuery(

@Override
public String toString(String s) {
return "WildcardMatchingQuery(" + fieldName + "\"" + patternString + "\")";
return "WildcardMatchingQuery(" + fieldName + ":\"" + patternString + "\")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,18 @@ public void testRegexpQuery() {
assertTrue(actualMatchingQuery.getSecondPhaseMatcher().test("abcdjk"));
assertTrue(actualMatchingQuery.getSecondPhaseMatcher().test("abefqwertyhi"));
}

public void testWildcardMatchAll() {
String pattern = "*";
MappedFieldType ft = new WildcardFieldMapper.WildcardFieldType("field");
Query actual = ft.wildcardQuery(pattern, null, null);
assertEquals(new WildcardFieldMapper.WildcardMatchingQuery("field", ft.existsQuery(null), "*"), actual);
}

public void testRegexpMatchAll() {
String pattern = ".*";
MappedFieldType ft = new WildcardFieldMapper.WildcardFieldType("field");
Query actual = ft.regexpQuery(pattern, 0, 0, 1000, null, null);
assertEquals(new WildcardFieldMapper.WildcardMatchingQuery("field", ft.existsQuery(null), "/.*/"), actual);
}
}

0 comments on commit 67584e3

Please sign in to comment.