Search in sources :

Example 1 with Children

use of org.elasticsearch.search.aggregations.bucket.children.Children 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 2 with Children

use of org.elasticsearch.search.aggregations.bucket.children.Children 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 3 with Children

use of org.elasticsearch.search.aggregations.bucket.children.Children 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 4 with Children

use of org.elasticsearch.search.aggregations.bucket.children.Children in project elasticsearch by elastic.

the class ChildrenIT method testNonExistingChildType.

public void testNonExistingChildType() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").addAggregation(children("non-existing", "xyz")).get();
    assertSearchResponse(searchResponse);
    Children children = searchResponse.getAggregations().get("non-existing");
    assertThat(children.getName(), equalTo("non-existing"));
    assertThat(children.getDocCount(), equalTo(0L));
}
Also used : Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with Children

use of org.elasticsearch.search.aggregations.bucket.children.Children in project elasticsearch by elastic.

the class ChildrenIT method testChildrenAggs.

public void testChildrenAggs() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", true)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(terms("commenters").field("commenter").size(10000).subAggregation(topHits("top_comments"))))).get();
    assertSearchResponse(searchResponse);
    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(categoryToControl.size()));
    for (Map.Entry<String, Control> entry1 : categoryToControl.entrySet()) {
        Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry1.getKey());
        assertThat(categoryBucket.getKeyAsString(), equalTo(entry1.getKey()));
        assertThat(categoryBucket.getDocCount(), equalTo((long) entry1.getValue().articleIds.size()));
        Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
        assertThat(childrenBucket.getName(), equalTo("to_comment"));
        assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size()));
        assertThat((long) childrenBucket.getProperty("_count"), equalTo((long) entry1.getValue().commentIds.size()));
        Terms commentersTerms = childrenBucket.getAggregations().get("commenters");
        assertThat((Terms) childrenBucket.getProperty("commenters"), sameInstance(commentersTerms));
        assertThat(commentersTerms.getBuckets().size(), equalTo(entry1.getValue().commenterToCommentId.size()));
        for (Map.Entry<String, Set<String>> entry2 : entry1.getValue().commenterToCommentId.entrySet()) {
            Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey());
            assertThat(commentBucket.getKeyAsString(), equalTo(entry2.getKey()));
            assertThat(commentBucket.getDocCount(), equalTo((long) entry2.getValue().size()));
            TopHits topHits = commentBucket.getAggregations().get("top_comments");
            for (SearchHit searchHit : topHits.getHits().getHits()) {
                assertThat(entry2.getValue().contains(searchHit.getId()), is(true));
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) 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) HashMap(java.util.HashMap) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)7 Children (org.elasticsearch.search.aggregations.bucket.children.Children)7 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 ArrayList (java.util.ArrayList)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 SearchHit (org.elasticsearch.search.SearchHit)2 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)1