当前位置:   article > 正文

python elasticsearch 分组统计_python bool must 多字段aggregations分组

python bool must 多字段aggregations分组

聚合(Aggregations)

  1. query = {
  2. "query": {
  3. "bool": {
  4. "must": [
  5. {"term": {"company_id": company_id}},
  6. {"term": {"subject_type": 2}},
  7. ],
  8. "must_not": [],
  9. "should": []
  10. }
  11. },
  12. "size": 0, # 设置返回信息条数
  13. "aggs": {
  14. "group_by_real_name": {
  15. "terms": {"field": "real_name.keyword"},
  16. "aggs": { #条件嵌套
  17. "group_by_screen_id": {
  18. "terms": {"field": "screen_id"},
  19. }
  20. }
  21. }
  22. }
  23. }

设置fielddata=true:

在系统终端设置:

curl -i -H "Content-Type:application/json" -XPUT 127.0.0.1:9200/your_index/_mapping/your_type/?pretty  -d'{"your_type":{"properties":{"your_field_name":{"type":"text","fielddata":true}}}}'

将以上标红位置更改为自己对应的字段,在Linux上似乎可以直接操作,windows似乎需要下一个curl插件,我未曾设置过fielddata=true,我是使用了上面real_name.keyword方法。

curl -i -H "Content-Type:application/json" -XPUT 127.0.0.1:9200/event/_mapping/koala-index/?pretty  -d'{"koala-index":{"properties":{"real_name":{"type":"text","fielddata":true}}}}'
 

去重统计:

query = {
    "query": {
        "bool": {
            "must": [
                {"term": {"company_id": company_id}},
            ],
            "must_not": [],
            "should": []
        }
    },
    "size": 0,
    "aggs": {
        "group_by_screen_id": {
            "terms": {"field": "screen_id"},
            "aggs": {"group_by_subject_type": {
                "terms": {"field": "subject_type"},
                "aggs": {"distinct_subject_ids": {       #去重统计
                    "cardinality": {"field": "subject_id"}
                }
                }
            }
            }
        }
    },
     "sort": [
       {"timestamp": {"order": 'desc'}}
     ],
    "from": (page - 1) * size,
    "size": size,
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/68132
推荐阅读
相关标签
  

闽ICP备14008679号