这是我的java代码和es代码,先通过contentid,name,level1_name分组,然后对flow,flow_mobile,flow_wifi,avg_flow_mobile,4个字段求和,想知道怎么对flow排序,按照网上查找的资料都是在只有一个分组字段下对下属求和字段排序才会有效,但是对多个字段下的求和字段值就失效了,我想知道有什么方式能对我上面的flow进行排序呢?
searchRequestBuilder.addAggregation(AggregationBuilders.terms("contentId").field("content_id")
.subAggregation(AggregationBuilders.terms("contentName").field("content_name")
.subAggregation(AggregationBuilders.terms("level1Name").field("level1_name").size(10).order(Terms.Order.aggregation("flow", false))
.subAggregation(AggregationBuilders.sum("flow").field("flow"))
.subAggregation(AggregationBuilders.sum("flowMobile").field("flow_mobile"))
.subAggregation(AggregationBuilders.sum("flowWifi").field("flow_wifi"))
.subAggregation(AggregationBuilders.sum("avgFlowMobile").field("avg_flow_mobile"))
)
)
);
上面我按照flow排序,由于多个group by,排序失效了
这是es代码:
{
"size" : 0,
"query" : {
"bool" : {
"must" : [
{
"term" : {
"province" : {
"value" : "10100",
"boost" : 1.0
}
}
},
{
"range" : {
"dayid" : {
"from" : "20190224",
"to" : "20190224",
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}
}
}
],
"must_not" : [
{
"term" : {
"city" : {
"value" : "0001",
"boost" : 1.0
}
}
},
{
"term" : {
"content_id" : {
"value" : "c0001",
"boost" : 1.0
}
}
},
{
"term" : {
"content_name" : {
"value" : "ignore",
"boost" : 1.0
}
}
},
{
"term" : {
"level1_name" : {
"value" : "ignore",
"boost" : 1.0
}
}
},
{
"term" : {
"flow" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"flow_mobile" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"flow_wifi" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"avg_flow_mobile" : {
"value" : "-2",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"aggregations" : {
"contentId" : {
"terms" : {
"field" : "content_id",
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false
},
"aggregations" : {
"contentName" : {
"terms" : {
"field" : "content_name",
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false
},
"aggregations" : {
"level1Name" : {
"terms" : {
"field" : "level1_name",
"size" : 10,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false,
"order": {
"flow": "desc"
}
},
"aggregations" : {
"flow" : {
"sum" : {
"field" : "flow"
}
},
"flowMobile" : {
"sum" : {
"field" : "flow_mobile"
}
},
"flowWifi" : {
"sum" : {
"field" : "flow_wifi"
}
},
"avgFlowMobile" : {
"sum" : {
"field" : "avg_flow_mobile"
}
}
}
}
}
}
}
}
}
}
searchRequestBuilder.addAggregation(AggregationBuilders.terms("contentId").field("content_id")
.subAggregation(AggregationBuilders.terms("contentName").field("content_name")
.subAggregation(AggregationBuilders.terms("level1Name").field("level1_name").size(10).order(Terms.Order.aggregation("flow", false))
.subAggregation(AggregationBuilders.sum("flow").field("flow"))
.subAggregation(AggregationBuilders.sum("flowMobile").field("flow_mobile"))
.subAggregation(AggregationBuilders.sum("flowWifi").field("flow_wifi"))
.subAggregation(AggregationBuilders.sum("avgFlowMobile").field("avg_flow_mobile"))
)
)
);
上面我按照flow排序,由于多个group by,排序失效了
这是es代码:
{
"size" : 0,
"query" : {
"bool" : {
"must" : [
{
"term" : {
"province" : {
"value" : "10100",
"boost" : 1.0
}
}
},
{
"range" : {
"dayid" : {
"from" : "20190224",
"to" : "20190224",
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}
}
}
],
"must_not" : [
{
"term" : {
"city" : {
"value" : "0001",
"boost" : 1.0
}
}
},
{
"term" : {
"content_id" : {
"value" : "c0001",
"boost" : 1.0
}
}
},
{
"term" : {
"content_name" : {
"value" : "ignore",
"boost" : 1.0
}
}
},
{
"term" : {
"level1_name" : {
"value" : "ignore",
"boost" : 1.0
}
}
},
{
"term" : {
"flow" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"flow_mobile" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"flow_wifi" : {
"value" : "-2",
"boost" : 1.0
}
}
},
{
"term" : {
"avg_flow_mobile" : {
"value" : "-2",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"aggregations" : {
"contentId" : {
"terms" : {
"field" : "content_id",
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false
},
"aggregations" : {
"contentName" : {
"terms" : {
"field" : "content_name",
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false
},
"aggregations" : {
"level1Name" : {
"terms" : {
"field" : "level1_name",
"size" : 10,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false,
"order": {
"flow": "desc"
}
},
"aggregations" : {
"flow" : {
"sum" : {
"field" : "flow"
}
},
"flowMobile" : {
"sum" : {
"field" : "flow_mobile"
}
},
"flowWifi" : {
"sum" : {
"field" : "flow_wifi"
}
},
"avgFlowMobile" : {
"sum" : {
"field" : "avg_flow_mobile"
}
}
}
}
}
}
}
}
}
}
0 个回复