Elasticsearch get count按城市分组的DISTINCT用户

yizd12fk  于 2023-05-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(156)

我有这样的结构索引

...
user_id => [
    type => keyword
],
geo => [
    type => nested
    city => [
       type => keyword
    ]
    ...
]
...

我想获取按www.example.com分组的不同用户geo.city
SQL

select
    geo.city as user_city, count(distinct user_id) as users
from 
    `table`
group by
    user_city
order by
    users desc

count total返回的是ok,但不是唯一的user_id我尝试这个但count by user_id是0

'aggs' => [
    'nested_geo' => [
        'nested' => [
             'path' => 'geo',
         ],
         'aggs' => [
              'group_by_city' => [
                   'terms' => [
                        'field' => 'geo.city',
                   ],
                   'aggs' => [
                        'distinct_users' => [
                             'cardinality' => [
                                  'field' => 'user_id',
                             ],
                        ],
                   ],
              ],
         ],
     ],
 ],
dpiehjr4

dpiehjr41#

使用' reverse_nested '解决

'aggs' => [
    'nested_geo' => [
        'nested' => [
            'path' => 'geo',
        ],
        'aggs' => [
            'group_by_city' => [
                'terms' => [
                    'field' => 'geo.city',
                ],
                'aggs' => [
                    'users' => [
                        //This is added
                        'reverse_nested' => new \stdClass(),
                        'aggs' => [
                            'distinct_users' => [
                                'cardinality' => [
                                    'field' => 'user_pseudo_id',
                                ],
                            ],
                        ]
                    ]
                ],
            ],
        ],
    ],
],

相关问题