[UPD] Elasticsearch版本是5.6
我在foo_index
中定义了以下映射:
{
"foo": {
"properties": {
"bar": {
"type": "object",
"dynamic": false
}
...
}
}
}
foo_index
中的某些文档包含具有任意json数据的bar
属性,而其他文档则没有。因此,存储在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"
}
}
}
为什么此查询不起作用?我希望它返回包含
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可以正常工作。
请注意,您将需要删除索引,使用映射中的更改重新创建索引,重新摄取文档,然后尝试查询。
关于rest - Elasticsearch存在对对象类型的查询返回0个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59750181/