Skip to content

Commit adbbf7f

Browse files
authored
Add deprecation logging for lenient boolean queries (#21570)
* Add deprecation logging for lenient boolean queries Relates to #21555, this is the deprecation logging for what was removed. * Make logger final and correct misspelled word
1 parent 1d89775 commit adbbf7f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.lucene.util.BytesRef;
2828
import org.elasticsearch.common.Booleans;
2929
import org.elasticsearch.common.Nullable;
30+
import org.elasticsearch.common.logging.DeprecationLogger;
31+
import org.elasticsearch.common.logging.Loggers;
3032
import org.elasticsearch.common.lucene.Lucene;
3133
import org.elasticsearch.common.settings.Settings;
3234
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -53,6 +55,8 @@ public class BooleanFieldMapper extends FieldMapper {
5355

5456
public static final String CONTENT_TYPE = "boolean";
5557

58+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(Loggers.getLogger(BooleanFieldMapper.class));
59+
5660
public static class Defaults {
5761
public static final MappedFieldType FIELD_TYPE = new BooleanFieldType();
5862

@@ -152,16 +156,25 @@ public BytesRef indexedValueForSearch(Object value) {
152156
} else {
153157
sValue = value.toString();
154158
}
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;
159+
switch (sValue) {
160+
case "true":
161+
return Values.TRUE;
162+
case "false":
163+
return Values.FALSE;
164+
default:
165+
deprecationLogger.deprecated("searching using boolean value [" + sValue +
166+
"] is deprecated, please use [true] or [false]");
167+
if (sValue.length() == 0) {
168+
return Values.FALSE;
169+
}
170+
if (sValue.length() == 1 && sValue.charAt(0) == 'F') {
171+
return Values.FALSE;
172+
}
173+
if (Booleans.parseBoolean(sValue, false)) {
174+
return Values.TRUE;
175+
}
176+
return Values.FALSE;
163177
}
164-
return Values.FALSE;
165178
}
166179

167180
@Override

docs/reference/mapping/types/boolean.asciidoc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ True values::
1313

1414
Anything that isn't false.
1515

16+
deprecated[5.1.0 While Elasticsearch will currently accept the above values during index time.
17+
Searching a boolean field using these pseudo-boolean values is deprecated.
18+
Please use "true" or "false" instead.]
19+
1620
For example:
1721

1822
[source,js]
@@ -32,21 +36,21 @@ PUT my_index
3236
3337
POST my_index/my_type/1
3438
{
35-
"is_published": true <1>
39+
"is_published": 1 <1>
3640
}
3741
3842
GET my_index/_search
3943
{
4044
"query": {
4145
"term": {
42-
"is_published": 1 <2>
46+
"is_published": true <2>
4347
}
4448
}
4549
}
4650
--------------------------------------------------
4751
// CONSOLE
48-
<1> Indexing a document with a JSON `true`.
49-
<2> Querying for the document with `1`, which is interpreted as `true`.
52+
<1> Indexing a document with `1`, which is interpreted as `true`.
53+
<2> Searching for documents with a JSON `true`.
5054

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

0 commit comments

Comments
 (0)