国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Python 實(shí)現(xiàn)elasticsearch group by

kelvinlee / 429人閱讀

摘要:實(shí)現(xiàn)聚合通過實(shí)現(xiàn)聚合,詳情可見文檔有時(shí)候查詢數(shù)據(jù)的時(shí)候可能需要實(shí)現(xiàn)多字段的功能,例如要實(shí)現(xiàn)多個(gè)維度的聚合,需要嵌套的查詢語句設(shè)置一個(gè)大的分桶數(shù),防止一次統(tǒng)計(jì)不完整用函數(shù)構(gòu)建聚合語句的部分處理得到的數(shù)據(jù),將其組織成

es實(shí)現(xiàn)聚合

es通過agg實(shí)現(xiàn)聚合,詳情可見 es文檔


有時(shí)候查詢es數(shù)據(jù)的時(shí)候可能需要實(shí)現(xiàn)多字段group by的功能,例如:

SELECT sum(item_count) from A group by field1, field2, field3

要實(shí)現(xiàn)多個(gè)維度的聚合,需要嵌套的agg查詢語句:

{
    "query": {
    },
    "aggs": {
        "field1": {
            "terms": {
                "field": "field1",
                "size": 2147483647 #設(shè)置一個(gè)大的分桶數(shù),防止一次統(tǒng)計(jì)不完整
            },
            "aggs": {
                "field2": {
                    "terms": {
                        "field": "field2",
                        "size": 2147483647
                    },
                    "aggs": {
                        "field3": {
                            "terms": {
                                "field": "field3",
                                "size": 2147483647
                            },
                            "aggs": {
                                "sum_field": {
                                    "sum": {
                                        "field": "sum_field"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "size": 0
}

用函數(shù)構(gòu)建聚合語句的agg部分:

def build_query_aggs(fields, sum_field):
    agg_data = {}
    curr_field = agg_data
    for item in fields:
        curr_field[item] = {
            "terms": {
                "field": item,
                "size": 2147483647
            },
            "aggs": {}
        }
        curr_field = curr_field[item]["aggs"]
    curr_field[sum_field] = {
        "sum": {
                "field": sum_field
            }
    }
    return agg_data

處理得到的數(shù)據(jù),將其組織成list:

def build_es_aggs_data(data, fields, sum_field):
    curr_field = None
    res_data = []
    if len(fields) > 0:
        curr_field = fields[0]
    else:
        return
    curr_buckets = data[curr_field]["buckets"]
    for item in curr_buckets:
        if len(fields) == 1:
            curr_data= {}
            curr_data[curr_field] = item["key"]
            curr_data[sum_field] = item[sum_field]["value"]
            res_data.append(curr_data)
        else:
            pre_data = deepcopy(build_es_aggs_data(item, fields[1:], sum_field))
            for pre_item in pre_data:
                pre_item[curr_field] = item["key"]
                res_data.append(pre_item)
    return res_data

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/45168.html

相關(guān)文章

  • Elasticsearch實(shí)戰(zhàn)Java High Level REST Client實(shí)現(xiàn)員工管理案例

    摘要:開發(fā)環(huán)境這里講的是實(shí)戰(zhàn)對(duì)于如何安裝,配置環(huán)境變量,配置不做講解詳細(xì)請(qǐng)查看學(xué)習(xí)筆記四在上安裝和啟動(dòng)增量同步數(shù)據(jù)到方式實(shí)現(xiàn)從零到一超級(jí)詳里面有做詳細(xì)講解,對(duì)于搭建集群目前還沒有寫相關(guān)文章,讀者可以自行在項(xiàng)目的文件中添加的依賴實(shí)現(xiàn)員工信息 開發(fā)環(huán)境: elasticsearch:7.0.0kibana:7.0.0JDK: 1.8.0_201maven: 3.6.1 這里講的是實(shí)戰(zhàn)對(duì)于如何安裝...

    Barry_Ng 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<