这段代码是关于多层聚合和嵌套域的聚合,来源:https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/search/aggregations/bucket/NestedTests.java

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.bucket;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.notNullValue;

/**
*
*/
@ElasticsearchIntegrationTest.SuiteScopeTest
public class NestedTests extends ElasticsearchIntegrationTest {

static int numParents;
static int[] numChildren;
static SubAggCollectionMode aggCollectionMode;

@Override
public void setupSuiteScopeCluster() throws Exception {

assertAcked(prepareCreate("idx")
.addMapping("type", "nested", "type=nested"));

List<IndexRequestBuilder> builders = new ArrayList<>();

numParents = randomIntBetween(3, 10);
numChildren = new int[numParents];
aggCollectionMode = randomFrom(SubAggCollectionMode.values());
logger.info("AGG COLLECTION MODE: " + aggCollectionMode);
int totalChildren = 0;
for (int i = 0; i < numParents; ++i) {
if (i == numParents - 1 && totalChildren == 0) {
// we need at least one child overall
numChildren[i] = randomIntBetween(1, 5);
} else {
numChildren[i] = randomInt(5);
}
totalChildren += numChildren[i];
}
assertTrue(totalChildren > 0);

for (int i = 0; i < numParents; i++) {
XContentBuilder source = jsonBuilder()
.startObject()
.field("value", i + 1)
.startArray("nested");
for (int j = 0; j < numChildren[i]; ++j) {
source = source.startObject().field("value", i + 1 + j).endObject();
}
source = source.endArray().endObject();
builders.add(client().prepareIndex("idx", "type", ""+i+1).setSource(source));
}

prepareCreate("empty_bucket_idx").addMapping("type", "value", "type=integer", "nested", "type=nested").execute().actionGet();
for (int i = 0; i < 2; i++) {
builders.add(client().prepareIndex("empty_bucket_idx", "type", ""+i).setSource(jsonBuilder()
.startObject()
.field("value", i*2)
.startArray("nested")
.startObject().field("value", i + 1).endObject()
.startObject().field("value", i + 2).endObject()
.startObject().field("value", i + 3).endObject()
.startObject().field("value", i + 4).endObject()
.startObject().field("value", i + 5).endObject()
.endArray()
.endObject()));
}

assertAcked(prepareCreate("idx_nested_nested_aggs")
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1")
.field("type", "nested")
.startObject("properties")
.startObject("nested2")
.field("type", "nested")
.endObject()
.endObject()
.endObject()
.endObject().endObject().endObject()));

builders.add(
client().prepareIndex("idx_nested_nested_aggs", "type", "1")
.setSource(jsonBuilder().startObject()
.startArray("nested1")
.startObject()
.field("a", "a")
.startArray("nested2")
.startObject()
.field("b", 2)
.endObject()
.endArray()
.endObject()
.startObject()
.field("a", "b")
.startArray("nested2")
.startObject()
.field("b", 2)
.endObject()
.endArray()
.endObject()
.endArray()
.endObject())
);
indexRandom(true, builders);
ensureSearchable();
}

@Test
public void simple() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(nested("nested").path("nested")
.subAggregation(stats("nested_value_stats").field("nested.value")))
.execute().actionGet();

assertSearchResponse(response);

double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
long sum = 0;
long count = 0;
for (int i = 0; i < numParents; ++i) {
for (int j = 0; j < numChildren[i]; ++j) {
final long value = i + 1 + j;
min = Math.min(min, value);
max = Math.max(max, value);
sum += value;
++count;
}
}

Nested nested = response.getAggregations().get("nested");
assertThat(nested, notNullValue());
assertThat(nested.getName(), equalTo("nested"));
assertThat(nested.getDocCount(), equalTo(count));
assertThat(nested.getAggregations().asList().isEmpty(), is(false));

Stats stats = nested.getAggregations().get("nested_value_stats");
assertThat(stats, notNullValue());
assertThat(stats.getMin(), equalTo(min));
assertThat(stats.getMax(), equalTo(max));
assertThat(stats.getCount(), equalTo(count));
assertThat(stats.getSum(), equalTo((double) sum));
assertThat(stats.getAvg(), equalTo((double) sum / count));
}

@Test
public void onNonNestedField() throws Exception {
try {
client().prepareSearch("idx")
.addAggregation(nested("nested").path("value")
.subAggregation(stats("nested_value_stats").field("nested.value")))
.execute().actionGet();

fail("expected execution to fail - an attempt to nested facet on non-nested field/path");

} catch (ElasticsearchException ese) {
}
}

@Test
public void nestedWithSubTermsAgg() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(nested("nested").path("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.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());
}
}
}

@Test
public void nestedAsSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(terms("top_values").field("value").size(100)
.collectMode(aggCollectionMode)
.subAggregation(nested("nested").path("nested")
.subAggregation(max("max_value").field("nested.value"))))
.execute().actionGet();

assertSearchResponse(response);

LongTerms values = response.getAggregations().get("top_values");
assertThat(values, notNullValue());
assertThat(values.getName(), equalTo("top_values"));
assertThat(values.getBuckets(), notNullValue());
assertThat(values.getBuckets().size(), equalTo(numParents));

for (int i = 0; i < numParents; i++) {
String topValue = "" + (i + 1);
assertThat(values.getBucketByKey(topValue), notNullValue());
Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested");
assertThat(nested, notNullValue());
Max max = nested.getAggregations().get("max_value");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i]));
}
}

@Test
public void nestNestedAggs() throws Exception {
SearchResponse response = client().prepareSearch("idx_nested_nested_aggs")
.addAggregation(nested("level1").path("nested1")
.subAggregation(terms("a").field("nested1.a")
.collectMode(aggCollectionMode)
.subAggregation(nested("level2").path("nested1.nested2")
.subAggregation(sum("sum").field("nested1.nested2.b")))))
.get();
assertSearchResponse(response);

Nested level1 = response.getAggregations().get("level1");
assertThat(level1, notNullValue());
assertThat(level1.getName(), equalTo("level1"));
assertThat(level1.getDocCount(), equalTo(2l));

StringTerms a = level1.getAggregations().get("a");
Terms.Bucket bBucket = a.getBucketByKey("a");
assertThat(bBucket.getDocCount(), equalTo(1l));

Nested level2 = bBucket.getAggregations().get("level2");
assertThat(level2.getDocCount(), equalTo(1l));
Sum sum = level2.getAggregations().get("sum");
assertThat(sum.getValue(), equalTo(2d));

a = level1.getAggregations().get("a");
bBucket = a.getBucketByKey("b");
assertThat(bBucket.getDocCount(), equalTo(1l));

level2 = bBucket.getAggregations().get("level2");
assertThat(level2.getDocCount(), equalTo(1l));
sum = level2.getAggregations().get("sum");
assertThat(sum.getValue(), equalTo(2d));
}

@Test
public void emptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx")
.setQuery(matchAllQuery())
.addAggregation(histogram("histo").field("value").interval(1l).minDocCount(0)
.subAggregation(nested("nested").path("nested")))
.execute().actionGet();

assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
Histogram histo = searchResponse.getAggregations().get("histo");
assertThat(histo, Matchers.notNullValue());
Histogram.Bucket bucket = histo.getBucketByKey(1l);
assertThat(bucket, Matchers.notNullValue());

Nested nested = bucket.getAggregations().get("nested");
assertThat(nested, Matchers.notNullValue());
assertThat(nested.getName(), equalTo("nested"));
assertThat(nested.getDocCount(), is(0l));
}
}

  1 /*2  * Licensed to Elasticsearch under one or more contributor3  * license agreements. See the NOTICE file distributed with4  * this work for additional information regarding copyright5  * ownership. Elasticsearch licenses this file to you under6  * the Apache License, Version 2.0 (the "License"); you may7  * not use this file except in compliance with the License.8  * You may obtain a copy of the License at9  *10  *    http://www.apache.org/licenses/LICENSE-2.011  *12  * Unless required by applicable law or agreed to in writing,13  * software distributed under the License is distributed on an14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15  * KIND, either express or implied.  See the License for the16  * specific language governing permissions and limitations17  * under the License.18  */19 package org.elasticsearch.search.aggregations.bucket;20 21 import org.elasticsearch.ElasticsearchException;22 import org.elasticsearch.action.index.IndexRequestBuilder;23 import org.elasticsearch.action.search.SearchResponse;24 import org.elasticsearch.common.xcontent.XContentBuilder;25 import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;26 import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;27 import org.elasticsearch.search.aggregations.bucket.nested.Nested;28 import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;29 import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;30 import org.elasticsearch.search.aggregations.bucket.terms.Terms;31 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;32 import org.elasticsearch.search.aggregations.metrics.max.Max;33 import org.elasticsearch.search.aggregations.metrics.stats.Stats;34 import org.elasticsearch.search.aggregations.metrics.sum.Sum;35 import org.elasticsearch.test.ElasticsearchIntegrationTest;36 import org.hamcrest.Matchers;37 import org.junit.Test;38 39 import java.util.ArrayList;40 import java.util.List;41 42 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;43 import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;44 import static org.elasticsearch.search.aggregations.AggregationBuilders.*;45 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;46 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;47 import static org.hamcrest.Matchers.equalTo;48 import static org.hamcrest.Matchers.is;49 import static org.hamcrest.core.IsNull.notNullValue;50 51 /**52  *53  */54 @ElasticsearchIntegrationTest.SuiteScopeTest55 public class NestedTests extends ElasticsearchIntegrationTest {56 57     static int numParents;58     static int[] numChildren;59     static SubAggCollectionMode aggCollectionMode;60 61     @Override62     public void setupSuiteScopeCluster() throws Exception {63 64         assertAcked(prepareCreate("idx")65                 .addMapping("type", "nested", "type=nested"));66 67         List<IndexRequestBuilder> builders = new ArrayList<>();68 69         numParents = randomIntBetween(3, 10);70         numChildren = new int[numParents];71         aggCollectionMode = randomFrom(SubAggCollectionMode.values());72         logger.info("AGG COLLECTION MODE: " + aggCollectionMode);73         int totalChildren = 0;74         for (int i = 0; i < numParents; ++i) {75             if (i == numParents - 1 && totalChildren == 0) {76                 // we need at least one child overall77                 numChildren[i] = randomIntBetween(1, 5);78             } else {79                 numChildren[i] = randomInt(5);80             }81             totalChildren += numChildren[i];82         }83         assertTrue(totalChildren > 0);84 85         for (int i = 0; i < numParents; i++) {86             XContentBuilder source = jsonBuilder()87                     .startObject()88                     .field("value", i + 1)89                     .startArray("nested");90             for (int j = 0; j < numChildren[i]; ++j) {91                 source = source.startObject().field("value", i + 1 + j).endObject();92             }93             source = source.endArray().endObject();94             builders.add(client().prepareIndex("idx", "type", ""+i+1).setSource(source));95         }96 97         prepareCreate("empty_bucket_idx").addMapping("type", "value", "type=integer", "nested", "type=nested").execute().actionGet();98         for (int i = 0; i < 2; i++) {99             builders.add(client().prepareIndex("empty_bucket_idx", "type", ""+i).setSource(jsonBuilder()
100                     .startObject()
101                     .field("value", i*2)
102                     .startArray("nested")
103                     .startObject().field("value", i + 1).endObject()
104                     .startObject().field("value", i + 2).endObject()
105                     .startObject().field("value", i + 3).endObject()
106                     .startObject().field("value", i + 4).endObject()
107                     .startObject().field("value", i + 5).endObject()
108                     .endArray()
109                     .endObject()));
110         }
111
112         assertAcked(prepareCreate("idx_nested_nested_aggs")
113                 .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
114                         .startObject("nested1")
115                             .field("type", "nested")
116                             .startObject("properties")
117                                 .startObject("nested2")
118                                     .field("type", "nested")
119                                 .endObject()
120                             .endObject()
121                         .endObject()
122                         .endObject().endObject().endObject()));
123
124         builders.add(
125                 client().prepareIndex("idx_nested_nested_aggs", "type", "1")
126                         .setSource(jsonBuilder().startObject()
127                                 .startArray("nested1")
128                                     .startObject()
129                                     .field("a", "a")
130                                         .startArray("nested2")
131                                             .startObject()
132                                                 .field("b", 2)
133                                             .endObject()
134                                         .endArray()
135                                     .endObject()
136                                     .startObject()
137                                         .field("a", "b")
138                                         .startArray("nested2")
139                                             .startObject()
140                                                 .field("b", 2)
141                                             .endObject()
142                                         .endArray()
143                                     .endObject()
144                                 .endArray()
145                             .endObject())
146         );
147         indexRandom(true, builders);
148         ensureSearchable();
149     }
150
151     @Test
152     public void simple() throws Exception {
153         SearchResponse response = client().prepareSearch("idx")
154                 .addAggregation(nested("nested").path("nested")
155                         .subAggregation(stats("nested_value_stats").field("nested.value")))
156                 .execute().actionGet();
157
158         assertSearchResponse(response);
159
160
161         double min = Double.POSITIVE_INFINITY;
162         double max = Double.NEGATIVE_INFINITY;
163         long sum = 0;
164         long count = 0;
165         for (int i = 0; i < numParents; ++i) {
166             for (int j = 0; j < numChildren[i]; ++j) {
167                 final long value = i + 1 + j;
168                 min = Math.min(min, value);
169                 max = Math.max(max, value);
170                 sum += value;
171                 ++count;
172             }
173         }
174
175         Nested nested = response.getAggregations().get("nested");
176         assertThat(nested, notNullValue());
177         assertThat(nested.getName(), equalTo("nested"));
178         assertThat(nested.getDocCount(), equalTo(count));
179         assertThat(nested.getAggregations().asList().isEmpty(), is(false));
180
181         Stats stats = nested.getAggregations().get("nested_value_stats");
182         assertThat(stats, notNullValue());
183         assertThat(stats.getMin(), equalTo(min));
184         assertThat(stats.getMax(), equalTo(max));
185         assertThat(stats.getCount(), equalTo(count));
186         assertThat(stats.getSum(), equalTo((double) sum));
187         assertThat(stats.getAvg(), equalTo((double) sum / count));
188     }
189
190     @Test
191     public void onNonNestedField() throws Exception {
192         try {
193             client().prepareSearch("idx")
194                     .addAggregation(nested("nested").path("value")
195                             .subAggregation(stats("nested_value_stats").field("nested.value")))
196                     .execute().actionGet();
197
198             fail("expected execution to fail - an attempt to nested facet on non-nested field/path");
199
200         } catch (ElasticsearchException ese) {
201         }
202     }
203
204     @Test
205     public void nestedWithSubTermsAgg() throws Exception {
206         SearchResponse response = client().prepareSearch("idx")
207                 .addAggregation(nested("nested").path("nested")
208                         .subAggregation(terms("values").field("nested.value").size(100)
209                                 .collectMode(aggCollectionMode)))
210                 .execute().actionGet();
211
212         assertSearchResponse(response);
213
214
215         long docCount = 0;
216         long[] counts = new long[numParents + 6];
217         for (int i = 0; i < numParents; ++i) {
218             for (int j = 0; j < numChildren[i]; ++j) {
219                 final int value = i + 1 + j;
220                 ++counts[value];
221                 ++docCount;
222             }
223         }
224         int uniqueValues = 0;
225         for (long count : counts) {
226             if (count > 0) {
227                 ++uniqueValues;
228             }
229         }
230
231         Nested nested = response.getAggregations().get("nested");
232         assertThat(nested, notNullValue());
233         assertThat(nested.getName(), equalTo("nested"));
234         assertThat(nested.getDocCount(), equalTo(docCount));
235         assertThat(nested.getAggregations().asList().isEmpty(), is(false));
236
237         LongTerms values = nested.getAggregations().get("values");
238         assertThat(values, notNullValue());
239         assertThat(values.getName(), equalTo("values"));
240         assertThat(values.getBuckets(), notNullValue());
241         assertThat(values.getBuckets().size(), equalTo(uniqueValues));
242         for (int i = 0; i < counts.length; ++i) {
243             final String key = Long.toString(i);
244             if (counts[i] == 0) {
245                 assertNull(values.getBucketByKey(key));
246             } else {
247                 Bucket bucket = values.getBucketByKey(key);
248                 assertNotNull(bucket);
249                 assertEquals(counts[i], bucket.getDocCount());
250             }
251         }
252     }
253
254     @Test
255     public void nestedAsSubAggregation() throws Exception {
256         SearchResponse response = client().prepareSearch("idx")
257                 .addAggregation(terms("top_values").field("value").size(100)
258                         .collectMode(aggCollectionMode)
259                         .subAggregation(nested("nested").path("nested")
260                                 .subAggregation(max("max_value").field("nested.value"))))
261                 .execute().actionGet();
262
263         assertSearchResponse(response);
264
265
266         LongTerms values = response.getAggregations().get("top_values");
267         assertThat(values, notNullValue());
268         assertThat(values.getName(), equalTo("top_values"));
269         assertThat(values.getBuckets(), notNullValue());
270         assertThat(values.getBuckets().size(), equalTo(numParents));
271
272         for (int i = 0; i < numParents; i++) {
273             String topValue = "" + (i + 1);
274             assertThat(values.getBucketByKey(topValue), notNullValue());
275             Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested");
276             assertThat(nested, notNullValue());
277             Max max = nested.getAggregations().get("max_value");
278             assertThat(max, notNullValue());
279             assertThat(max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i]));
280         }
281     }
282
283     @Test
284     public void nestNestedAggs() throws Exception {
285         SearchResponse response = client().prepareSearch("idx_nested_nested_aggs")
286                 .addAggregation(nested("level1").path("nested1")
287                         .subAggregation(terms("a").field("nested1.a")
288                                 .collectMode(aggCollectionMode)
289                                 .subAggregation(nested("level2").path("nested1.nested2")
290                                         .subAggregation(sum("sum").field("nested1.nested2.b")))))
291                 .get();
292         assertSearchResponse(response);
293
294
295         Nested level1 = response.getAggregations().get("level1");
296         assertThat(level1, notNullValue());
297         assertThat(level1.getName(), equalTo("level1"));
298         assertThat(level1.getDocCount(), equalTo(2l));
299
300         StringTerms a = level1.getAggregations().get("a");
301         Terms.Bucket bBucket = a.getBucketByKey("a");
302         assertThat(bBucket.getDocCount(), equalTo(1l));
303
304         Nested level2 = bBucket.getAggregations().get("level2");
305         assertThat(level2.getDocCount(), equalTo(1l));
306         Sum sum = level2.getAggregations().get("sum");
307         assertThat(sum.getValue(), equalTo(2d));
308
309         a = level1.getAggregations().get("a");
310         bBucket = a.getBucketByKey("b");
311         assertThat(bBucket.getDocCount(), equalTo(1l));
312
313         level2 = bBucket.getAggregations().get("level2");
314         assertThat(level2.getDocCount(), equalTo(1l));
315         sum = level2.getAggregations().get("sum");
316         assertThat(sum.getValue(), equalTo(2d));
317     }
318
319
320     @Test
321     public void emptyAggregation() throws Exception {
322         SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx")
323                 .setQuery(matchAllQuery())
324                 .addAggregation(histogram("histo").field("value").interval(1l).minDocCount(0)
325                         .subAggregation(nested("nested").path("nested")))
326                 .execute().actionGet();
327
328         assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
329         Histogram histo = searchResponse.getAggregations().get("histo");
330         assertThat(histo, Matchers.notNullValue());
331         Histogram.Bucket bucket = histo.getBucketByKey(1l);
332         assertThat(bucket, Matchers.notNullValue());
333
334         Nested nested = bucket.getAggregations().get("nested");
335         assertThat(nested, Matchers.notNullValue());
336         assertThat(nested.getName(), equalTo("nested"));
337         assertThat(nested.getDocCount(), is(0l));
338     }
339 }

上面的代码是链接上的.下面的是自己的应用.

public static Map<String, Object> GetRegionInfo(Client client,
RequestSignal requestSignal, Set<String> set) {

Map<String, Object> result = new HashMap<String, Object>();

AggregationBuilder aggs1 = AggregationBuilders.nested("level1").path(
"nna_regions");
AggregationBuilder aggs2 = AggregationBuilders.filter("level2").filter(
ConstValue.AGGS_FILTERBUILDER);
AggregationBuilder aggs3 = AggregationBuilders.terms("level3")
.field("nna_regions.sca_region").size(5000);
SumBuilder aggs4 = AggregationBuilders.sum("level4").field(
"nna_regions.dna_score");

SearchResponse response = client
.prepareSearch("flume-*-content-*")
.setQuery(ConstValue.queryBuilder_statAction(requestSignal))
.setSearchType("count")
.addAggregation(
aggs1.subAggregation(aggs2.subAggregation(aggs3
.subAggregation(aggs4)))).execute().actionGet();

Nested level1 = response.getAggregations().get("level1");
Filter level2 = level1.getAggregations().get("level2");

Terms level3 = level2.getAggregations().get("level3");
Collection<Terms.Bucket> collection = level3.getBuckets();

for (Terms.Bucket bucket : collection) {
String region = bucket.getKey();
long count = bucket.getDocCount();
double score = 0;
if (set.contains(region)) {
Sum sum = bucket.getAggregations().get("level4");

if (sum == null) {
System.out.println("null");
} else {
score = sum.getValue();
}
Map<String, Object> tmp = new HashMap<String, Object>();
tmp.put("count", count);
tmp.put("score", score);
result.put(region, tmp);
}
}
return result;
}

 1     public static Map<String, Object> GetRegionInfo(Client client,2             RequestSignal requestSignal, Set<String> set) {3 4         Map<String, Object> result = new HashMap<String, Object>();5 6         AggregationBuilder aggs1 = AggregationBuilders.nested("level1").path(7                 "nna_regions");8         AggregationBuilder aggs2 = AggregationBuilders.filter("level2").filter(9                 ConstValue.AGGS_FILTERBUILDER);
10         AggregationBuilder aggs3 = AggregationBuilders.terms("level3")
11                 .field("nna_regions.sca_region").size(5000);
12         SumBuilder aggs4 = AggregationBuilders.sum("level4").field(
13                 "nna_regions.dna_score");
14
15         SearchResponse response = client
16                 .prepareSearch("flume-*-content-*")
17                 .setQuery(ConstValue.queryBuilder_statAction(requestSignal))
18                 .setSearchType("count")
19                 .addAggregation(
20                         aggs1.subAggregation(aggs2.subAggregation(aggs3
21                                 .subAggregation(aggs4)))).execute().actionGet();
22
23         Nested level1 = response.getAggregations().get("level1");
24         Filter level2 = level1.getAggregations().get("level2");
25
26         Terms level3 = level2.getAggregations().get("level3");
27         Collection<Terms.Bucket> collection = level3.getBuckets();
28
29         for (Terms.Bucket bucket : collection) {
30             String region = bucket.getKey();
31             long count = bucket.getDocCount();
32             double score = 0;
33             if (set.contains(region)) {
34                 Sum sum = bucket.getAggregations().get("level4");
35
36                 if (sum == null) {
37                     System.out.println("null");
38                 } else {
39                     score = sum.getValue();
40                 }
41                 Map<String, Object> tmp = new HashMap<String, Object>();
42                 tmp.put("count", count);
43                 tmp.put("score", score);
44                 result.put(region, tmp);
45             }
46         }
47         return result;
48     }

aggs1是取得嵌套域的名.

其他的代码,关于取日期域值的方法.

private String statRequest(Client esClient) {
FilteredQueryBuilder builder = QueryBuilders.filteredQuery(
QueryBuilders.matchAllQuery(),
FilterBuilders.rangeFilter("tfp_save_time").from(begTime)
.to(endTime).includeLower(true).includeUpper(true));

AggregationBuilder aggs1 = AggregationBuilders.terms("inp_type").field(
"inp_type");

AggregationBuilder aggs = AggregationBuilders.dateHistogram("By_Date")
.field("tfp_save_time").format("yyyy-MM-dd HH:mm:ss")
.extendedBounds(begTime, endTime).interval(statType);

SearchResponse response = esClient.prepareSearch("flume-*-content*")
.setQuery(builder).setSearchType("count")
.addAggregation(aggs1.subAggregation(aggs)).execute()
.actionGet();

Terms terms = response.getAggregations().get("inp_type");
Collection<Terms.Bucket> inp_type = terms.getBuckets();
Iterator<Bucket> inp_type_It = inp_type.iterator();
// Gson gson = new GsonBuilder().disableHtmlEscaping().create();

StatResult statResult = new StatResult(); // result.

while (inp_type_It.hasNext()) {

HashMap<String, Integer> test = new HashMap<String, Integer>();// result
// nested.
Bucket inpBucket = inp_type_It.next();
// System.out.println(inpBucket.getKey());
// System.out.println(inpBucket.getDocCount());
DateHistogram dateHistogram = (DateHistogram) inpBucket
.getAggregations().get("By_Date");
Collection<DateHistogram.Bucket> by_date = (Collection<DateHistogram.Bucket>) dateHistogram
.getBuckets();

Iterator<DateHistogram.Bucket> by_date_It = by_date.iterator();

while (by_date_It.hasNext()) {
DateHistogram.Bucket bucket = by_date_It.next();

int count = Integer.parseInt(String.valueOf(bucket
.getDocCount()));
String newdate = postDate(bucket.getKey());

test.put(newdate, count);
}
if (!test.isEmpty()) {
statResult.add(inpBucket.getKey(), test);
}
}
return statResult.toString();
}

转载于:https://www.cnblogs.com/sha0830/p/5549305.html

ElasticSearch Aggs的一些使用方法相关推荐

  1. 【最佳实践】Elasticsearch Snapshot 备份的使用方法

    简介:常见的数据库都会提供备份的机制,以解决在数据库无法使用的情况下,可以开启新的实例,然后通过备份来恢复数据减少损失. 作者介绍 魏彬,普翔科技 CTO,开源软件爱好者,中国第一位 Elastic ...

  2. Elasticsearch常见错误及解决方法

    Elasticsearch常见错误及解决方法: 1.启动时候报错:Caused by: java.net.BindException: Cannot assign requested address ...

  3. elasticsearch client依赖包下载方法

    elasticsearch client依赖包的maven配置 <?xml version="1.0"?><project xmlns="http:// ...

  4. php 搜索引擎 分词_PHP使用elasticsearch搜索安装及分词方法

    一.背景 为什么会用到这个ES搜索? 是因为我在看乌云的漏洞案例库时候,搜索即为不方便. 比如说说我要搜索一个 SQL注入 那mysql匹配的时候是like模糊匹配,搜索必须要有SQL注入这四个字,连 ...

  5. Elasticsearch提高查询性能的方法

    ES性能并没有想象中那么好.很多时候数据量大了,特别是有几亿条数据的时候,可能第一次搜索的时候,是5-10s,后面反而就快了,可能就几百毫秒. 说实话,ES 性能优化不要期待着随手调一个参数,就可以万 ...

  6. Elasticsearch X-pack证书过期解决方法

    目录 证书未过期 重新编译破解x-pack-5.6.2.jar 重启Es服务 证书已过期 重新生成x-pack-5.6.2.jar,步骤同上 创建license.json license.json目录 ...

  7. elasticsearch aggs

    创建索引 PUT /test_index_v1 {"mappings": {"properties": {"th_val":{"t ...

  8. es - elasticsearch - aggs - pipeline - moving_avg

    世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程. 文章目录 pipeline 移动平均聚合:moving_avg 特点 作用 配置项 样式 实例 建索引 查询 结 ...

  9. es - elasticsearch - aggs - metrics - t-test

    世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程. 问:t-test有什么特点? 答: 问:t-test如何使用? 答: DELETE t_test_test;PU ...

最新文章

  1. python连接redis有中文_Python连接Redis并操作
  2. 【总结】有三AI秋季划模型优化组3月直播讲了哪些内容,为什么每一个从事深度学习的同学都应该掌握模型优化的内容...
  3. 【必须收藏】那些酷炫的深度学习网络图怎么画出来的?
  4. 高等数理统计(part5)--分布族的完备性
  5. python自动化测试脚本可以测php吗_自动化测试,用Python还是Java?
  6. Java中数字朝着0.5的倍数取舍
  7. 虎虎生威,挑战云上魔方(活动期完成可得实物魔方)
  8. DELL linux 网卡驱动升级
  9. 机器学习实战笔记1——机器学习导论
  10. 利用python第三方库过抖音小店后台滑块
  11. R语言 AHP层次分析法——如何验证矩阵一致性
  12. C++实现费马小定理素数判定法和米勒拉宾素数判定算法生成大素数
  13. 计算机毕业设计Python+uniapp鲸落图书商城小程序LW(小程序+源码+LW)
  14. VueCLi跑项目时卡在98% after emitting CopyPlugin无法运行
  15. 10道虐心的Java面试题,被面试官虐哭了,同事一题都没答对
  16. 17.sql server数据库使用规则、二八原则、数据库查询集群--数据库读写分离多种实现、数据库配置读写分离(by-朝夕)
  17. nodes are available: 1 node(s) had taints that the pod didn‘t tolerate
  18. Three.js精灵模型Sprite
  19. Kubernetes CKS 2021 Course【15】---Microservice Vulnerabilities - mTLS
  20. 腾讯云服务器重庆地域和成都地域区别及哪个更好

热门文章

  1. coding-summaries
  2. Body-parser
  3. 从零开始搭建SpringBoot项目(一)——开发环境搭建(图文详细)
  4. Cesium与STK中的天空盒子(skybox)
  5. choco无法将choco识别_choco入门
  6. 对话框程序, 在 OnInitDialog的最后 showWindow(SW_HIDE),不管用的原因
  7. 【案例】电影数据分析
  8. 深入浅出图神经网络~卷积神经网络(上)
  9. Authentication failed for 解决办法
  10. BI神器Power Query(4)-- PQ导入动态名称定义的表格