Searching nested documents for a field regardless of the positioning of the field

被刻印的时光 ゝ 提交于 2019-12-13 00:06:05

问题


Consider a document in Elasticsearch like this:

{
  "id": 1,
  "Comment": "Comment text",
  "Reply": [{
    "id": 2,
    "Comment": "Nested comment text",
  }, {
    "id": 3,
    "Comment": "Another nested comment text",
  }]
}

I want to search for id == 2 without knowing whether it is in the firsts level of the document or in the second. Is this possible? Please also keep in mind that the nested level can be anything (unknown at development time).

If this is possible, what's the query to return this document by searching for id == 2 without knowing that there's an id is in the second level of the document?


回答1:


Try this:

{
  "query": {
    "query_string": {
      "fields": ["*.id","id"],
      "query": "2"
    }
  }
}


来源:https://stackoverflow.com/questions/38986099/searching-nested-documents-for-a-field-regardless-of-the-positioning-of-the-fiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!