org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder Java Examples
The following examples show how to use
org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchSearchDao.java From metron with Apache License 2.0 | 6 votes |
private TermsAggregationBuilder getGroupsTermBuilder(GroupRequest groupRequest, int index) { List<Group> groups = groupRequest.getGroups(); Group group = groups.get(index); String aggregationName = getGroupByAggregationName(group.getField()); TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(aggregationName); termsBuilder .field(group.getField()) .size(accessConfig.getMaxSearchGroups()) .order(getElasticsearchGroupOrder(group.getOrder())); if (index < groups.size() - 1) { termsBuilder.subAggregation(getGroupsTermBuilder(groupRequest, index + 1)); } Optional<String> scoreField = groupRequest.getScoreField(); if (scoreField.isPresent()) { SumAggregationBuilder scoreSumAggregationBuilder = AggregationBuilders.sum(getSumAggregationName(scoreField.get())).field(scoreField.get()).missing(0); termsBuilder.subAggregation(scoreSumAggregationBuilder); } return termsBuilder; }
Example #2
Source File: TBRDaoESImp.java From blue-marlin with Apache License 2.0 | 5 votes |
/** * This method returns the aggregation value of tbr documents. * * @param boolQueryBuilder * @return * @throws IOException * @throws JSONException */ private double getSumForQuery(BoolQueryBuilder boolQueryBuilder) throws IOException { SearchRequest searchRequest; ESResponse esResponse; double ratio = 0; SumAggregationBuilder aggregationBuilders = new SumAggregationBuilder("sum").script( new Script(genericScriptReader(Paths.get(TBR_KEYS_TOTAL)))); searchRequest = new SearchRequest(tbrIndex).source( new SearchSourceBuilder().query(boolQueryBuilder).size(0). aggregation(aggregationBuilders)); esResponse = esclient.search(searchRequest); Map<String, Object> map = esResponse.getSourceMap(); if (map.containsKey("value")) { ratio = Double.parseDouble(map.get("value").toString()); } else { LOGGER.warn("Result of getSumForQuery() does not contain key \"value!\""); } return ratio; }
Example #3
Source File: SumAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ // QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "weather_hourforcast"); SumAggregationBuilder aggregationBuilder = AggregationBuilders.sum("utm").field("utm"); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // searchSourceBuilder.query(matchQueryBuilder); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); //统计结果 Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){ System.out.println(((ParsedSum)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example #4
Source File: ElasticsearchAggregate.java From dk-fitting with Apache License 2.0 | 4 votes |
public void implement(Implementor implementor) { implementor.visitChild(0, getInput()); ElasticsearchTable esTable = implementor.getElasticsearchTable(); List<AggregateCall> aggCallList = getAggCallList(); List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList(); final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType()); final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType()); esTable.setOutNames(outNames); // final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType()); TermsAggregationBuilder groupAggregationBuilder = null; AggregationBuilder groupAggregationBuilder2 = null; //防止出现group但没有sort出错的情况 for(int i = 0 ;i<groupSet.cardinality();i++) { final String inName = inNames.get(groupSet.nth(i)); groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList)); groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList)); esTable.setIsGroup(true); } List<String> out = new ArrayList<String>(); //聚合函数的名称 for(AggregateCall call : aggCallList) { SqlAggFunction function = call.getAggregation(); List<Integer> argList = call.getArgList(); String functionName = function.getName(); out.add(functionName); switch (function.getKind()) { case MIN: RelDataTypeField typeField = fieldList.get(argList.get(0)); if (groupAggregationBuilder == null) { //min值 与 原字段值 的类型是一样的 esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()), ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass()); }else { MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()); // esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass()); groupAggregationBuilder.subAggregation(minAgg); groupAggregationBuilder2.subAggregation(minAgg); } break; case MAX: //max值 与 原字段值 的类型是一样的 RelDataTypeField typeField1 = fieldList.get(argList.get(0)); if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()), ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass()); }else { groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase())); } break; case COUNT: if(groupAggregationBuilder == null) { if (argList == null || argList.size() == 0)//count(*) esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class); else esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class); }else { groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList))); groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList))); } break; case SUM: if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class); }else { String s = fieldList.get(argList.get(0)).getName().toLowerCase(); SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()); groupAggregationBuilder.subAggregation(sumAgg); groupAggregationBuilder2.subAggregation(sumAgg); } break; case AVG: if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class); }else { AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()); groupAggregationBuilder.subAggregation(avgAgg); groupAggregationBuilder2.subAggregation(avgAgg); } break; default:break; } } if (groupAggregationBuilder != null) { esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class); esTable.addAggregationBuilderList2(groupAggregationBuilder); } esTable.setOut(out); }
Example #5
Source File: ElasticsearchAggregate.java From dk-fitting with Apache License 2.0 | 4 votes |
public void implement(Implementor implementor) { implementor.visitChild(0, getInput()); ElasticsearchTable esTable = implementor.getElasticsearchTable(); List<AggregateCall> aggCallList = getAggCallList(); List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList(); final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType()); final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType()); esTable.setOutNames(outNames); // final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType()); TermsAggregationBuilder groupAggregationBuilder = null; AggregationBuilder groupAggregationBuilder2 = null; //防止出现group但没有sort出错的情况 for(int i = 0 ;i<groupSet.cardinality();i++) { final String inName = inNames.get(groupSet.nth(i)); groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList)); groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList)); esTable.setIsGroup(true); } List<String> out = new ArrayList<String>(); //聚合函数的名称 for(AggregateCall call : aggCallList) { SqlAggFunction function = call.getAggregation(); List<Integer> argList = call.getArgList(); String functionName = function.getName(); out.add(functionName); switch (function.getKind()) { case MIN: RelDataTypeField typeField = fieldList.get(argList.get(0)); if (groupAggregationBuilder == null) { //min值 与 原字段值 的类型是一样的 esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()), ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass()); }else { MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()); // esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass()); groupAggregationBuilder.subAggregation(minAgg); groupAggregationBuilder2.subAggregation(minAgg); } break; case MAX: //max值 与 原字段值 的类型是一样的 RelDataTypeField typeField1 = fieldList.get(argList.get(0)); if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()), ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass()); }else { groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase())); } break; case COUNT: if(groupAggregationBuilder == null) { if (argList == null || argList.size() == 0)//count(*) esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class); else esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class); }else { groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList))); groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList))); } break; case SUM: if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class); }else { String s = fieldList.get(argList.get(0)).getName().toLowerCase(); SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()); groupAggregationBuilder.subAggregation(sumAgg); groupAggregationBuilder2.subAggregation(sumAgg); } break; case AVG: if(groupAggregationBuilder == null) { esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class); }else { AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()); groupAggregationBuilder.subAggregation(avgAgg); groupAggregationBuilder2.subAggregation(avgAgg); } break; default:break; } } if (groupAggregationBuilder != null) { esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class); esTable.addAggregationBuilderList2(groupAggregationBuilder); } esTable.setOut(out); }