Search in sources :

Example 1 with Nested

use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleNested1ToRootToNested2.

public void testSimpleNested1ToRootToNested2() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1").subAggregation(reverseNested("nested1_to_root").subAggregation(nested("root_to_nested2", "nested1.nested2")))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(9L));
    ReverseNested reverseNested = nested.getAggregations().get("nested1_to_root");
    assertThat(reverseNested.getName(), equalTo("nested1_to_root"));
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    nested = reverseNested.getAggregations().get("root_to_nested2");
    assertThat(nested.getName(), equalTo("root_to_nested2"));
    assertThat(nested.getDocCount(), equalTo(27L));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Nested

use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleReverseNestedToNested1.

public void testSimpleReverseNestedToNested1() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1.nested2").subAggregation(terms("field2").field("nested1.nested2.field2").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())).size(10000).subAggregation(reverseNested("nested1_to_field1").path("nested1").subAggregation(terms("field1").field("nested1.field1").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())))))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(27L));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    Terms usernames = nested.getAggregations().get("field2");
    assertThat(usernames, notNullValue());
    assertThat(usernames.getBuckets().size(), equalTo(5));
    List<Terms.Bucket> usernameBuckets = new ArrayList<>(usernames.getBuckets());
    Terms.Bucket bucket = usernameBuckets.get(0);
    assertThat(bucket.getKeyAsString(), equalTo("0"));
    assertThat(bucket.getDocCount(), equalTo(12L));
    ReverseNested reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(5L));
    Terms tags = reverseNested.getAggregations().get("field1");
    List<Terms.Bucket> tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(3L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    bucket = usernameBuckets.get(1);
    assertThat(bucket.getKeyAsString(), equalTo("1"));
    assertThat(bucket.getDocCount(), equalTo(6L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(2);
    assertThat(bucket.getKeyAsString(), equalTo("2"));
    assertThat(bucket.getDocCount(), equalTo(5L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(3);
    assertThat(bucket.getKeyAsString(), equalTo("3"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
    bucket = usernameBuckets.get(4);
    assertThat(bucket.getKeyAsString(), equalTo("4"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Nested

use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.

the class NestedIT method testNestedWithSubTermsAgg.

public void testNestedWithSubTermsAgg() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(nested("nested", "nested").subAggregation(terms("values").field("nested.value").size(100).collectMode(aggCollectionMode))).execute().actionGet();
    assertSearchResponse(response);
    long docCount = 0;
    long[] counts = new long[numParents + 6];
    for (int i = 0; i < numParents; ++i) {
        for (int j = 0; j < numChildren[i]; ++j) {
            final int value = i + 1 + j;
            ++counts[value];
            ++docCount;
        }
    }
    int uniqueValues = 0;
    for (long count : counts) {
        if (count > 0) {
            ++uniqueValues;
        }
    }
    Nested nested = response.getAggregations().get("nested");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested"));
    assertThat(nested.getDocCount(), equalTo(docCount));
    assertThat(nested.getProperty("_count"), equalTo(docCount));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    LongTerms values = nested.getAggregations().get("values");
    assertThat(values, notNullValue());
    assertThat(values.getName(), equalTo("values"));
    assertThat(values.getBuckets(), notNullValue());
    assertThat(values.getBuckets().size(), equalTo(uniqueValues));
    for (int i = 0; i < counts.length; ++i) {
        final String key = Long.toString(i);
        if (counts[i] == 0) {
            assertNull(values.getBucketByKey(key));
        } else {
            Bucket bucket = values.getBucketByKey(key);
            assertNotNull(bucket);
            assertEquals(counts[i], bucket.getDocCount());
        }
    }
    assertThat(nested.getProperty("values"), sameInstance(values));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) LongTerms(org.elasticsearch.search.aggregations.bucket.terms.LongTerms) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with Nested

use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.

the class NestedIT method testNonExistingNestedField.

public void testNonExistingNestedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").addAggregation(nested("nested", "value").subAggregation(stats("nested_value_stats").field("nested.value"))).execute().actionGet();
    Nested nested = searchResponse.getAggregations().get("nested");
    assertThat(nested, Matchers.notNullValue());
    assertThat(nested.getName(), equalTo("nested"));
    assertThat(nested.getDocCount(), is(0L));
}
Also used : Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with Nested

use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.

the class NestedIT method testNestedSameDocIdProcessedMultipleTime.

public void testNestedSameDocIdProcessedMultipleTime() throws Exception {
    assertAcked(prepareCreate("idx4").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("product", "categories", "type=keyword", "name", "type=text", "property", "type=nested"));
    ensureGreen("idx4");
    client().prepareIndex("idx4", "product", "1").setSource(jsonBuilder().startObject().field("name", "product1").array("categories", "1", "2", "3", "4").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 2).endObject().startObject().field("id", 3).endObject().endArray().endObject()).get();
    client().prepareIndex("idx4", "product", "2").setSource(jsonBuilder().startObject().field("name", "product2").array("categories", "1", "2").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 5).endObject().startObject().field("id", 4).endObject().endArray().endObject()).get();
    refresh();
    SearchResponse response = client().prepareSearch("idx4").setTypes("product").addAggregation(terms("category").field("categories").subAggregation(nested("property", "property").subAggregation(terms("property_id").field("property.id")))).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    Terms category = response.getAggregations().get("category");
    assertThat(category.getBuckets().size(), equalTo(4));
    Terms.Bucket bucket = category.getBucketByKey("1");
    assertThat(bucket.getDocCount(), equalTo(2L));
    Nested property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    Terms propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("2");
    assertThat(bucket.getDocCount(), equalTo(2L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("3");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("4");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) LongTerms(org.elasticsearch.search.aggregations.bucket.terms.LongTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)19 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)19 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)8 LongTerms (org.elasticsearch.search.aggregations.bucket.terms.LongTerms)6 AggregationBuilders.reverseNested (org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested)5 ReverseNested (org.elasticsearch.search.aggregations.bucket.nested.ReverseNested)5 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)5 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)4 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)4 ArrayList (java.util.ArrayList)3 SearchHits (org.elasticsearch.search.SearchHits)3 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)3 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 SearchHitField (org.elasticsearch.search.SearchHitField)2 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)2 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)2 Explanation (org.apache.lucene.search.Explanation)1