如何从elasticsearch中的查询中获取满足特定条件的特定嵌套对象

时间:2018-03-14 07:31:57

标签: elasticsearch nested

Iam storing in elastic like this


{
  "user":{
           "name":"Johnson",
           "age":29,
           "education":[
            {
                "school":"STE college",
                "year":2017,
                "qualification":"Phd",
                "isRecent":1
            },
            {
                "school":"business school",
                "year":2015,
                "qualification":"Mba",
                "isRecent":0
            },
            {
                "school":"saint school",
                "year":2013,
                "qualification":"Btech",
                "isRecent":0
            }
           ]
     }
}

我需要获得年龄为29岁的查询数据,并将学校作为" STE大学"只有教育最近的地方(学校=" STE大学"同一嵌套对象的isRecent = 1)

1 个答案:

答案 0 :(得分:0)

很大程度上取决于您的映射,但假设school被映射为文本和关键字,这应该有效:

{
    "query" : { 
        "bool" : { 
            "must" : [
                {"term" : {"user.age" : "29"}},
                {"term" : {"user.education.school.keyword" : "STE college"}},
                {"term" : {"user.education.isRecent" : "1"}}
            ]
        }
    }
}