Skip to content

Commit 96122aa

Browse files
authored
Be strict when parsing values searching for booleans (#21555)
This changes only the query parsing behavior to be strict when searching on boolean values. We continue to accept the variety of values during index time, but searches will only be parsed using `"true"` or `"false"`. Resolves #21545
1 parent cd4634b commit 96122aa

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

core/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,15 @@ public BytesRef indexedValueForSearch(Object value) {
152152
} else {
153153
sValue = value.toString();
154154
}
155-
if (sValue.length() == 0) {
156-
return Values.FALSE;
157-
}
158-
if (sValue.length() == 1 && sValue.charAt(0) == 'F') {
159-
return Values.FALSE;
160-
}
161-
if (Booleans.parseBoolean(sValue, false)) {
162-
return Values.TRUE;
155+
switch (sValue) {
156+
case "true":
157+
return Values.TRUE;
158+
case "false":
159+
return Values.FALSE;
160+
default:
161+
throw new IllegalArgumentException("Can't parse boolean value [" +
162+
sValue + "], expected [true] or [false]");
163163
}
164-
return Values.FALSE;
165164
}
166165

167166
@Override

core/src/test/java/org/elasticsearch/index/mapper/ExternalValuesMapperIntegrationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testExternalValues() throws Exception {
106106
SearchResponse response;
107107

108108
response = client().prepareSearch("test-idx")
109-
.setPostFilter(QueryBuilders.termQuery("field.bool", "T"))
109+
.setPostFilter(QueryBuilders.termQuery("field.bool", "true"))
110110
.execute().actionGet();
111111

112112
assertThat(response.getHits().totalHits(), equalTo((long) 1));

core/src/test/java/org/elasticsearch/search/query/QueryStringIT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ public void testPhraseQueryOnFieldWithNoPositions() throws Exception {
247247
assertHitCount(resp, 1L);
248248
}
249249

250+
public void testBooleanStrictQuery() throws Exception {
251+
Exception e = expectThrows(Exception.class, () ->
252+
client().prepareSearch("test").setQuery(
253+
queryStringQuery("foo").field("f_bool")).get());
254+
assertThat(ExceptionsHelper.detailedMessage(e),
255+
containsString("Can't parse boolean value [foo], expected [true] or [false]"));
256+
}
257+
250258
private void assertHits(SearchHits hits, String... ids) {
251259
assertThat(hits.totalHits(), equalTo((long) ids.length));
252260
Set<String> hitIds = new HashSet<>();

docs/reference/mapping/types/boolean.asciidoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ PUT my_index
3232
3333
POST my_index/my_type/1
3434
{
35-
"is_published": true <1>
35+
"is_published": 1 <1>
3636
}
3737
3838
GET my_index/_search
3939
{
4040
"query": {
4141
"term": {
42-
"is_published": 1 <2>
42+
"is_published": true <2>
4343
}
4444
}
4545
}
4646
--------------------------------------------------
4747
// CONSOLE
48-
<1> Indexing a document with a JSON `true`.
49-
<2> Querying for the document with `1`, which is interpreted as `true`.
48+
<1> Indexing a document with `1`, which is interpreted as `true`.
49+
<1> Searching for documents with a JSON `true`.
5050

5151
Aggregations like the <<search-aggregations-bucket-terms-aggregation,`terms`
5252
aggregation>> use `1` and `0` for the `key`, and the strings `"true"` and

docs/reference/migration/migrate_6_0/search.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55

66
* The `collect_payloads` parameter of the `span_near` query has been removed. Payloads will be
77
loaded when needed.
8+
9+
* Queries on boolean fields now strictly parse boolean-like values. This means
10+
only the strings `"true"` and `"false"` will be parsed into their boolean
11+
counterparts. Other strings will cause an error to be thrown.

0 commit comments

Comments
 (0)