简体   繁体   English

Elasticsearch查询具有多级子级的数据

[英]Elasticsearch query on data with multi level child

Given this sample data: 鉴于此样本数据:

"users": {
    "user1": {
         "first": "john",
         "last": "bellamy"
     },
    "user2": {
         .....
         .....
     }
 }

How can I set up elasticsearch to query/search on child first and last ? 如何设置elasticsearch来查询/搜索孩子的firstlast Ohter tutorials only shows one level child, not this 2 or more level child. Ohter教程只显示一个级别的孩子,而不是这个2级或更多级别的孩子。

I tried looking for a solution, and I guess that it has something to do with mapping option? 我试着寻找解决方案,我猜它与mapping选项有关?

I just started elasticsearch few days ago, already manage to set up and adding data. 我刚刚开始使用elasticsearch,已经设法设置和添加数据。

This works for me 这适合我

{
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "users.user2.firstname": {
                        "value": "sumit"
                    }
                }
            }]
        }
    }
}

nested users approach 嵌套用户方法

mappings 映射

{
    "mappings": {
        "test_type": {
            "properties": {
                "users": {
                    "type": "nested",
                    "properties": {
                        "firstname": {
                            "type": "text"
                        },
                        "lastname": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

query 询问

{
    "query": {
        "bool": {
            "must": [{
                "nested": {
                    "inner_hits": {},
                    "path": "users",
                    "query": {
                        "bool": {
                            "must": [{
                                "term": {
                                    "users.firstname": {
                                        "value": "ajay"
                                    }
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM