Skip to content

Commit b20eaf3

Browse files
authored
Handle special regex cases for version fields (#132511)
It is possible that the runAutomaton created is `null` as its one of the following cases: - none: meaning no matches - all: meaning all matches - single: meaning its just a single term (not actually regex) Each of these can be handled directly.
1 parent afdd0b8 commit b20eaf3

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

docs/changelog/132511.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 132511
2+
summary: Handle special regex cases for version fields
3+
area: Search
4+
type: bug
5+
issues: []

x-pack/plugin/mapper-version/src/main/java/org/elasticsearch/xpack/versionfield/VersionStringFieldMapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOExc
198198
@Override
199199
protected AcceptStatus accept(BytesRef term) throws IOException {
200200
BytesRef decoded = VersionEncoder.decodeVersion(term);
201+
if (compiled.runAutomaton == null) {
202+
return switch (compiled.type) {
203+
case SINGLE -> decoded.equals(compiled.term) ? AcceptStatus.YES : AcceptStatus.NO;
204+
case ALL -> AcceptStatus.YES;
205+
case NONE -> AcceptStatus.NO;
206+
default -> {
207+
assert false : "Unexpected automaton type: " + compiled.type;
208+
yield AcceptStatus.NO;
209+
}
210+
};
211+
}
201212
boolean accepted = compiled.runAutomaton.run(decoded.bytes, decoded.offset, decoded.length);
202213
if (accepted) {
203214
return AcceptStatus.YES;

x-pack/plugin/mapper-version/src/test/java/org/elasticsearch/xpack/versionfield/VersionStringFieldTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,35 @@ public void testRegexQuery() throws Exception {
216216
assertEquals("2.1.0-alpha.beta", response.getHits().getHits()[1].getSourceAsMap().get("version"));
217217
}
218218
);
219+
220+
assertResponse(
221+
client().prepareSearch(indexName).setQuery(QueryBuilders.regexpQuery("version", ".*").caseInsensitive(true)),
222+
response -> {
223+
assertEquals(5, response.getHits().getTotalHits().value());
224+
}
225+
);
226+
227+
assertResponse(
228+
client().prepareSearch(indexName).setQuery(QueryBuilders.regexpQuery("version", "2\\.1\\.0").caseInsensitive(false)),
229+
response -> {
230+
assertEquals(1, response.getHits().getTotalHits().value());
231+
}
232+
);
233+
234+
assertResponse(
235+
client().prepareSearch(indexName).setQuery(QueryBuilders.regexpQuery("version", "2\\.1\\.1").caseInsensitive(false)),
236+
response -> {
237+
assertEquals(0, response.getHits().getTotalHits().value());
238+
}
239+
);
240+
241+
// empty regex should not match anything
242+
assertResponse(
243+
client().prepareSearch(indexName).setQuery(QueryBuilders.regexpQuery("version", "").caseInsensitive(false)),
244+
response -> {
245+
assertEquals(0, response.getHits().getTotalHits().value());
246+
}
247+
);
219248
}
220249

221250
public void testFuzzyQuery() throws Exception {

0 commit comments

Comments
 (0)