搜索字段的嵌套文档,无论字段的位置如何 [英] Searching nested documents for a field regardless of the positioning of the field

查看:98
本文介绍了搜索字段的嵌套文档,无论字段的位置如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
id:1,
评论:评论文字,
回复:[{
id:2,
评论:嵌套评论文字,
},{
id:3,
评论:另一个嵌套的评论文字,
}]
}

我想搜索 id == 2 而不知道它是在文档的第一级还是在第二。这可能吗?还请记住,嵌套级别可以是任何东西(在开发时未知)。



如果可以的话,通过搜索 id == 2 而不知道文档的第二级有一个 id

解决方案

尝试这样:

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


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?

解决方案

Try this:

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

这篇关于搜索字段的嵌套文档,无论字段的位置如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆