Search in sources :

Example 16 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ChildrenIT method testParentWithMultipleBuckets.

public void testParentWithMultipleBuckets() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", false)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(topHits("top_comments").sort("_uid", SortOrder.ASC)))).get();
    assertSearchResponse(searchResponse);
    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(3));
    for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
        logger.info("bucket={}", bucket.getKey());
        Children childrenBucket = bucket.getAggregations().get("to_comment");
        TopHits topHits = childrenBucket.getAggregations().get("top_comments");
        logger.info("total_hits={}", topHits.getHits().getTotalHits());
        for (SearchHit searchHit : topHits.getHits()) {
            logger.info("hit= {} {} {}", searchHit.getSortValues()[0], searchHit.getType(), searchHit.getId());
        }
    }
    Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
    assertThat(categoryBucket.getKeyAsString(), equalTo("a"));
    assertThat(categoryBucket.getDocCount(), equalTo(3L));
    Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(2L));
    TopHits topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(2L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("b");
    assertThat(categoryBucket.getKeyAsString(), equalTo("b"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("c");
    assertThat(categoryBucket.getKeyAsString(), equalTo("c"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ChildrenIT method testHierarchicalChildrenAggs.

public void testHierarchicalChildrenAggs() {
    String indexName = "geo";
    String grandParentType = "continent";
    String parentType = "country";
    String childType = "city";
    assertAcked(prepareCreate(indexName).setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).addMapping(grandParentType, "name", "type=keyword").addMapping(parentType, "_parent", "type=" + grandParentType).addMapping(childType, "_parent", "type=" + parentType));
    client().prepareIndex(indexName, grandParentType, "1").setSource("name", "europe").get();
    client().prepareIndex(indexName, parentType, "2").setParent("1").setSource("name", "belgium").get();
    client().prepareIndex(indexName, childType, "3").setParent("2").setRouting("1").setSource("name", "brussels").get();
    refresh();
    SearchResponse response = client().prepareSearch(indexName).setQuery(matchQuery("name", "europe")).addAggregation(children(parentType, parentType).subAggregation(children(childType, childType).subAggregation(terms("name").field("name")))).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    Children children = response.getAggregations().get(parentType);
    assertThat(children.getName(), equalTo(parentType));
    assertThat(children.getDocCount(), equalTo(1L));
    children = children.getAggregations().get(childType);
    assertThat(children.getName(), equalTo(childType));
    assertThat(children.getDocCount(), equalTo(1L));
    Terms terms = children.getAggregations().get("name");
    assertThat(terms.getBuckets().size(), equalTo(1));
    assertThat(terms.getBuckets().get(0).getKey().toString(), equalTo("brussels"));
    assertThat(terms.getBuckets().get(0).getDocCount(), equalTo(1L));
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ChildrenIT method testPostCollectAllLeafReaders.

public void testPostCollectAllLeafReaders() throws Exception {
    // The 'towns' and 'parent_names' aggs operate on parent docs and if child docs are in different segments we need
    // to ensure those segments which child docs are also evaluated to in the post collect phase.
    // Before we only evaluated segments that yielded matches in 'towns' and 'parent_names' aggs, which caused
    // us to miss to evaluate child docs in segments we didn't have parent matches for.
    assertAcked(prepareCreate("index").addMapping("parentType", "name", "type=keyword", "town", "type=keyword").addMapping("childType", "_parent", "type=parentType", "name", "type=keyword", "age", "type=integer"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("index", "parentType", "1").setSource("name", "Bob", "town", "Memphis"));
    requests.add(client().prepareIndex("index", "parentType", "2").setSource("name", "Alice", "town", "Chicago"));
    requests.add(client().prepareIndex("index", "parentType", "3").setSource("name", "Bill", "town", "Chicago"));
    requests.add(client().prepareIndex("index", "childType", "1").setSource("name", "Jill", "age", 5).setParent("1"));
    requests.add(client().prepareIndex("index", "childType", "2").setSource("name", "Joey", "age", 3).setParent("1"));
    requests.add(client().prepareIndex("index", "childType", "3").setSource("name", "John", "age", 2).setParent("2"));
    requests.add(client().prepareIndex("index", "childType", "4").setSource("name", "Betty", "age", 6).setParent("3"));
    requests.add(client().prepareIndex("index", "childType", "5").setSource("name", "Dan", "age", 1).setParent("3"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("index").setSize(0).addAggregation(AggregationBuilders.terms("towns").field("town").subAggregation(AggregationBuilders.terms("parent_names").field("name").subAggregation(AggregationBuilders.children("child_docs", "childType")))).get();
    Terms towns = response.getAggregations().get("towns");
    assertThat(towns.getBuckets().size(), equalTo(2));
    assertThat(towns.getBuckets().get(0).getKeyAsString(), equalTo("Chicago"));
    assertThat(towns.getBuckets().get(0).getDocCount(), equalTo(2L));
    Terms parents = towns.getBuckets().get(0).getAggregations().get("parent_names");
    assertThat(parents.getBuckets().size(), equalTo(2));
    assertThat(parents.getBuckets().get(0).getKeyAsString(), equalTo("Alice"));
    assertThat(parents.getBuckets().get(0).getDocCount(), equalTo(1L));
    Children children = parents.getBuckets().get(0).getAggregations().get("child_docs");
    assertThat(children.getDocCount(), equalTo(1L));
    assertThat(parents.getBuckets().get(1).getKeyAsString(), equalTo("Bill"));
    assertThat(parents.getBuckets().get(1).getDocCount(), equalTo(1L));
    children = parents.getBuckets().get(1).getAggregations().get("child_docs");
    assertThat(children.getDocCount(), equalTo(2L));
    assertThat(towns.getBuckets().get(1).getKeyAsString(), equalTo("Memphis"));
    assertThat(towns.getBuckets().get(1).getDocCount(), equalTo(1L));
    parents = towns.getBuckets().get(1).getAggregations().get("parent_names");
    assertThat(parents.getBuckets().size(), equalTo(1));
    assertThat(parents.getBuckets().get(0).getKeyAsString(), equalTo("Bob"));
    assertThat(parents.getBuckets().get(0).getDocCount(), equalTo(1L));
    children = parents.getBuckets().get(0).getAggregations().get("child_docs");
    assertThat(children.getDocCount(), equalTo(2L));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class DiversifiedSamplerIT method testNestedSamples.

public void testNestedSamples() throws Exception {
    // Test samples nested under samples
    int MAX_DOCS_PER_AUTHOR = 1;
    int MAX_DOCS_PER_GENRE = 2;
    DiversifiedAggregationBuilder rootSample = new DiversifiedAggregationBuilder("genreSample").shardSize(100).field("genre").maxDocsPerValue(MAX_DOCS_PER_GENRE);
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    sampleAgg.subAggregation(terms("genres").field("genre"));
    rootSample.subAggregation(sampleAgg);
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(rootSample).execute().actionGet();
    assertSearchResponse(response);
    Sampler genreSample = response.getAggregations().get("genreSample");
    Sampler sample = genreSample.getAggregations().get("sample");
    Terms genres = sample.getAggregations().get("genres");
    Collection<Bucket> testBuckets = genres.getBuckets();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_GENRE));
    }
    Terms authors = sample.getAggregations().get("authors");
    testBuckets = authors.getBuckets();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class DiversifiedSamplerIT method testSimpleDiversity.

public void testSimpleDiversity() throws Exception {
    int MAX_DOCS_PER_AUTHOR = 1;
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    Terms authors = sample.getAggregations().get("authors");
    Collection<Bucket> testBuckets = authors.getBuckets();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)300 SearchResponse (org.elasticsearch.action.search.SearchResponse)268 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)100 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)38 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)26 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)24 Script (org.elasticsearch.script.Script)22 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)16 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 Aggregation (org.elasticsearch.search.aggregations.Aggregation)14 SearchHits (org.elasticsearch.search.SearchHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13