查询不同级别的多层嵌套文档 [英] Query a multi level nested document at different levels

查看:66
本文介绍了查询不同级别的多层嵌套文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下格式的数据

{
  "mappings": {
    "blog": {
      "properties": {
        "comments": {
          "type": "nested",
          "properties": {
            "subComments": {
              "type": "nested"
            }
          }
        }
      }
    }
  }
}

我有多个文档,其数据类似

And i have multiple documents with data like

{
  "blog_post_id": "blog1",
  "comments": [
    {
      "id": "c1",
      "user_id": "u1",
      "timestamp": 1487781975676,
      "value": "CVLA1",
      "subComments": [
        {
          "value": "sub comment 1"
        },
        {
          "value": "sub comment 2"
        }
      ]
    },
    {
      "id": "c2",
      "user_id": "u1",
      "timestamp": 1487781975686,
      "value": "CVLA2",
      "subComments": [
        {
          "value": "sub comment 3"
        },
        {
          "value": "sub comment 4"
        }
      ]
    }
  ]
}

我要匹配具有注释值CVLA1的博客文档和具有子注释2"值的suc注释.

I'd like match the blog documents which have comment value CVLA1 and a suc comment which has value "sub comment 2".

我写了一个类似

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.value": "CVLA1"
              }
            },
            {
              "nested": {
                "path": "comments.subComments",
                "query": {
                  "match": {
                    "commnets.subComments.value": "sub comment 2"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

但是这个不能正常工作.对于如何在多层嵌套文档的不同级别上进行查询有任何帮助.

But this one doesn't work as expected. Any help how to query at different levels of a multi level nested document.

推荐答案

您的查询中有一个关于 commnets.subComments.value 的错字.它应该是 comments.subComments.value .因此整个查询看起来像这样:

You have a typo in your query around commnets.subComments.value. It should be comments.subComments.value. So the entire query would look like this:

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.value": "CVLA1"
              }
            },
            {
              "nested": {
                "path": "comments.subComments",
                "query": {
                  "match": {
                    "comments.subComments.value": "sub comment 2"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

我仔细检查了-对我来说很好.

I double checked - it works fine for me.

这篇关于查询不同级别的多层嵌套文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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