|
49 | 49 | import java.util.Map;
|
50 | 50 | import java.util.Set;
|
51 | 51 | import java.util.function.Function;
|
| 52 | +import java.util.stream.Collectors; |
52 | 53 |
|
53 | 54 | import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
54 | 55 | import static org.elasticsearch.search.aggregations.AggregationBuilders.avg;
|
@@ -1287,4 +1288,92 @@ public void testScriptWithValueType() throws Exception {
|
1287 | 1288 | assertThat(ex.getCause().getMessage(), containsString("Unknown value type [foobar]"));
|
1288 | 1289 | }
|
1289 | 1290 | }
|
| 1291 | + |
| 1292 | + public void testOrderByKey() throws Exception { |
| 1293 | + Map<String, long[]> data = new HashMap<>(); |
| 1294 | + for (int i = 0; i < 5; i++) { |
| 1295 | + assertAcked( |
| 1296 | + indicesAdmin().prepareCreate("idx" + i).setMapping(SINGLE_VALUED_FIELD_NAME, "type=keyword", "filter", "type=boolean") |
| 1297 | + ); |
| 1298 | + List<IndexRequestBuilder> builders = new ArrayList<>(); |
| 1299 | + for (int j = 0; j < 100; j++) { |
| 1300 | + String val = "val" + random().nextInt(1000); |
| 1301 | + boolean filter = randomBoolean(); |
| 1302 | + long[] counter = data.computeIfAbsent(val, s -> new long[] { 0 }); |
| 1303 | + if (filter == false) { |
| 1304 | + counter[0]++; |
| 1305 | + } |
| 1306 | + builders.add( |
| 1307 | + prepareIndex("idx" + i).setSource( |
| 1308 | + jsonBuilder().startObject().field(SINGLE_VALUED_FIELD_NAME, val).field("filter", filter).endObject() |
| 1309 | + ) |
| 1310 | + ); |
| 1311 | + } |
| 1312 | + indexRandom(true, builders); |
| 1313 | + } |
| 1314 | + List<String> allKeys = new ArrayList<>(data.keySet()); |
| 1315 | + List<String> keysMinDocCount1 = allKeys.stream().filter(key -> data.get(key)[0] > 0).collect(Collectors.toList()); |
| 1316 | + List<String> keysMinDocCount2 = allKeys.stream().filter(key -> data.get(key)[0] > 1).collect(Collectors.toList()); |
| 1317 | + // test for different batch sizes to exercise partial reduces |
| 1318 | + for (int batchReduceSize = 2; batchReduceSize < 6; batchReduceSize++) { |
| 1319 | + // with min_doc_count = 0 |
| 1320 | + allKeys.sort(String::compareTo); |
| 1321 | + assertOrderByKeyResponse(allKeys, data, true, 0, batchReduceSize); |
| 1322 | + Collections.reverse(allKeys); |
| 1323 | + assertOrderByKeyResponse(allKeys, data, false, 0, batchReduceSize); |
| 1324 | + // with min_doc_count = 1 |
| 1325 | + keysMinDocCount1.sort(String::compareTo); |
| 1326 | + assertOrderByKeyResponse(keysMinDocCount1, data, true, 1, batchReduceSize); |
| 1327 | + Collections.reverse(keysMinDocCount1); |
| 1328 | + assertOrderByKeyResponse(keysMinDocCount1, data, false, 1, batchReduceSize); |
| 1329 | + // with min_doc_count = 2 |
| 1330 | + keysMinDocCount2.sort(String::compareTo); |
| 1331 | + assertOrderByKeyResponse(keysMinDocCount2, data, true, 2, batchReduceSize); |
| 1332 | + Collections.reverse(keysMinDocCount2); |
| 1333 | + assertOrderByKeyResponse(keysMinDocCount2, data, false, 2, batchReduceSize); |
| 1334 | + } |
| 1335 | + for (int i = 0; i < 5; i++) { |
| 1336 | + assertAcked(indicesAdmin().prepareDelete("idx" + i)); |
| 1337 | + } |
| 1338 | + } |
| 1339 | + |
| 1340 | + private void assertOrderByKeyResponse( |
| 1341 | + List<String> keys, |
| 1342 | + Map<String, long[]> counts, |
| 1343 | + boolean asc, |
| 1344 | + int minDocCount, |
| 1345 | + int batchReduceSize |
| 1346 | + ) { |
| 1347 | + int size = randomIntBetween(1, keys.size()); |
| 1348 | + long sumOtherCount = 0; |
| 1349 | + for (int i = size; i < keys.size(); i++) { |
| 1350 | + sumOtherCount += counts.get(keys.get(i))[0]; |
| 1351 | + } |
| 1352 | + final long finalSumOtherCount = sumOtherCount; |
| 1353 | + assertNoFailuresAndResponse( |
| 1354 | + prepareSearch("idx0", "idx1", "idx2", "idx3", "idx4").setBatchedReduceSize(batchReduceSize) |
| 1355 | + .setQuery(QueryBuilders.termQuery("filter", false)) |
| 1356 | + .addAggregation( |
| 1357 | + new TermsAggregationBuilder("terms").field(SINGLE_VALUED_FIELD_NAME) |
| 1358 | + .size(size) |
| 1359 | + .shardSize(500) |
| 1360 | + .minDocCount(minDocCount) |
| 1361 | + .order(BucketOrder.key(asc)) |
| 1362 | + ), |
| 1363 | + response -> { |
| 1364 | + StringTerms terms = response.getAggregations().get("terms"); |
| 1365 | + assertThat(terms, notNullValue()); |
| 1366 | + assertThat(terms.getName(), equalTo("terms")); |
| 1367 | + assertThat(terms.getBuckets().size(), equalTo(size)); |
| 1368 | + assertThat(terms.getSumOfOtherDocCounts(), equalTo(finalSumOtherCount)); |
| 1369 | + |
| 1370 | + for (int i = 0; i < size; i++) { |
| 1371 | + StringTerms.Bucket bucket = terms.getBuckets().get(i); |
| 1372 | + assertThat(bucket, notNullValue()); |
| 1373 | + assertThat(bucket.getKeyAsString(), equalTo(keys.get(i))); |
| 1374 | + assertThat(bucket.getDocCount(), equalTo(counts.get(keys.get(i))[0])); |
| 1375 | + } |
| 1376 | + } |
| 1377 | + ); |
| 1378 | + } |
1290 | 1379 | }
|
0 commit comments