【问题标题】:How to get nested documents by field value from other documents (ElasticSearch)?如何从其他文档(ElasticSearch)中按字段值获取嵌套文档?
【发布时间】:2017-08-04 13:35:34
【问题描述】:

示例查询:

localhost:8080/content/child/123

这个查询应该返回父 id 等于“123”的文档,然后我需要从返回的文档中获取 id 并在 Elastic 中搜索父 id 等于这个 id 的文档。

示例结果:

{ "id": "test", "parentId" : "123", "name" : "bambo" }

{ "id": "someId", "parentId" : "test", "name" : "bambo 2" }

【问题讨论】:

  • GET /content/child/123 表示搜索_index=content; _type=child; _id=123所在的特定文档。因此,在您的示例中,查询与两个文档都不匹配。你想返回什么?两个文件,还是只是第二个文件?不完全确定您在寻找什么。

标签: java elasticsearch


【解决方案1】:

首先GET /content/child/123表示搜索_index=content; _type=child; _id=123所在的具体文档,这不是你要找的。 _id 字段与您的 idparentId 字段完全不同。

据我所知,ES 目前没有您描述的“嵌套搜索”功能。您需要进行两次单独的搜索。


要搜索字段(“parentId”)包含特定值(“123”)的文档,您需要以下搜索查询

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "parentId": "123"
        }
      }
    }
  }
}

第一次搜索后,响应将是一个 JSON 对象 resp。您可以在resp["hits"]["hits"] 中找到返回结果的列表。然后,解析一个结果对象,得到你想要的id字段。例如,resp["hits"]["hits"][0]["_source"]["id"] 将为您提供 id 字段。

在这里查看文档https://www.elastic.co/guide/en/elasticsearch/reference/current/_the_search_api.html

【讨论】:

  • 这不是对弹性的查询,而是对某些休息控制器 /123 是父 ID 的查询,我想在字段父 ID 中获取所有具有此值的文档,然后我需要获取它们的 ID 并搜索嵌套文档,如果他们的父 ID 等于其中一个 ID,那么我也需要返回它们。
猜你喜欢
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多