use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
BoolQueryBuilder boolQ = new BoolQueryBuilder();
boolQ.must(termQuery("tag", "tag1"));
boolQ.must(termQuery("tag", "tag2"));
SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")).add("both", boolQ)).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix matrix = response.getAggregations().get("tags");
assertThat(matrix, notNullValue());
assertThat(matrix.getName(), equalTo("tags"));
int expectedBuckets = 0;
if (numTag1Docs > 0) {
expectedBuckets++;
}
if (numTag2Docs > 0) {
expectedBuckets++;
}
if (numMultiTagDocs > 0) {
// both, both&tag1, both&tag2, tag1&tag2
expectedBuckets += 4;
}
assertThat(matrix.getBuckets().size(), equalTo(expectedBuckets));
assertThat(matrix.getProperty("_bucket_count"), equalTo(expectedBuckets));
Object[] propertiesKeys = (Object[]) matrix.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) matrix.getProperty("_count");
Object[] propertiesCounts = (Object[]) matrix.getProperty("avg_value.value");
assertEquals(expectedBuckets, propertiesKeys.length);
assertEquals(propertiesKeys.length, propertiesDocCounts.length);
assertEquals(propertiesKeys.length, propertiesCounts.length);
for (int i = 0; i < propertiesCounts.length; i++) {
AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey(propertiesKeys[i].toString());
assertThat(bucket, Matchers.notNullValue());
Avg avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat((long) propertiesDocCounts[i], equalTo(bucket.getDocCount()));
assertThat((double) propertiesCounts[i], equalTo(avgValue.getValue()));
}
AdjacencyMatrix.Bucket tag1Bucket = matrix.getBucketByKey("tag1");
assertThat(tag1Bucket, Matchers.notNullValue());
assertThat(tag1Bucket.getDocCount(), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numSingleTag1Docs; i++) {
sum += i + 1;
}
for (int i = numSingleTag1Docs + numSingleTag2Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag1Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket1Value = tag1Bucket.getAggregations().get("avg_value");
assertThat(avgBucket1Value, notNullValue());
assertThat(avgBucket1Value.getName(), equalTo("avg_value"));
assertThat(avgBucket1Value.getValue(), equalTo((double) sum / numTag1Docs));
Bucket tag2Bucket = matrix.getBucketByKey("tag2");
assertThat(tag2Bucket, Matchers.notNullValue());
assertThat(tag2Bucket.getDocCount(), equalTo((long) numTag2Docs));
sum = 0;
for (int i = numSingleTag1Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag2Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket2Value = tag2Bucket.getAggregations().get("avg_value");
assertThat(avgBucket2Value, notNullValue());
assertThat(avgBucket2Value.getName(), equalTo("avg_value"));
assertThat(avgBucket2Value.getValue(), equalTo((double) sum / numTag2Docs));
// Check intersection buckets are computed correctly by comparing with
// ANDed query bucket results
Bucket bucketBothQ = matrix.getBucketByKey("both");
if (numMultiTagDocs == 0) {
// Empty intersections are not returned.
assertThat(bucketBothQ, Matchers.nullValue());
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.nullValue());
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.nullValue());
} else {
assertThat(bucketBothQ, Matchers.notNullValue());
assertThat(bucketBothQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueBothQ = bucketBothQ.getAggregations().get("avg_value");
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.notNullValue());
assertThat(bucketIntersectQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueIntersectQ = bucketBothQ.getAggregations().get("avg_value");
assertThat(avgValueIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.notNullValue());
assertThat(tag1Both.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueTag1BothIntersectQ = tag1Both.getAggregations().get("avg_value");
assertThat(avgValueTag1BothIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
}
}
use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testEmptyFilterDeclarations.
// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
QueryBuilder emptyFilter = new BoolQueryBuilder();
SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("all", emptyFilter).add("tag1", termQuery("tag", "tag1")))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
AdjacencyMatrix.Bucket allBucket = filters.getBucketByKey("all");
assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
AdjacencyMatrix.Bucket bucket = filters.getBucketByKey("tag1");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testAsSubAggregation.
public void testAsSubAggregation() {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field("value").interval(2L).subAggregation(adjacencyMatrix("matrix", newMap("all", matchAllQuery())))).get();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getBuckets().size(), greaterThanOrEqualTo(1));
for (Histogram.Bucket bucket : histo.getBuckets()) {
AdjacencyMatrix matrix = bucket.getAggregations().get("matrix");
assertThat(matrix, notNullValue());
assertThat(matrix.getBuckets().size(), equalTo(1));
AdjacencyMatrix.Bucket filterBucket = matrix.getBuckets().get(0);
assertEquals(bucket.getDocCount(), filterBucket.getDocCount());
}
}
use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testCustomSeparator.
public void testCustomSeparator() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", "\t", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix matrix = response.getAggregations().get("tags");
assertThat(matrix, notNullValue());
AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey("tag1\ttag2");
if (numMultiTagDocs == 0) {
assertThat(bucket, Matchers.nullValue());
} else {
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numMultiTagDocs));
}
}
use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testEmptyAggregation.
public void testEmptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(adjacencyMatrix("matrix", newMap("all", matchAllQuery())))).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
Histogram histo = searchResponse.getAggregations().get("histo");
assertThat(histo, Matchers.notNullValue());
Histogram.Bucket bucket = histo.getBuckets().get(1);
assertThat(bucket, Matchers.notNullValue());
AdjacencyMatrix matrix = bucket.getAggregations().get("matrix");
assertThat(matrix, notNullValue());
AdjacencyMatrix.Bucket all = matrix.getBucketByKey("all");
assertThat(all, Matchers.nullValue());
}
Aggregations