private ValuesSourceAggregationBuilder makeCountAgg ( MethodField field ) { // Cardinality is approximate DISTINCT. if ( "DISTINCT" . equals ( field . getOption ( ) ) ) { if ( field . getParams ( ) . size ( ) == 1 ) { return AggregationBuilders . cardinality ( field . getAlias ( ) ) . field ( field . getParams ( ) . get ( 0 ) . value . toString ( ) ) ; } else { Integer precision_threshold = ( Integer ) ( field . getParams ( ) . get ( 1 ) . value ) ; return AggregationBuilders . cardinality ( field . getAlias ( ) ) . precisionThreshold ( precision_threshold ) . field ( field . getParams ( ) . get ( 0 ) . value . toString ( ) ) ; } } String fieldName = field . getParams ( ) . get ( 0 ) . value . toString ( ) ; /* zhongshu-comment count(1) count(0)这种应该是查不到东西的，除非你的字段名就叫做1、0这样 es的count是针对某个字段做count的，见下面的dsl，对os这个字段做count "aggregations": { "COUNT(os)": { "value_count": { "field": "os" } } } 假如你是写count(*)，那es-sql就帮你转成对"_index"字段做count，每一条数据都会有"_index"字段，该字段存储的是索引的名字 */ // In case of count(*) we use '_index' as field parameter to count all documents if ( "*" . equals ( fieldName ) ) { KVValue kvValue = new KVValue ( null , "_index" ) ; field . getParams ( ) . set ( 0 , kvValue ) ; /* zhongshu-comment 这个看起来有点多此一举：先将"_index"字符串封装到KVValue中，然后再kv.toString()得到"_index"字符串，还不如直接将"_index"传进去AggregationBuilders.count(field.getAlias()).field("_index"); 其实目的是为了改变形参MethodField field的params参数中的值，由"*"改为"_index" */ return AggregationBuilders . count ( field . getAlias ( ) ) . field ( kvValue . toString ( ) ) ; } else { return AggregationBuilders . count ( field . getAlias ( ) ) . field ( fieldName ) ; } }
Create count aggregation .