Search in sources :

Example 6 with AdjacencyMatrix

use of org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix in project elasticsearch by elastic.

the class AdjacencyMatrixIT method testSimple.

public void testSimple() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")))).execute().actionGet();
    assertSearchResponse(response);
    AdjacencyMatrix matrix = response.getAggregations().get("tags");
    assertThat(matrix, notNullValue());
    assertThat(matrix.getName(), equalTo("tags"));
    int expected = numMultiTagDocs > 0 ? 3 : 2;
    assertThat(matrix.getBuckets().size(), equalTo(expected));
    AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
    bucket = matrix.getBucketByKey("tag2");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
    bucket = matrix.getBucketByKey("tag1&tag2");
    if (numMultiTagDocs == 0) {
        assertThat(bucket, Matchers.nullValue());
    } else {
        assertThat(bucket, Matchers.notNullValue());
        assertThat(bucket.getDocCount(), equalTo((long) numMultiTagDocs));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) AdjacencyMatrix(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)6 AdjacencyMatrix (org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix)6 Bucket (org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)1