用户 WittyID 错过了一些重要的事情,例如:
- 的值
null_value
必须与您的字段具有相同的数据类型,因此在他的示例中,他声明integer
字段但已定义NULL
as null_values
, 会抛出json_parse_exception
这被提到为important
in the 官方链接如下:
null_value 需要与字段具有相同的数据类型。为了
例如,长字段不能有字符串 null_value。
- The
null_value
仅影响数据的索引方式,它不会修改 _source 文档,因此在源文档中,无论您通过什么,都将被存储,而不是中提到的null_values
参数,在查询时您还需要使用该值null_value
param..
简而言之,null
ES 无法识别,因此您可以定义自定义值null
然后用它来索引和查询null
使用下面的示例很容易解释整个事情,任何人都可以尝试:
创建索引
{
"mappings": {
"properties": {
"my_signed_integer": {
"type":"integer",
"null_value": -1 --> note we defining `null` values as `-1`.
}
}
}
}
索引文档
-
store null
整数文档
{
“我的号码”:空
}
如果您从 ES 获取此文档,它将返回如下:
{
"_index": "so-6053847",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"my_number": null. --> As explained earlier, in source its stored as `null`.
}
}
-
指数非负值
{
“我的号码”:10
}
搜索查询以获取具有的整数null
values
{
"query": {
"term": {
"my_signed_integer": -1 -->notice same `null_value`, you need to mention
}
}
}
Result:
"hits": [
{
"_index": "so-6053847",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"my_signed_integer": null --> notice it shows `null`, not `-1`
}
}
]
搜索查询其他数字(不为空),即在我们的例子中10
{
"query": {
"term": {
"my_signed_integer": 10
}
}
}
Result
"hits": [
{
"_index": "so-6053847",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"my_signed_integer": 10 -->source matches the indexed value for this doc
}
}
]