Elasticsearch 存在对象类型 returns 0 个结果的查询
Elasticseach exists query on object type returns 0 results
[UPD] Elasticsearch 版本为 5.6
我在 foo_index
中定义了以下映射:
{
"foo": {
"properties": {
"bar": {
"type": "object",
"dynamic": false
}
...
}
}
}
foo_index
中的某些文档包含 bar
属性 和任意 json 数据,而其他文档则不包含。所以存储在 foo_index
中的文档如下所示:
[
{ bar: { arbitrary: { json: 1 } }, ... },
{ bar: { arbitrary: { json: 2 } }, ... },
{ ... }, // bar property is absent,
{ bar: { arbitrary: { json: 3 } }, ... }
]
当我执行以下查询以仅获取那些包含 bar
属性 的对象时,我得到 0 个结果:
GET foo_index/foo/_search
{
"query": {
"exists": {
"field": "bar"
}
}
}
为什么这个查询不起作用?我希望它包含 return foo
包含 bar
属性.
的文档
您无法查看文档的原因是您在映射中设置了字段 dynamic: false
。
所以基本上,当您这样做时,您是在告诉 elasticsearch 不要 为该字段创建单独的倒排索引。基本上该字段虽然存在于文档中,但它不会是可搜索的字段。因此,没有查询适用于该字段。换句话说,ES 的行为就像没有这样的字段一样。
如this link中所述,当它设置为false
时,我们有以下内容
Newly detected fields are ignored. These fields will not be indexed so
will not be searchable but will still appear in the _source field of
returned hits. These fields will not be added to the mapping, new
fields must be added explicitly.
基本上将您的映射更改为以下内容:
映射:
PUT foo_index
{
"mappings": {
"foo":{
"properties": {
"bar":{
"type": "object"
}
}
}
}
}
如果您在映射中删除该字段,Exists query 将正常工作。
请注意,您需要删除索引,使用映射更改重新创建索引,重新摄取文档,然后尝试查询。
[UPD] Elasticsearch 版本为 5.6
我在 foo_index
中定义了以下映射:
{
"foo": {
"properties": {
"bar": {
"type": "object",
"dynamic": false
}
...
}
}
}
foo_index
中的某些文档包含 bar
属性 和任意 json 数据,而其他文档则不包含。所以存储在 foo_index
中的文档如下所示:
[
{ bar: { arbitrary: { json: 1 } }, ... },
{ bar: { arbitrary: { json: 2 } }, ... },
{ ... }, // bar property is absent,
{ bar: { arbitrary: { json: 3 } }, ... }
]
当我执行以下查询以仅获取那些包含 bar
属性 的对象时,我得到 0 个结果:
GET foo_index/foo/_search
{
"query": {
"exists": {
"field": "bar"
}
}
}
为什么这个查询不起作用?我希望它包含 return foo
包含 bar
属性.
您无法查看文档的原因是您在映射中设置了字段 dynamic: false
。
所以基本上,当您这样做时,您是在告诉 elasticsearch 不要 为该字段创建单独的倒排索引。基本上该字段虽然存在于文档中,但它不会是可搜索的字段。因此,没有查询适用于该字段。换句话说,ES 的行为就像没有这样的字段一样。
如this link中所述,当它设置为false
Newly detected fields are ignored. These fields will not be indexed so will not be searchable but will still appear in the _source field of returned hits. These fields will not be added to the mapping, new fields must be added explicitly.
基本上将您的映射更改为以下内容:
映射:
PUT foo_index
{
"mappings": {
"foo":{
"properties": {
"bar":{
"type": "object"
}
}
}
}
}
如果您在映射中删除该字段,Exists query 将正常工作。
请注意,您需要删除索引,使用映射更改重新创建索引,重新摄取文档,然后尝试查询。