ES里多字段分组后排序

ES里多字段分组再排序,只有两种办法,其他办法都不准:

办法一:用script

 //主桶:设置要聚合的字段,sql
        TermsBuilder one = AggregationBuilders.terms("one").script(new Script("doc['"+dbFiled+"'].value +'{#}' + doc['"+sqlFiled+"'].value"));

        one.subAggregation(AggregationBuilders.max(queryTime + "Max").field(queryTime));
        one.subAggregation(AggregationBuilders.min(queryTime + "Min").field(queryTime));
        one.subAggregation(AggregationBuilders.avg(queryTime + "Avg").field(queryTime));

        one.order(Terms.Order.aggregation(queryTime + "Max", false));
        one.size(100);

        //构建查询语句
        srb.addAggregation(one);

这种办法是效率低,但灵活。不用冗余字段。

 这种办法需要配置es,如下:配置文件elasticsearch.yml

script.inline:on
script.indexd:on

 配置完后重启服务端,如果你客户端处于连接状态,那也要重启。

办法二:冗余字段

在logstash抓数据的时候,就冗余合并字段,配置略。

猜你喜欢

转载自jdkleo.iteye.com/blog/2389589
今日推荐