use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.
the class InternalMatrixStatsTests method testReduceRandom.
@Override
public void testReduceRandom() {
int numValues = 10000;
int numShards = randomIntBetween(1, 20);
int valuesPerShard = (int) Math.floor(numValues / numShards);
List<Double> aValues = new ArrayList<>();
List<Double> bValues = new ArrayList<>();
RunningStats runningStats = new RunningStats();
List<InternalAggregation> shardResults = new ArrayList<>();
int valuePerShardCounter = 0;
for (int i = 0; i < numValues; i++) {
double valueA = randomDouble();
aValues.add(valueA);
double valueB = randomDouble();
bValues.add(valueB);
runningStats.add(new String[] { "a", "b" }, new double[] { valueA, valueB });
if (++valuePerShardCounter == valuesPerShard) {
shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
runningStats = new RunningStats();
valuePerShardCounter = 0;
}
}
if (valuePerShardCounter != 0) {
shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
}
MultiPassStats multiPassStats = new MultiPassStats("a", "b");
multiPassStats.computeStats(aValues, bValues);
ScriptService mockScriptService = mockScriptService();
MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, b -> {
}, PipelineTree.EMPTY);
InternalMatrixStats reduced = (InternalMatrixStats) shardResults.get(0).reduce(shardResults, context);
multiPassStats.assertNearlyEqual(reduced.getResults());
}
use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.
the class GlobalIT method testWithStatsSubAggregator.
public void testWithStatsSubAggregator() throws Exception {
SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("tag", "tag1")).addAggregation(global("global").subAggregation(stats("value_stats").field("value"))).get();
assertSearchResponse(response);
Global global = response.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
assertThat(global.getDocCount(), equalTo((long) numDocs));
assertThat((long) ((InternalAggregation) global).getProperty("_count"), equalTo((long) numDocs));
assertThat(global.getAggregations().asList().isEmpty(), is(false));
Stats stats = global.getAggregations().get("value_stats");
assertThat((Stats) ((InternalAggregation) global).getProperty("value_stats"), sameInstance(stats));
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("value_stats"));
long sum = 0;
for (int i = 0; i < numDocs; ++i) {
sum += i + 1;
}
assertThat(stats.getAvg(), equalTo((double) sum / numDocs));
assertThat(stats.getMin(), equalTo(1.0));
assertThat(stats.getMax(), equalTo((double) numDocs));
assertThat(stats.getCount(), equalTo((long) numDocs));
assertThat(stats.getSum(), equalTo((double) sum));
}
use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.
the class FilterIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(filter("tag1", termQuery("tag", "tag1")).subAggregation(avg("avg_value").field("value"))).get();
assertSearchResponse(response);
Filter filter = response.getAggregations().get("tag1");
assertThat(filter, notNullValue());
assertThat(filter.getName(), equalTo("tag1"));
assertThat(filter.getDocCount(), equalTo((long) numTag1Docs));
assertThat((long) ((InternalAggregation) filter).getProperty("_count"), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numTag1Docs; ++i) {
sum += i + 1;
}
assertThat(filter.getAggregations().asList().isEmpty(), is(false));
Avg avgValue = filter.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs));
assertThat((double) ((InternalAggregation) filter).getProperty("avg_value.value"), equalTo((double) sum / numTag1Docs));
}
use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.
the class GeoBoundsIT method testSingleValuedField_getProperty.
public void testSingleValuedField_getProperty() throws Exception {
SearchResponse searchResponse = client().prepareSearch(IDX_NAME).setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(geoBounds(aggName).field(SINGLE_VALUED_FIELD_NAME).wrapLongitude(false))).get();
assertSearchResponse(searchResponse);
Global global = searchResponse.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
assertThat(global.getDocCount(), equalTo((long) numDocs));
assertThat(global.getAggregations(), notNullValue());
assertThat(global.getAggregations().asMap().size(), equalTo(1));
GeoBounds geobounds = global.getAggregations().get(aggName);
assertThat(geobounds, notNullValue());
assertThat(geobounds.getName(), equalTo(aggName));
assertThat((GeoBounds) ((InternalAggregation) global).getProperty(aggName), sameInstance(geobounds));
GeoPoint topLeft = geobounds.topLeft();
GeoPoint bottomRight = geobounds.bottomRight();
assertThat(topLeft.lat(), closeTo(singleTopLeft.lat(), GEOHASH_TOLERANCE));
assertThat(topLeft.lon(), closeTo(singleTopLeft.lon(), GEOHASH_TOLERANCE));
assertThat(bottomRight.lat(), closeTo(singleBottomRight.lat(), GEOHASH_TOLERANCE));
assertThat(bottomRight.lon(), closeTo(singleBottomRight.lon(), GEOHASH_TOLERANCE));
assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".top"), closeTo(singleTopLeft.lat(), GEOHASH_TOLERANCE));
assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".left"), closeTo(singleTopLeft.lon(), GEOHASH_TOLERANCE));
assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".bottom"), closeTo(singleBottomRight.lat(), GEOHASH_TOLERANCE));
assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".right"), closeTo(singleBottomRight.lon(), GEOHASH_TOLERANCE));
}
use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.
the class CardinalityIT method testSingleValuedNumericGetProperty.
public void testSingleValuedNumericGetProperty() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(cardinality("cardinality").precisionThreshold(precisionThreshold).field(singleNumericField()))).get();
assertSearchResponse(searchResponse);
Global global = searchResponse.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
// assertThat(global.getDocCount(), equalTo(numDocs));
assertThat(global.getAggregations(), notNullValue());
assertThat(global.getAggregations().asMap().size(), equalTo(1));
Cardinality cardinality = global.getAggregations().get("cardinality");
assertThat(cardinality, notNullValue());
assertThat(cardinality.getName(), equalTo("cardinality"));
long expectedValue = numDocs;
assertCount(cardinality, expectedValue);
assertThat(((InternalAggregation) global).getProperty("cardinality"), equalTo(cardinality));
assertThat(((InternalAggregation) global).getProperty("cardinality.value"), equalTo((double) cardinality.getValue()));
assertThat((double) ((InternalAggregation) cardinality).getProperty("value"), equalTo((double) cardinality.getValue()));
}
Aggregations