在Elasticsearch中查询嵌套数组

时间:2017-06-05 14:47:07

标签: elasticsearch kibana

我在Elasticsearch中使用以下语法获得以下数据索引:

PUT /try1
{
"mappings" : {
  "product" : {
   "properties" : {
     "name": { "type" : "text" },
     "categories": {
       "type": "nested",
       "properties": {
         "range":{"type":"text"}
       }
     }
   }
  }
 }
}

范围类型有一个单词数组:[“high”,“medium”,“low”] 我需要访问嵌套类别中的range元素。我尝试使用以下语法:

GET /try1/product/_search
{
    "query": {
        "nested" : {
            "path" : "categories",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"categories.range": "low"} }
                    ]
                }
            }
        }
    }

}

但是,我收到错误消息:  “reason”:“”“无法创建查询:...

有人可以为此提供解决方案吗?

2 个答案:

答案 0 :(得分:0)

@KGB你可以尝试使你的查询略有不同:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "categories.range": "low"
                    }
                }
            ]
        }
    }
}

答案 1 :(得分:0)

{
    "query": {
        "nested" : {
            "path" : "categories",
            "query" : {
                "bool" : {
                    "must" : [
                    { categories.range": "low"}
                    ]
                }
            }
        }
    }
}

这完美无缺