rest - Elasticsearch存在对对象类型的查询返回0个结果

标签 rest api elasticsearch elasticsearch-5

[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/

相关文章:

firebase - Firebase Firestore REST API 是否支持流式传输

ios - subview 可以算作在 iOS 中使用未记录的 API 吗?

arrays - 在 ElasticSearch 中从数组中删除元素/对象,然后匹配查询

java - 使用 play 框架的 elasticsearch 问题

jquery - Ajax 请求使用 Rails 保存文件

java - jersey RESTful api 在使用 FormDataMultipart 时回复 415 不受支持的媒体类型

api - InputPhoneContact 没有 len()

javascript - 使用 Javascript 的 Google URL 缩短 API

Elasticsearch 地理距离查询

android - 在 Internet 上发布我的 RESTful Web 服务