ElasticSearch - 查询以过滤和聚合嵌套对象术语 [英] ElasticSearch - Query to filter and aggregate on nested object term

查看:186
本文介绍了ElasticSearch - 查询以过滤和聚合嵌套对象术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的弹性搜索索引中包含以下类型的嵌套对象

I have nested objects in my elastic search index of the following type

 "_source": {
           "NAME": "MNQ",
           "LAST_MOD_DATE": 1373587200000,
           "ACTIVE_FL": "Y",
           "ID": "1008",
           "USER": [
              {
                 "USR_ID": 499,
                 "STATUS": "INACTV",
                 "NAME": "ABC"
              },
              {
                 "USR_ID": 53,
                 "STATUS": "ACTV",
                 "NAME": "XYZ"
              }
            ]
        }

我有以下用例来查询索引:

And I have following use cases for querying the index:


  • 获取特定ID的所有活动用户。例如:我想要获得活跃于id 1008 的用户,在这种情况下,这将是用户 XYZ

  • 获取所有活跃用户。例如:我执行一个match_all查询,我想在术语 USER.NAME 中聚合,但只能返回我的活跃用户名。

  • Get all active users for a particular id. Eg: I want to get users that are active for id 1008 which in this case would be user XYZ
  • Get all active users. Eg: I perform a match_all query and I wan to aggregate on term USER.NAME but it should only return me the names of active users.

我在执行这些嵌套操作时遇到困难,因为搜索活动状态将返回一个甚至有一个用户为活动的记录。我无法专门过滤掉非活动用户。

I am having trouble performing these nested operations as search for active status will return a record which has even one of the users as active. I am unable to specifically filter out inactive users. Any help in this regard is greatly appreciated.

推荐答案

因为您总是有兴趣提取用户,那么在这种情况下 parent-child 关系将比嵌套文档类型更好为用户。与嵌套类型一样,响应将具有不必要的有效内容,而inner_hits具有弹性。

since you are always interested in fetching the users then in that case parent-child relationship will do better than nested documents type for users. As with nested type the response will have unnecessary payload with inner_hits for elastic.

为了提供更好的导航和更灵活的查询,嵌套关联然后嵌套类型。

As the provide better navigation and more flexible queries for nested association then nested type.

同样在映射中可能需要选择类型为关键字或创建自定义分析器来保留您正在搜索区分大小写搜索的案例。

Also in mappings you may have to select type to keyword or create a custom analyzer to retain the case as you are searching as case sensitive search.

父子关系的映射

PUT parent_child_index
{
    "mappings": {
        "parent_document": {
            "properties": {
                "NAME": {
                    "type": "keyword"
                }
            }
        },
        "user": {
            "_parent": {
                "type": "parent_document"
            },
            "properties": {
                "USER_ID": {
                    "type": "keyword"
                },
                "STATUS": {
                    "type": "keyword"
                },
                "NAME": {
                    "type": "keyword"
                }
            }
        }
    }
}

索引父子文档

POST parent_child_index/parent_document
{
    "NAME": "MNQ",
    "LAST_MOD_DATE": 1373587200000,
    "ACTIVE_FL": "Y",
    "ID": "1008"
}


POST parent_child_index/user?parent=AVyBzQXmp_hWdUR22wGr
{
    "USR_ID": 53,
    "STATUS": "ACTV",
    "NAME": "XYZ"
}

查询

POST parent_child_index/user/_search
{
    "query": {
        "bool": {
            "must": [{
                    "has_parent": {
                        "parent_type": "parent_document",
                        "query": {
                            "bool": {
                                "must": [{
                                    "term": {
                                        "ID": {
                                            "value": "1008"
                                        }
                                    }
                                }]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "STATUS": {
                            "value": "ACTV"
                        }
                    }
                }
            ]
        }
    }
}

POST parent_child_index/user/_search
{
    "size": 0,
    "aggs": {
        "active_users": {
            "filter": {
                "term": {
                    "STATUS": "ACTV"
                }
            },
            "aggs": {
                "user_name": {
                    "terms": {
                        "field": "NAME",
                        "size": 10
                    }
                }
            }
        }
    }
}

这篇关于ElasticSearch - 查询以过滤和聚合嵌套对象术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆