|
9 | 9 | package org.elasticsearch.search.aggregations.bucket;
|
10 | 10 |
|
11 | 11 | import org.apache.lucene.search.join.ScoreMode;
|
| 12 | +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; |
12 | 13 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
13 | 14 | import org.elasticsearch.action.search.SearchRequestBuilder;
|
14 | 15 | import org.elasticsearch.index.query.InnerHitBuilder;
|
|
18 | 19 | import org.elasticsearch.search.aggregations.InternalAggregation;
|
19 | 20 | import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
20 | 21 | import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
| 22 | +import org.elasticsearch.search.aggregations.bucket.nested.InternalNested; |
21 | 23 | import org.elasticsearch.search.aggregations.bucket.nested.Nested;
|
| 24 | +import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder; |
22 | 25 | import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
|
23 | 26 | import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
24 | 27 | import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
25 | 28 | import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
|
| 29 | +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; |
| 30 | +import org.elasticsearch.search.aggregations.metrics.CardinalityAggregationBuilder; |
26 | 31 | import org.elasticsearch.search.aggregations.metrics.Max;
|
27 | 32 | import org.elasticsearch.search.aggregations.metrics.Stats;
|
28 | 33 | import org.elasticsearch.search.aggregations.metrics.Sum;
|
@@ -890,4 +895,87 @@ public void testSyntheticSource() throws Exception {
|
890 | 895 | assertEquals("a", nested.get("number"));
|
891 | 896 | });
|
892 | 897 | }
|
| 898 | + |
| 899 | + public void testScoring() throws Exception { |
| 900 | + assertAcked( |
| 901 | + prepareCreate("scoring").setMapping( |
| 902 | + jsonBuilder().startObject() |
| 903 | + .startObject("properties") |
| 904 | + .startObject("tags") |
| 905 | + .field("type", "nested") |
| 906 | + .startObject("properties") |
| 907 | + .startObject("key") |
| 908 | + .field("type", "keyword") |
| 909 | + .endObject() |
| 910 | + .startObject("value") |
| 911 | + .field("type", "keyword") |
| 912 | + .endObject() |
| 913 | + .endObject() |
| 914 | + .endObject() |
| 915 | + .endObject() |
| 916 | + .endObject() |
| 917 | + ) |
| 918 | + ); |
| 919 | + ensureGreen("scoring"); |
| 920 | + |
| 921 | + prepareIndex("scoring").setId("1") |
| 922 | + .setSource( |
| 923 | + jsonBuilder().startObject() |
| 924 | + .startArray("tags") |
| 925 | + .startObject() |
| 926 | + .field("key", "state") |
| 927 | + .field("value", "texas") |
| 928 | + .endObject() |
| 929 | + .endArray() |
| 930 | + .endObject() |
| 931 | + ) |
| 932 | + .get(); |
| 933 | + refresh("scoring"); |
| 934 | + prepareIndex("scoring").setId("2") |
| 935 | + .setSource( |
| 936 | + jsonBuilder().startObject() |
| 937 | + .startArray("tags") |
| 938 | + .startObject() |
| 939 | + .field("key", "state") |
| 940 | + .field("value", "utah") |
| 941 | + .endObject() |
| 942 | + .endArray() |
| 943 | + .endObject() |
| 944 | + ) |
| 945 | + .get(); |
| 946 | + refresh("scoring"); |
| 947 | + prepareIndex("scoring").setId("3") |
| 948 | + .setSource( |
| 949 | + jsonBuilder().startObject() |
| 950 | + .startArray("tags") |
| 951 | + .startObject() |
| 952 | + .field("key", "state") |
| 953 | + .field("value", "texas") |
| 954 | + .endObject() |
| 955 | + .endArray() |
| 956 | + .endObject() |
| 957 | + ) |
| 958 | + .get(); |
| 959 | + refresh("scoring"); |
| 960 | + |
| 961 | + assertResponse( |
| 962 | + client().prepareSearch("scoring") |
| 963 | + .setSize(0) |
| 964 | + .addAggregation( |
| 965 | + new NestedAggregationBuilder("tags", "tags").subAggregation( |
| 966 | + new TermsAggregationBuilder("keys").field("tags.key") |
| 967 | + .executionHint("map") |
| 968 | + .subAggregation(new TermsAggregationBuilder("values").field("tags.value")) |
| 969 | + .subAggregation(new CardinalityAggregationBuilder("values_count").field("tags.value")) |
| 970 | + ) |
| 971 | + ), |
| 972 | + searchResponse -> { |
| 973 | + InternalNested nested = searchResponse.getAggregations().get("tags"); |
| 974 | + assertThat(nested.getDocCount(), equalTo(3L)); |
| 975 | + assertThat(nested.getAggregations().asList().size(), equalTo(1)); |
| 976 | + } |
| 977 | + ); |
| 978 | + |
| 979 | + assertAcked(indicesAdmin().delete(new DeleteIndexRequest("scoring")).get()); |
| 980 | + } |
893 | 981 | }
|
0 commit comments