1. 程式人生 > >elasticsearch系列-ES對多個欄位聚合,select A,B,COUNT(*) from table group by A,B

elasticsearch系列-ES對多個欄位聚合,select A,B,COUNT(*) from table group by A,B

ES對多個欄位聚合,select A,B,COUNT(*)from table group by A,B 假設有下表 NAME SEX PROF 李誠 男 副教授 張旭 男 講師 王萍 女 助教 劉冰 女 助教 要查詢select SEX,PROF,COUNT(*) from table group by SEX,PROF 1、正確的答案: 修改elasticsearch.yml配置檔案,新增下面兩個配置,重啟es叢集 script.engine.groovy.inline.aggs: on script.engine.groovy.inline.search: on
{
    "size": 0,
    "query": {
        "match_all": {}
    },
    "aggs": {
        "sexprof": {
            "terms": {
                "script": {
                    "inline": "doc['SEX.keyword'].value +'-split-'+ doc['PROF.keyword'].value "
                }
            }
        }
    }
}
JavaAPI Script script = new Script(ScriptType.INLINE, "groovy", "doc['SEX.keyword'].value+'-split-'+doc['PROF.keyword'].value", new HashMap<String, Object>()); TermsAggregationBuilder callTypeTeamAgg =AggregationBuilders.terms("sexprof").script(script); 這樣得到的sex,prof兩列是一起返回的,中間通過"-split-"分開,拿到結果後自行處理,結果大概像下面的(省略了沒用的資訊):
{
    "aggregations": {
        "sexprof": {
            "doc_count_error_upper_bound": 5,
            "sum_other_doc_count": 379,
            "buckets": [
                {
                    "key": "女-split-助教",
                    "doc_count": 2
                },
                {
                    "key": "男-split-講師",
                    "doc_count": 1
                },
                {
                    "key": "男-split-教授",
                    "doc_count": 1
                }
            ]
        }
    }
}

2、錯誤的答案:
{
    "query": {
        "match_all": {}
    },
    "aggs": {
        "sex": {
            "terms": {
                "field": "SEX.keyword"
            }
        },
        "prof": {
            "terms": {
                "field": "PROF.keyword'"
            }
        }
    }
}
拿到的結果是大概像這樣的(省略了沒用的資訊),分開統計了,這明顯不是我們想要的
{
    "aggregations": {
        "sex": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "女",
                    "doc_count": 2
                },
                {
                    "key": "男",
                    "doc_count": 2
                }
            ]
        },
        "prof": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "副教授",
                    "doc_count": 1
                },
                {
                    "key": "講師",
                    "doc_count": 1
                },
                {
                    "key": "助教",
                    "doc_count": 1
                }
            ]
        }
    }
}