如何在弹性搜索中获取嵌套对象?

时间:2016-06-30 11:43:29

标签: java elasticsearch

How can i fetch nested object in elastic search ? 

{
            "_index": "userinfo",
            "_type": "userdetails",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": "2",
               "name": "Robert Mark",
               "age": 42,
               "email": "robert.mark@ceb.com",
               "userType": {
                  "id": "3",
                  "type": "End User"
               },
               "hobbies": [
                  {
                     "id": "3",
                     "description": "Writing Books"
                  },
                  {
                     "id": "4",
                     "description": "Gardening"
                  }
               ]
            }
         }

这是我的json结构我想要获取所有记录,其中爱好描述是“园艺”。

我是弹性搜索的新手,如果有人知道,请帮助我。

“description”:“园艺”

2 个答案:

答案 0 :(得分:1)

这是您的查询:

{
  "query": {
    "nested": {
      "path": "hobbies",
      "query": {
        "match": {
          "hobbies.description": "Gardening"
        }
      }
    }
  }
}

答案 1 :(得分:0)

israelst是正确的如果你为你的爱好对象预先创建了nested映射类型。但默认情况下,它使用object而不是`嵌套。

{
  "query" : {
    "match" : {
      "hobbies.description" : "Gardening"
    }
  }
}

请注意,查询实际上是相同的,事实上,我的示例是嵌套版本的子集。