Skip to content

Commit

Permalink
Make empty string searches be consistent with case (in)sensitivity (e…
Browse files Browse the repository at this point in the history
…lastic#110833)

If we determine that the searchable term is completely empty, we switch back to a regular term query. This way we return the same docs as expected when we do a case sensitive search.

closes: elastic#108968
  • Loading branch information
benwtrent authored Jul 17, 2024
1 parent db8bd66 commit 28c7cbc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog/110833.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 110833
summary: Make empty string searches be consistent with case (in)sensitivity
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
"case insensitive term query on blank keyword is consistent":
- requires:
cluster_features: [ "gte_v8.16.0" ]
reason: "query consistency bug fix in 8.16.0"
- do:
indices.create:
index: index_with_blank_keyword
body:
settings:
number_of_shards: 1
mappings:
properties:
keyword_field:
type: keyword
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "index_with_blank_keyword", "_id": "1"}}'
- '{"keyword_field": ""}'

- do:
search:
rest_total_hits_as_int: true
index: index_with_blank_keyword
body: {"query" : {"term" : {"keyword_field" : {"value": ""}}}}

- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
index: index_with_blank_keyword
body: { "query": { "term": { "keyword_field": {"value": "", "case_insensitive": true } } } }

- match: { hits.total: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ protected BytesRef indexedValueForSearch(Object value) {
@Override
public Query termQueryCaseInsensitive(Object value, SearchExecutionContext context) {
failIfNotIndexed();
return AutomatonQueries.caseInsensitiveTermQuery(new Term(name(), indexedValueForSearch(value)));
final BytesRef valueForSearch = indexedValueForSearch(value);
// check if valueForSearch is the same as an empty string
// if we have a length of zero, just do a regular term query
if (valueForSearch.length == 0) {
return termQuery(value, context);
}
return AutomatonQueries.caseInsensitiveTermQuery(new Term(name(), valueForSearch));
}

@Override
Expand Down

0 comments on commit 28c7cbc

Please sign in to comment.