/* * 统计每种兴趣下职工的平均年龄 * 聚合也允许分级汇总 */ @Test public void subAggregationTest() { Client client = ClientTemplate.getClient(); SearchResponse response = client .prepareSearch("fccs") .setTypes("employee") .addAggregation( AggregationBuilders.terms("by_interests") .field("interests") .subAggregation(AggregationBuilders.avg("avg_age").field("age"))) .get(); Terms terms = response.getAggregations().get("by_interests"); List<Bucket> buckets = terms.getBuckets(); for (Bucket bucket : buckets) { Avg avg = bucket.getAggregations().get("avg_age"); System.out.println( "key:" + bucket.getKey() + " doc_count:" + bucket.getDocCount() + " avg_age:" + avg.getValue()); } client.close(); }
@Override public void runAggregations() { System.out.println("get aggregations:"); System.out.println("connecting cluster..."); Node node = nodeBuilder().clusterName("ron-es").client(true).node(); Client client = node.client(); AbstractAggregationBuilder maxAgg = AggregationBuilders.max("max-km").field("km"); AbstractAggregationBuilder minAgg = AggregationBuilders.min("min-km").field("km"); AbstractAggregationBuilder avgAgg = AggregationBuilders.avg("avg-km").field("km"); AbstractAggregationBuilder sumAgg = AggregationBuilders.sum("sum-km").field("km"); String startTime = Util.getDateTimeNow(); SearchResponse response = client .prepareSearch("vehicles") .setTypes("cars") .addAggregation(maxAgg) .addAggregation(minAgg) .addAggregation(avgAgg) .addAggregation(sumAgg) .execute() .actionGet(); String stopTime = Util.getDateTimeNow(); _timeElapssed = Util.getDateDiff(startTime, stopTime); // SearchResponse response = // client.prepareSearch("vehicles").setTypes("cars").addAggregation(AggregationBuilders.max("max-km").field("km")).execute().actionGet(); client.close(); System.out.println("node closed"); // SearchResponse response = client.prepareSearch().execute().actionGet(); // Stats agg = response.getAggregations().get("km"); // double max = agg.getMax(); System.out.println(response); }
/** * Create aggregation according to the SQL function. * * @param field SQL function * @param parent parentAggregation * @return AggregationBuilder represents the SQL function * @throws SqlParseException in case of unrecognized function */ public AbstractAggregationBuilder makeFieldAgg( MethodField field, AbstractAggregationBuilder parent) throws SqlParseException { groupMap.put(field.getAlias(), new KVValue("FIELD", parent)); ValuesSourceMetricsAggregationBuilder builder; field.setAlias(fixAlias(field.getAlias())); switch (field.getName().toUpperCase()) { case "SUM": builder = AggregationBuilders.sum(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "MAX": builder = AggregationBuilders.max(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "MIN": builder = AggregationBuilders.min(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "AVG": builder = AggregationBuilders.avg(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "STATS": builder = AggregationBuilders.stats(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "EXTENDED_STATS": builder = AggregationBuilders.extendedStats(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "PERCENTILES": builder = AggregationBuilders.percentiles(field.getAlias()); addSpecificPercentiles((PercentilesBuilder) builder, field.getParams()); return addFieldOrScriptToAggregation(field, builder); case "TOPHITS": return makeTopHitsAgg(field); case "SCRIPTED_METRIC": return scriptedMetric(field); case "COUNT": groupMap.put(field.getAlias(), new KVValue("COUNT", parent)); return makeCountAgg(field); default: throw new SqlParseException("the agg function not to define !"); } }
public static SearchSourceBuilder randomSearchSourceBuilder( Supplier<HighlightBuilder> randomHighlightBuilder, Supplier<SuggestBuilder> randomSuggestBuilder, Supplier<RescoreBuilder<?>> randomRescoreBuilder, Supplier<List<SearchExtBuilder>> randomExtBuilders) { SearchSourceBuilder builder = new SearchSourceBuilder(); if (randomBoolean()) { builder.from(randomIntBetween(0, 10000)); } if (randomBoolean()) { builder.size(randomIntBetween(0, 10000)); } if (randomBoolean()) { builder.explain(randomBoolean()); } if (randomBoolean()) { builder.version(randomBoolean()); } if (randomBoolean()) { builder.trackScores(randomBoolean()); } if (randomBoolean()) { builder.minScore(randomFloat() * 1000); } if (randomBoolean()) { builder.timeout(TimeValue.parseTimeValue(randomTimeValue(), null, "timeout")); } if (randomBoolean()) { builder.terminateAfter(randomIntBetween(1, 100000)); } switch (randomInt(2)) { case 0: builder.storedFields(); break; case 1: builder.storedField("_none_"); break; case 2: int fieldsSize = randomInt(25); List<String> fields = new ArrayList<>(fieldsSize); for (int i = 0; i < fieldsSize; i++) { fields.add(randomAsciiOfLengthBetween(5, 50)); } builder.storedFields(fields); break; default: throw new IllegalStateException(); } if (randomBoolean()) { int scriptFieldsSize = randomInt(25); for (int i = 0; i < scriptFieldsSize; i++) { if (randomBoolean()) { builder.scriptField( randomAsciiOfLengthBetween(5, 50), new Script("foo"), randomBoolean()); } else { builder.scriptField(randomAsciiOfLengthBetween(5, 50), new Script("foo")); } } } if (randomBoolean()) { FetchSourceContext fetchSourceContext; int branch = randomInt(5); String[] includes = new String[randomIntBetween(0, 20)]; for (int i = 0; i < includes.length; i++) { includes[i] = randomAsciiOfLengthBetween(5, 20); } String[] excludes = new String[randomIntBetween(0, 20)]; for (int i = 0; i < excludes.length; i++) { excludes[i] = randomAsciiOfLengthBetween(5, 20); } switch (branch) { case 0: fetchSourceContext = new FetchSourceContext(randomBoolean()); break; case 1: fetchSourceContext = new FetchSourceContext(true, includes, excludes); break; case 2: fetchSourceContext = new FetchSourceContext( true, new String[] {randomAsciiOfLengthBetween(5, 20)}, new String[] {randomAsciiOfLengthBetween(5, 20)}); break; case 3: fetchSourceContext = new FetchSourceContext(true, includes, excludes); break; case 4: fetchSourceContext = new FetchSourceContext(true, includes, null); break; case 5: fetchSourceContext = new FetchSourceContext(true, new String[] {randomAsciiOfLengthBetween(5, 20)}, null); break; default: throw new IllegalStateException(); } builder.fetchSource(fetchSourceContext); } if (randomBoolean()) { int size = randomIntBetween(0, 20); List<String> statsGroups = new ArrayList<>(size); for (int i = 0; i < size; i++) { statsGroups.add(randomAsciiOfLengthBetween(5, 20)); } builder.stats(statsGroups); } if (randomBoolean()) { int indexBoostSize = randomIntBetween(1, 10); for (int i = 0; i < indexBoostSize; i++) { builder.indexBoost(randomAsciiOfLengthBetween(5, 20), randomFloat() * 10); } } if (randomBoolean()) { builder.query( QueryBuilders.termQuery( randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20))); } if (randomBoolean()) { builder.postFilter( QueryBuilders.termQuery( randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20))); } if (randomBoolean()) { int numSorts = randomIntBetween(1, 5); for (int i = 0; i < numSorts; i++) { int branch = randomInt(5); switch (branch) { case 0: builder.sort( SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)) .order(randomFrom(SortOrder.values()))); break; case 1: builder.sort( SortBuilders.geoDistanceSort( randomAsciiOfLengthBetween(5, 20), AbstractQueryTestCase.randomGeohash(1, 12)) .order(randomFrom(SortOrder.values()))); break; case 2: builder.sort(SortBuilders.scoreSort().order(randomFrom(SortOrder.values()))); break; case 3: builder.sort( SortBuilders.scriptSort(new Script("foo"), ScriptSortBuilder.ScriptSortType.NUMBER) .order(randomFrom(SortOrder.values()))); break; case 4: builder.sort(randomAsciiOfLengthBetween(5, 20)); break; case 5: builder.sort(randomAsciiOfLengthBetween(5, 20), randomFrom(SortOrder.values())); break; } } } if (randomBoolean()) { int numSearchFrom = randomIntBetween(1, 5); try { // We build a json version of the search_from first in order to // ensure that every number type remain the same before/after xcontent (de)serialization. // This is not a problem because the final type of each field value is extracted from // associated sort field. // This little trick ensure that equals and hashcode are the same when using the xcontent // serialization. XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); jsonBuilder.startObject(); jsonBuilder.startArray("search_from"); for (int i = 0; i < numSearchFrom; i++) { int branch = randomInt(8); switch (branch) { case 0: jsonBuilder.value(randomInt()); break; case 1: jsonBuilder.value(randomFloat()); break; case 2: jsonBuilder.value(randomLong()); break; case 3: jsonBuilder.value(randomDouble()); break; case 4: jsonBuilder.value(randomAsciiOfLengthBetween(5, 20)); break; case 5: jsonBuilder.value(randomBoolean()); break; case 6: jsonBuilder.value(randomByte()); break; case 7: jsonBuilder.value(randomShort()); break; case 8: jsonBuilder.value(new Text(randomAsciiOfLengthBetween(5, 20))); break; } } jsonBuilder.endArray(); jsonBuilder.endObject(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(jsonBuilder.bytes()); parser.nextToken(); parser.nextToken(); parser.nextToken(); builder.searchAfter(SearchAfterBuilder.fromXContent(parser, null).getSortValues()); } catch (IOException e) { throw new RuntimeException("Error building search_from", e); } } if (randomBoolean()) { builder.highlighter(randomHighlightBuilder.get()); } if (randomBoolean()) { builder.suggest(randomSuggestBuilder.get()); } if (randomBoolean()) { int numRescores = randomIntBetween(1, 5); for (int i = 0; i < numRescores; i++) { builder.addRescorer(randomRescoreBuilder.get()); } } if (randomBoolean()) { builder.aggregation(AggregationBuilders.avg(randomAsciiOfLengthBetween(5, 20))); } if (randomBoolean()) { builder.ext(randomExtBuilders.get()); } if (randomBoolean()) { String field = randomBoolean() ? null : randomAsciiOfLengthBetween(5, 20); int max = between(2, 1000); int id = randomInt(max - 1); if (field == null) { builder.slice(new SliceBuilder(id, max)); } else { builder.slice(new SliceBuilder(field, id, max)); } } return builder; }