|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.search.aggregations.metrics; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.util.Strings; |
| 12 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 13 | +import org.elasticsearch.action.search.SearchRequestBuilder; |
| 14 | +import org.elasticsearch.common.settings.Settings; |
| 15 | +import org.elasticsearch.rest.RestStatus; |
| 16 | +import org.elasticsearch.search.aggregations.bucket.terms.Terms; |
| 17 | +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregatorFactory.ExecutionMode; |
| 18 | +import org.elasticsearch.search.sort.SortBuilders; |
| 19 | +import org.elasticsearch.search.sort.SortOrder; |
| 20 | +import org.elasticsearch.test.ESIntegTestCase; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; |
| 27 | +import static org.elasticsearch.search.aggregations.AggregationBuilders.topHits; |
| 28 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 29 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures; |
| 30 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse; |
| 31 | +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; |
| 32 | +import static org.hamcrest.Matchers.containsString; |
| 33 | +import static org.hamcrest.Matchers.notNullValue; |
| 34 | + |
| 35 | +@ESIntegTestCase.SuiteScopeTestCase() |
| 36 | +public class LargeTopHitsIT extends ESIntegTestCase { |
| 37 | + |
| 38 | + private static final String TERMS_AGGS_FIELD_1 = "terms1"; |
| 39 | + private static final String TERMS_AGGS_FIELD_2 = "terms2"; |
| 40 | + private static final String TERMS_AGGS_FIELD_3 = "terms3"; |
| 41 | + private static final String SORT_FIELD = "sort"; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 45 | + return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)).put("indices.breaker.request.type", "memory").build(); |
| 46 | + } |
| 47 | + |
| 48 | + public static String randomExecutionHint() { |
| 49 | + return randomBoolean() ? null : randomFrom(ExecutionMode.values()).toString(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void setupSuiteScopeCluster() throws Exception { |
| 54 | + initSmallIdx(); |
| 55 | + ensureSearchable(); |
| 56 | + } |
| 57 | + |
| 58 | + private void initSmallIdx() throws IOException { |
| 59 | + createIndex("small_idx"); |
| 60 | + ensureGreen("small_idx"); |
| 61 | + populateIndex("small_idx", 5, 40_000); |
| 62 | + } |
| 63 | + |
| 64 | + private void initLargeIdx() throws IOException { |
| 65 | + createIndex("large_idx"); |
| 66 | + ensureGreen("large_idx"); |
| 67 | + populateIndex("large_idx", 70, 50_000); |
| 68 | + } |
| 69 | + |
| 70 | + public void testSimple() { |
| 71 | + assertNoFailuresAndResponse(query("small_idx"), response -> { |
| 72 | + Terms terms = response.getAggregations().get("terms"); |
| 73 | + assertThat(terms, notNullValue()); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + public void test500Queries() { |
| 78 | + for (int i = 0; i < 500; i++) { |
| 79 | + // make sure we are not leaking memory over multiple queries |
| 80 | + assertNoFailuresAndResponse(query("small_idx"), response -> { |
| 81 | + Terms terms = response.getAggregations().get("terms"); |
| 82 | + assertThat(terms, notNullValue()); |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // This works most of the time, but it's not consistent: it still triggers OOM sometimes. |
| 88 | + // The test env is too small and non-deterministic to hold all these data and results. |
| 89 | + @AwaitsFix(bugUrl = "see comment above") |
| 90 | + public void testBreakAndRecover() throws IOException { |
| 91 | + initLargeIdx(); |
| 92 | + assertNoFailuresAndResponse(query("small_idx"), response -> { |
| 93 | + Terms terms = response.getAggregations().get("terms"); |
| 94 | + assertThat(terms, notNullValue()); |
| 95 | + }); |
| 96 | + |
| 97 | + assertFailures(query("large_idx"), RestStatus.TOO_MANY_REQUESTS, containsString("Data too large")); |
| 98 | + |
| 99 | + assertNoFailuresAndResponse(query("small_idx"), response -> { |
| 100 | + Terms terms = response.getAggregations().get("terms"); |
| 101 | + assertThat(terms, notNullValue()); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private void createIndex(String idxName) { |
| 106 | + assertAcked( |
| 107 | + prepareCreate(idxName).setMapping( |
| 108 | + TERMS_AGGS_FIELD_1, |
| 109 | + "type=keyword", |
| 110 | + TERMS_AGGS_FIELD_2, |
| 111 | + "type=keyword", |
| 112 | + TERMS_AGGS_FIELD_3, |
| 113 | + "type=keyword", |
| 114 | + "text", |
| 115 | + "type=text,store=true", |
| 116 | + "large_text_1", |
| 117 | + "type=text,store=false", |
| 118 | + "large_text_2", |
| 119 | + "type=text,store=false", |
| 120 | + "large_text_3", |
| 121 | + "type=text,store=false", |
| 122 | + "large_text_4", |
| 123 | + "type=text,store=false", |
| 124 | + "large_text_5", |
| 125 | + "type=text,store=false" |
| 126 | + ) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private void populateIndex(String idxName, int nDocs, int size) throws IOException { |
| 131 | + for (int i = 0; i < nDocs; i++) { |
| 132 | + List<IndexRequestBuilder> builders = new ArrayList<>(); |
| 133 | + builders.add( |
| 134 | + prepareIndex(idxName).setId(Integer.toString(i)) |
| 135 | + .setSource( |
| 136 | + jsonBuilder().startObject() |
| 137 | + .field(TERMS_AGGS_FIELD_1, "val" + i % 53) |
| 138 | + .field(TERMS_AGGS_FIELD_2, "val" + i % 23) |
| 139 | + .field(TERMS_AGGS_FIELD_3, "val" + i % 10) |
| 140 | + .field(SORT_FIELD, i) |
| 141 | + .field("text", "some text to entertain") |
| 142 | + .field("large_text_1", Strings.repeat("this is a text field 1 ", size)) |
| 143 | + .field("large_text_2", Strings.repeat("this is a text field 2 ", size)) |
| 144 | + .field("large_text_3", Strings.repeat("this is a text field 3 ", size)) |
| 145 | + .field("large_text_4", Strings.repeat("this is a text field 4 ", size)) |
| 146 | + .field("large_text_5", Strings.repeat("this is a text field 5 ", size)) |
| 147 | + .field("field1", 5) |
| 148 | + .field("field2", 2.71) |
| 149 | + .endObject() |
| 150 | + ) |
| 151 | + ); |
| 152 | + |
| 153 | + indexRandom(true, builders); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static SearchRequestBuilder query(String indexName) { |
| 158 | + return prepareSearch(indexName).addAggregation( |
| 159 | + terms("terms").executionHint(randomExecutionHint()) |
| 160 | + .field(TERMS_AGGS_FIELD_1) |
| 161 | + .subAggregation( |
| 162 | + terms("terms").executionHint(randomExecutionHint()) |
| 163 | + .field(TERMS_AGGS_FIELD_2) |
| 164 | + .subAggregation( |
| 165 | + terms("terms").executionHint(randomExecutionHint()) |
| 166 | + .field(TERMS_AGGS_FIELD_2) |
| 167 | + .subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))) |
| 168 | + ) |
| 169 | + ) |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments