同时使用 group by、order by、limit 三个操作得出的结果并不是预期的
SQL:SELECT correspond_brand_name, SUM(store_count) AS total_store FROM es_car_store_query WHERE current_year = 2017 AND week='42' GROUP BY terms(field='correspond_brand_name',size='200',alias='correspond_brand_name') order by total_store DESC LIMIT 0,10
explain: { "from" : 0, "size" : 0, "query" : { "bool" : { "filter" : [ { "bool" : { "must" : [ { "bool" : { "must" : [ { "match_phrase" : { "current_year" : { "query" : 2017, "slop" : 0, "boost" : 1.0 } } }, { "match_phrase" : { "week" : { "query" : "42", "slop" : 0, "boost" : 1.0 } } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : { "includes" : [ "correspond_brand_name", "SUM" ], "excludes" : [ ] }, "stored_fields" : "correspond_brand_name", "aggregations" : { "correspond_brand_name" : { "terms" : { "field" : "correspond_brand_name", "size" : 200, "min_doc_count" : 1, "shard_min_doc_count" : 0, "show_term_doc_count_error" : false, "order" : [ { "total_store" : "desc" }, { "_term" : "asc" } ] }, "aggregations" : { "total_store" : { "sum" : { "field" : "store_count" } } } } } } 似乎这里的LIMIT并没有起作用,因为 {"from" : 0,"size" : 0}。于是将SQL修改为以下写法: SELECT correspond_brand_name, SUM(store_count) AS total_store FROM es_car_store_query WHERE current_year = 2017 AND week='42' GROUP BY correspond_brand_name order by total_store DESC LIMIT 0,10 explain: { "from" : 0, "size" : 0, "query" : { "bool" : { "filter" : [ { "bool" : { "must" : [ { "bool" : { "must" : [ { "match_phrase" : { "current_year" : { "query" : 2017, "slop" : 0, "boost" : 1.0 } } }, { "match_phrase" : { "week" : { "query" : "42", "slop" : 0, "boost" : 1.0 } } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : { "includes" : [ "correspond_brand_name", "SUM" ], "excludes" : [ ] }, "stored_fields" : "correspond_brand_name", "aggregations" : { "correspond_brand_name" : { "terms" : { "field" : "correspond_brand_name", "size" : 10, "shard_size" : 2000, "min_doc_count" : 1, "shard_min_doc_count" : 0, "show_term_doc_count_error" : false, "order" : [ { "total_store" : "desc" }, { "_term" : "asc" } ] }, "aggregations" : { "total_store" : { "sum" : { "field" : "store_count" } } } } } } 可以发现limit作用到了被group by的correspond_brand_name字段上,from和zise字段仍然没有变化:{"from" : 0,"size" : 0}。
我的用意是使用 correspond_brand_name 字段分组,求 store_count 字段的和,并按照所求的和倒序排列一下。然后在所有的结果集中取前10条。
同时,我也测试了没有 group by操作的时候,这时候是符合我的预期结果的: SQL:SELECT correspond_brand_name FROM es_car_store_query WHERE current_year = 2017 AND week='42' LIMIT 0,10 explain: { "from" : 0, "size" : 10, "query" : { "bool" : { "filter" : [ { "bool" : { "must" : [ { "bool" : { "must" : [ { "match_phrase" : { "current_year" : { "query" : 2017, "slop" : 0, "boost" : 1.0 } } }, { "match_phrase" : { "week" : { "query" : "42", "slop" : 0, "boost" : 1.0 } } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } } ], "disable_coord" : false, "adjust_pure_negative" : true, "boost" : 1.0 } @ }, "_source" : { "includes" : [ "correspond_brand_name" ], "excludes" : [ ] } } @shi-yuan 麻烦看看是我写的问题呢,还是bug呢?有没有建议。万分感谢!
SELECT correspond_brand_name, SUM(store_count) AS total_store FROM es_car_store_query WHERE current_year = 2017 AND week='42' GROUP BY terms(field='correspond_brand_name',size='10',alias='correspond_brand_name') ORDER BY total_store DESC
terms(field='correspond_brand_name',size='10',alias='correspond_brand_name') 这样的写法可能会导致统计的数据不完整 @shi-yuan
terms agg的分页,目前是不支持的,这个需要在client端做的,如果担心返回的数据量大,可以借用_filtering_values_with_partitions取到所有
Hi, is it possible to add the parameter "partition" to the terms function?
SELECT * FROM index GROUP BY terms(field='correspond_brand_name',size='10',alias='correspond_brand_name',include='{"partition":0,"num_partitions":20}')