Elasticsearch父子关系文档的一个例子

1、创建索引及映射关系

put /test

{
    "mappings":{
        "osm":{
            "properties":{
                "user_event":{
                    "type":"join",
                    "relations":{
                        "user":"event"
                    }

                }

            }

        }

    }

}

2、添加父文档

put /test/osm/1?refresh

{
    "data":{
        "userId":1,
        "userName":"小王",
        "description":"第一条用户信息"
    }
,
    "user_event":{
        "name":"user"
    }

}

3、添加两条子文档

PUT /test/osm/2?routing=1&refresh

{
    "data":{
        "userId":1,
        "eventId":1,
        "eventName":"工单报错",
        "description":"第一个事件信息"
    }
,
    "user_event":{
        "name":"event",
        "parent":"1"
    }

}

PUT /test/osm/3?routing=1&refresh

{
    "data":{
        "userId":1,
        "eventId":
2,
        
"eventName":"呼叫电话",
        
"description":"第二个事件信息"
    }
,
    "user_event":{
        "name":"event",
        "parent":"1"
    }

}

5、查询客户信息

POST /test/_search

{
     "query" : {
        "bool":{
            "must":[
                {
                    "match":{
                        "data.userName":"小"
                    }

                }
,
                {
                    "has_child":{
                        "type":"event",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "match":{
                                            "data.eventName":"电话"
                                        }

                                    }

                                ]

                            }

                        }
,
                        "inner_hits":{
                        }

                    }

                }

            ]

        }

    }

}

6、查询事件信息

POST /test/_search

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "data.eventName":"工单"
                    }

                }
,
                {
                    "has_parent":{
                        "parent_type":"user",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "match":{
                                            "data.userName":"小王"
                                        }

                                    }

                                ]

                            }

                        }

                    }

                }

            ]

        }

    }

}

你可能感兴趣的:(搜索引擎)