Elasticsearch:查询嵌套数组

时间:2016-04-07 15:21:43

标签: elasticsearch

我已经看过类似的问题,但当然,没有一个是我想要做的。

当我运行下面的查询时,我收到此错误:

  

“reason”:“[嵌套]无法在路径[contentGroup]”

下找到嵌套对象

我认为问题是contentGroup.name不存在,因为contentGroup是一个数组而不是一个对象。它需要是这样的: contentGroup的[0]。名称 和 contentGroup的[1]。名称 但我无法弄清楚如何做到这一点。

另一件可能错误的事情是我有两个项目互相嵌套,我不知道这是不是正确。

任何帮助都会很棒!

我的映射:

{
"mappings": {
    "articles": {
        "properties": {
            "contentGroups": {
                "type": "nested",
                "properties": {
                    "contentGroup": {
                        "type": "nested",
                        "properties": {
                            "id": {
                            "type": "string"
                            },
                            "name": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

当我在文章中输入时创建的内容(注意正在创建的数组):

"contentGroups": {
"contentGroup": [
    {
        "name": "Breaking",
        "id": "104"
    },
    {
        "name": "News",
        "id": "22"
    }
]

我的查询:

{
"query": {
    "bool": {
        "must": [
            { "match": { "headline": "whatever" }}, 
            {
                "nested": {
                    "path": "contentGroup",
                    "query": {
                        "bool": {
                            "must": [
                                { "match": { "contentGroup.name": "Breaking" }}
                            ]
                        }
                    }
                }
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:3)

您应该使用更简单的映射:

{
  "mappings": {
    "articles": {
      "properties": {
        "contentGroups": {
          "properties": {
            "id": {
              "type": "string"
            },
            "name": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

elasticsearch中的每个字段都支持多个值,无需指定此值。