Skip to content

Be strict when parsing values searching for booleans #21555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,15 @@ public BytesRef indexedValueForSearch(Object value) {
} else {
sValue = value.toString();
}
if (sValue.length() == 0) {
return Values.FALSE;
}
if (sValue.length() == 1 && sValue.charAt(0) == 'F') {
return Values.FALSE;
}
if (Booleans.parseBoolean(sValue, false)) {
return Values.TRUE;
switch (sValue) {
case "true":
return Values.TRUE;
case "false":
return Values.FALSE;
default:
throw new IllegalArgumentException("Can't parse boolean value [" +
sValue + "], expected [true] or [false]");
}
return Values.FALSE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testExternalValues() throws Exception {
SearchResponse response;

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

assertThat(response.getHits().totalHits(), equalTo((long) 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ public void testPhraseQueryOnFieldWithNoPositions() throws Exception {
assertHitCount(resp, 1L);
}

public void testBooleanStrictQuery() throws Exception {
Exception e = expectThrows(Exception.class, () ->
client().prepareSearch("test").setQuery(
queryStringQuery("foo").field("f_bool")).get());
assertThat(ExceptionsHelper.detailedMessage(e),
containsString("Can't parse boolean value [foo], expected [true] or [false]"));
}

private void assertHits(SearchHits hits, String... ids) {
assertThat(hits.totalHits(), equalTo((long) ids.length));
Set<String> hitIds = new HashSet<>();
Expand Down
8 changes: 4 additions & 4 deletions docs/reference/mapping/types/boolean.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ PUT my_index

POST my_index/my_type/1
{
"is_published": true <1>
"is_published": 1 <1>
}

GET my_index/_search
{
"query": {
"term": {
"is_published": 1 <2>
"is_published": true <2>
}
}
}
--------------------------------------------------
// CONSOLE
<1> Indexing a document with a JSON `true`.
<2> Querying for the document with `1`, which is interpreted as `true`.
<1> Indexing a document with `1`, which is interpreted as `true`.
<1> Searching for documents with a JSON `true`.

Aggregations like the <<search-aggregations-bucket-terms-aggregation,`terms`
aggregation>> use `1` and `0` for the `key`, and the strings `"true"` and
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/migration/migrate_6_0/search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@

* The `collect_payloads` parameter of the `span_near` query has been removed. Payloads will be
loaded when needed.

* Queries on boolean fields now strictly parse boolean-like values. This means
only the strings `"true"` and `"false"` will be parsed into their boolean
counterparts. Other strings will cause an error to be thrown.