Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
43a676f
fix: hash the tsid if required
salvatore-campagna Jul 27, 2023
1d900e4
fix: comment document id assertion
salvatore-campagna Jul 28, 2023
0c24a12
fix: base 64 encode the hash
salvatore-campagna Aug 2, 2023
672e27c
test: a few aggregations (cardinality not working)
salvatore-campagna Aug 2, 2023
7e78dac
fix: minor refactor
salvatore-campagna Aug 4, 2023
a9f439b
fix: refactor and disable test
salvatore-campagna Aug 29, 2023
8ce7d21
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Aug 31, 2023
e24c3be
fix: always hash the tsid
salvatore-campagna Sep 4, 2023
7b9dcd2
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Sep 4, 2023
58b44f9
fix: base64 without padding
salvatore-campagna Sep 4, 2023
6a92d61
fix: remove hash prefix
salvatore-campagna Sep 4, 2023
56645db
fix: use tsid hash values in different tests
salvatore-campagna Sep 4, 2023
1364c59
fix: use similarity hash to hash the _tsid
salvatore-campagna Sep 7, 2023
a55021a
fix: enable test and rewrite comment
salvatore-campagna Sep 7, 2023
2d4a60a
fix: some _tsid hash values
salvatore-campagna Sep 7, 2023
0db1f0d
fix: some more _tsid hash values
salvatore-campagna Sep 7, 2023
6bb3695
fix: some more _tsid values and yaml test skip version
salvatore-campagna Sep 7, 2023
a435a06
fix: increase the dimension field limit
salvatore-campagna Oct 5, 2023
fffc5aa
fix: hash the tsid field using a new hashing scheme
salvatore-campagna Oct 17, 2023
5e224c6
fix: incorrect hash128
salvatore-campagna Oct 19, 2023
36edec5
fix: optimize hash128
salvatore-campagna Oct 19, 2023
b606182
fix: some tsid values in tests
salvatore-campagna Oct 19, 2023
e38babd
fix: update tsid hash reducing memory allocations
salvatore-campagna Oct 19, 2023
28c1319
fix: comment
salvatore-campagna Oct 19, 2023
7242b79
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Oct 30, 2023
c5fa3cc
fix: simplify tsid hashing
salvatore-campagna Oct 30, 2023
8c2b8aa
fix: update some tsid values in tests
salvatore-campagna Oct 30, 2023
acc0d14
fix: document id generation
salvatore-campagna Oct 31, 2023
ef33743
fix: use the tsid hash as a bucket key
salvatore-campagna Nov 3, 2023
103b1f8
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Nov 21, 2023
1044c8a
fix: use tsid hashing everywhere, adapt downsampling remove decodeTsi…
salvatore-campagna Nov 22, 2023
57454c2
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Nov 22, 2023
b08c6df
fix: a few test failing because of id changes
salvatore-campagna Nov 22, 2023
2822d0f
fix: some tsid values after introducing tsid hashing
salvatore-campagna Nov 23, 2023
6ce797f
fix: some more tests
salvatore-campagna Nov 23, 2023
d256209
fix: update yaml skip version
salvatore-campagna Nov 23, 2023
a2b7ab3
fix: some more tests
salvatore-campagna Nov 23, 2023
1173778
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Nov 23, 2023
16f5f03
fix: merge conflict
salvatore-campagna Nov 23, 2023
c820e87
fix: other tsid tests
salvatore-campagna Nov 23, 2023
677bb21
fix: some more tsid tests
salvatore-campagna Nov 24, 2023
4d5fd77
fix: some more tests and ParsedTimeSeries
salvatore-campagna Nov 24, 2023
64c7172
fix: geo line tests and a few more tsid values in composite
salvatore-campagna Nov 24, 2023
ca9f668
fix: downsampling tsid values and bucket ordering
salvatore-campagna Nov 24, 2023
9d5351f
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Nov 24, 2023
0dff470
fix: no way to get tsid from dimensions anymore
salvatore-campagna Nov 24, 2023
a1b9f17
fix: decrease indexing pressure in test to avoid error
salvatore-campagna Nov 24, 2023
950942c
fix: parse after key string as a BytesRef
salvatore-campagna Nov 24, 2023
8a626ca
fix: downsampling test after tsid hashing
salvatore-campagna Nov 24, 2023
0b0d114
fix: possible NPE
salvatore-campagna Nov 24, 2023
2417509
fix: TimeSeriesIdFieldMapperTsidHashingTests
salvatore-campagna Nov 24, 2023
cbd99fd
fix: TsidExtractingIdFieldMapper
salvatore-campagna Nov 24, 2023
fa321d8
fix: sime other yaml tests
salvatore-campagna Nov 24, 2023
e046bbb
fix: a few more tests
salvatore-campagna Nov 27, 2023
41663e3
fix: remove metrics
salvatore-campagna Nov 27, 2023
b12e5e6
fix: undo interface and method renaming
salvatore-campagna Nov 27, 2023
51ff8b5
fix: just change ordering of docs
salvatore-campagna Nov 27, 2023
ed51100
fix: enable assertion on document id
salvatore-campagna Nov 27, 2023
57a9cdc
fix: forbidded api
salvatore-campagna Nov 27, 2023
09fa281
fix: remove assertion
salvatore-campagna Nov 27, 2023
1e79c3a
fix: do not base64 encode in parseBytesRef
salvatore-campagna Nov 27, 2023
7632bd9
fix: update InternalComposite formatting and parsing of tsid
salvatore-campagna Nov 27, 2023
5f17caf
fix: do not parse the BytesRef
salvatore-campagna Nov 27, 2023
64bb5d7
fix: composite tsid parsing and matching
salvatore-campagna Nov 27, 2023
697d09c
fix: yet another yaml test with tsid
salvatore-campagna Nov 27, 2023
f96a35b
fix: larger byte arrays might result in incorrect Base64 encoding
salvatore-campagna Nov 28, 2023
1e7fbc6
fix: a bunch of tsid values in yaml tests
salvatore-campagna Nov 28, 2023
dc0dd90
fix: some more tsid values
salvatore-campagna Nov 28, 2023
bf94a0f
fix: some more tsid values
salvatore-campagna Nov 28, 2023
28b0732
fix: some tsid values and modified yaml test with bug
salvatore-campagna Nov 28, 2023
b843b66
fix: TsidExtractingIdFieldMapperTests
salvatore-campagna Nov 28, 2023
2d54146
fix: TsidExtractingIdFieldMapperTests and tsid to BytesRef
salvatore-campagna Nov 29, 2023
5e9cce4
fix: Base 64 using larger buffer and new tsid values
salvatore-campagna Nov 29, 2023
b0456ab
fix: some more tests
salvatore-campagna Nov 29, 2023
b4465f1
fix: downsampling and other tests
salvatore-campagna Nov 29, 2023
c12fb39
fix: a few more tests
salvatore-campagna Nov 30, 2023
2a46382
fix: some more yaml tests
salvatore-campagna Nov 30, 2023
cb5fe2e
fix: allow warnings in yaml test
salvatore-campagna Nov 30, 2023
6d54d6a
Bring back the property of the time_series agg to use tsid is bucket …
martijnvg Nov 30, 2023
44d6521
iter
martijnvg Nov 30, 2023
1de5dc9
fix: geoline yaml and code style
salvatore-campagna Nov 30, 2023
0128c9a
fix: a yaml test and a rate aggregation test
salvatore-campagna Nov 30, 2023
0bb9f84
fix: geoline test for time series mode
salvatore-campagna Dec 1, 2023
eef35dc
fix: another yaml test
salvatore-campagna Dec 1, 2023
8f90e58
fix: disable IndexingIT test for versions before 8.12
salvatore-campagna Dec 1, 2023
0bd2dbe
fix: backward compatibility of time series doc values formatting
salvatore-campagna Dec 4, 2023
d8dc94b
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 4, 2023
08714b3
fix: temporarily execute terms aggregation in map mode
salvatore-campagna Dec 4, 2023
83912aa
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 4, 2023
4b8aaa6
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 5, 2023
1ab0603
fix: do not index the dimension field
salvatore-campagna Dec 5, 2023
ac7e24e
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 5, 2023
85be087
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 5, 2023
f62886f
test: temporarily disable flattened fields failing tests
salvatore-campagna Dec 6, 2023
84ef189
fix: address some review comments
salvatore-campagna Dec 7, 2023
c53f66b
fix: rename method
salvatore-campagna Dec 7, 2023
19e50c7
fix: update format comment
salvatore-campagna Dec 7, 2023
8366580
fix: _tsid encoding & decoding
salvatore-campagna Dec 7, 2023
126f877
fix: sort dimensions by name
salvatore-campagna Dec 12, 2023
f3bfc50
fix: temporarily fix the number of indices to 1
salvatore-campagna Dec 12, 2023
3ed3534
fix: update yaml tests skip version
salvatore-campagna Dec 12, 2023
75a161f
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 12, 2023
f8af828
fix: update a couple of tests to version 8.13
salvatore-campagna Dec 12, 2023
926ba0c
fix: another yaml test after switch to 8.13
salvatore-campagna Dec 12, 2023
6803f28
fix: _tsid sort vs _tsid hash sort bucket creation
salvatore-campagna Dec 13, 2023
f712b18
fix: bucket ordering
salvatore-campagna Dec 13, 2023
b433e61
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 13, 2023
1d8f99b
fix: missing skip version for time series test
salvatore-campagna Dec 13, 2023
a638502
test: unnecessary fields and hits
salvatore-campagna Dec 13, 2023
09ef74b
fix: allow empty dimension values
salvatore-campagna Dec 19, 2023
f2a616f
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 19, 2023
3105725
fix: if statetment and additional yaml test
salvatore-campagna Dec 20, 2023
e58e105
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Dec 20, 2023
7084934
fix: typo
salvatore-campagna Dec 20, 2023
9027df9
fix: validate max number of dimensions for versions before 8.13
salvatore-campagna Dec 21, 2023
a612d22
fix: two tests
salvatore-campagna Dec 21, 2023
a7629e8
fix: remove match_all queries
salvatore-campagna Dec 21, 2023
493e8ac
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Jan 8, 2024
0f3620f
fix: address review comments
salvatore-campagna Jan 9, 2024
2c925d3
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Jan 9, 2024
df9cb54
fix: remove unused import
salvatore-campagna Jan 9, 2024
2d748bb
restore original file
salvatore-campagna Jan 9, 2024
a199ef2
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Jan 9, 2024
becde73
fix: remove unused encoder
salvatore-campagna Jan 10, 2024
f523408
fix: catch Throwable to catch AssertionError in non-release builds
salvatore-campagna Jan 10, 2024
7d50542
fix: catch the assertion error when calling utf8ToString
salvatore-campagna Jan 10, 2024
5f1d08b
fix: catch AssertionError while parsing keyword fields
salvatore-campagna Jan 10, 2024
faf76d6
comment: remove comment no longer relevant
salvatore-campagna Jan 10, 2024
cd01b8c
test: include test on number of buckets
salvatore-campagna Jan 15, 2024
d719bed
fix: remove cardinality test and change some settings
salvatore-campagna Jan 16, 2024
1831af3
bring back InternalTimeSeries assertions
martijnvg Jan 18, 2024
196f6bf
fix: checkstyle warnings
salvatore-campagna Jan 23, 2024
466b93e
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Jan 23, 2024
d096959
fix: incorrect variable name
salvatore-campagna Jan 23, 2024
54727e4
fix: indexing pressure too high
salvatore-campagna Jan 23, 2024
464bd36
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Jan 23, 2024
a58f97b
fix: release search response
salvatore-campagna Jan 23, 2024
608ce2d
fix: release responses
salvatore-campagna Jan 23, 2024
4d9a4a4
fix: release responses
salvatore-campagna Jan 23, 2024
acf1678
fix: increase dimension limit default setting
salvatore-campagna Jan 29, 2024
31d4203
fix: total number of fields limit
salvatore-campagna Jan 31, 2024
56c5415
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Feb 1, 2024
ef4f9a2
fix: use a record instead of a class
salvatore-campagna Feb 1, 2024
e3a49ba
fix: spelling hasing vs hashing
salvatore-campagna Feb 1, 2024
b0ab308
fix: update Lucene version
salvatore-campagna Feb 1, 2024
76a38c9
fix: checkstyle warnings
salvatore-campagna Feb 1, 2024
78f30aa
Merge branch 'main' into fix/93564-time-series-dimensions-limit
salvatore-campagna Feb 1, 2024
c53fac5
fix: remove conflict marker
salvatore-campagna Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -76,7 +78,7 @@ public class TimeSeriesAggregationsIT extends AggregationIntegTestCase {

@Override
public void setupSuiteScopeCluster() throws Exception {
int numberOfIndices = randomIntBetween(1, 3);
int numberOfIndices = randomIntBetween(1, 1);
numberOfDimensions = randomIntBetween(1, 5);
numberOfMetrics = randomIntBetween(1, 10);
String[] routingKeys = randomSubsetOf(
Expand Down Expand Up @@ -146,7 +148,7 @@ public void setupSuiteScopeCluster() throws Exception {
for (int i = 0; i < numberOfDocs; i++) {
XContentBuilder docSource = XContentFactory.jsonBuilder();
docSource.startObject();
Map<String, String> key = new HashMap<>();
Map<String, String> key = new TreeMap<>(Comparator.naturalOrder());
for (int d = 0; d < numberOfDimensions; d++) {
String dim = randomFrom(dimensions[d]);
docSource.field("dim_" + d, dim);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.aggregations.bucket;

import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.aggregations.AggregationIntegTestCase;
import org.elasticsearch.aggregations.bucket.timeseries.InternalTimeSeries;
import org.elasticsearch.aggregations.bucket.timeseries.TimeSeriesAggregationBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.CardinalityAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.InternalCardinality;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.junit.Before;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Supplier;

public class TimeSeriesAggregationsUnlimitedDimensionsIT extends AggregationIntegTestCase {
private static int numberOfDimensions;
private static int numberOfDocuments;

@Before
public void setup() throws Exception {
numberOfDimensions = randomIntBetween(25, 99);
final XContentBuilder mapping = timeSeriesIndexMapping();
long startMillis = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis("2023-01-01T00:00:00Z");
long endMillis = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis("2023-01-02T00:00:00Z");
numberOfDocuments = randomIntBetween(100, 200);
final Iterator<Long> timestamps = getTimestamps(startMillis, endMillis, numberOfDocuments);
// NOTE: use also the last (changing) dimension so to make sure documents are not indexed all in the same shard.
final String[] routingDimensions = new String[] { "dim_0", "dim_" + (numberOfDimensions - 1) };
assertTrue(prepareTimeSeriesIndex(mapping, startMillis, endMillis, routingDimensions).isAcknowledged());

logger.info("Dimensions: " + numberOfDimensions + " docs: " + numberOfDocuments + " start: " + startMillis + " end: " + endMillis);

// NOTE: we need the tsid to be larger than 32 Kb
final String fooDimValue = "foo_" + randomAlphaOfLengthBetween(2048, 2048 + 128);
final String barDimValue = "bar_" + randomAlphaOfLengthBetween(2048, 2048 + 256);
final String bazDimValue = "baz_" + randomAlphaOfLengthBetween(2048, 2048 + 512);

final BulkRequestBuilder bulkIndexRequest = client().prepareBulk();
for (int docId = 0; docId < numberOfDocuments; docId++) {
final XContentBuilder document = timeSeriesDocument(fooDimValue, barDimValue, bazDimValue, docId, timestamps::next);
bulkIndexRequest.add(client().prepareIndex("index").setOpType(DocWriteRequest.OpType.CREATE).setSource(document));
}
BulkResponse bulkIndexResponse = bulkIndexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
assertFalse(bulkIndexResponse.hasFailures());
assertEquals(RestStatus.OK.getStatus(), client().admin().indices().prepareFlush("index").get().getStatus().getStatus());
}

private static XContentBuilder timeSeriesDocument(
final String fooDimValue,
final String barDimValue,
final String bazDimValue,
int docId,
final Supplier<Long> timestampSupplier
) throws IOException {
final XContentBuilder docSource = XContentFactory.jsonBuilder();
docSource.startObject();
// NOTE: we assign dimensions in such a way that almost all of them have the same value but the last one.
// This way we are going to have just two time series (and two distinct tsid) and the last dimension identifies
// which time series the document belongs to.
for (int dimId = 0; dimId < numberOfDimensions - 1; dimId++) {
docSource.field("dim_" + dimId, fooDimValue);
}
docSource.field("dim_" + (numberOfDimensions - 1), docId % 2 == 0 ? barDimValue : bazDimValue);
docSource.field("counter_metric", docId + 1);
docSource.field("gauge_metric", randomDoubleBetween(1000.0, 2000.0, true));
docSource.field("@timestamp", timestampSupplier.get());
docSource.endObject();

return docSource;
}

private CreateIndexResponse prepareTimeSeriesIndex(
final XContentBuilder mapping,
long startMillis,
long endMillis,
final String[] routingDimensions
) {
return prepareCreate("index").setSettings(
Settings.builder()
.put("mode", "time_series")
.put("routing_path", String.join(",", routingDimensions))
.put("index.number_of_shards", randomIntBetween(1, 3))
.put("index.number_of_replicas", randomIntBetween(1, 3))
.put("time_series.start_time", startMillis)
.put("time_series.end_time", endMillis)
.put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), 4192)
.build()
).setMapping(mapping).get();
}

private static Iterator<Long> getTimestamps(long startMillis, long endMillis, int numberOfDocs) {
final Set<Long> timestamps = new TreeSet<>();
while (timestamps.size() < numberOfDocs) {
timestamps.add(randomLongBetween(startMillis, endMillis));
}
return timestamps.iterator();
}

private static XContentBuilder timeSeriesIndexMapping() throws IOException {
final XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.startObject("properties");
for (int i = 0; i < numberOfDimensions; i++) {
builder.startObject("dim_" + i);
builder.field("type", "keyword");
builder.field("time_series_dimension", true);
builder.endObject();
}
builder.startObject("counter_metric");
builder.field("type", "double");
builder.field("time_series_metric", "counter");
builder.endObject();
builder.startObject("gauge_metric");
builder.field("type", "double");
builder.field("time_series_metric", "gauge");
builder.endObject();
builder.endObject(); // properties
builder.endObject();
return builder;
}

public void testTimeSeriesAggregation() {
final TimeSeriesAggregationBuilder timeSeries = new TimeSeriesAggregationBuilder("ts");
final SearchResponse aggregationResponse = client().prepareSearch("index").addAggregation(timeSeries).setSize(0).get();
try {
assertTimeSeriesAggregation((InternalTimeSeries) aggregationResponse.getAggregations().asList().get(0));
} finally {
aggregationResponse.decRef();
}
}

public void testSumByTsid() {
final TimeSeriesAggregationBuilder timeSeries = new TimeSeriesAggregationBuilder("ts").subAggregation(
new SumAggregationBuilder("sum").field("gauge_metric")
);
final SearchResponse searchResponse = client().prepareSearch("index").setQuery(new MatchAllQueryBuilder()).get();
assertNotEquals(numberOfDocuments, searchResponse.getHits().getHits().length);
final SearchResponse aggregationResponse = client().prepareSearch("index").addAggregation(timeSeries).setSize(0).get();
try {
assertTimeSeriesAggregation((InternalTimeSeries) aggregationResponse.getAggregations().asList().get(0));
} finally {
searchResponse.decRef();
aggregationResponse.decRef();
}
}

public void testTermsByTsid() {
final TimeSeriesAggregationBuilder timeSeries = new TimeSeriesAggregationBuilder("ts").subAggregation(
new TermsAggregationBuilder("terms").field("dim_0")
);
final SearchResponse aggregationResponse = client().prepareSearch("index").addAggregation(timeSeries).setSize(0).get();
try {
assertTimeSeriesAggregation((InternalTimeSeries) aggregationResponse.getAggregations().asList().get(0));
} finally {
aggregationResponse.decRef();
}
}

public void testDateHistogramByTsid() {
final TimeSeriesAggregationBuilder timeSeries = new TimeSeriesAggregationBuilder("ts").subAggregation(
new DateHistogramAggregationBuilder("date_histogram").field("@timestamp").calendarInterval(DateHistogramInterval.MINUTE)
);
final SearchResponse aggregationResponse = client().prepareSearch("index").addAggregation(timeSeries).setSize(0).get();
try {
assertTimeSeriesAggregation((InternalTimeSeries) aggregationResponse.getAggregations().asList().get(0));
} finally {
aggregationResponse.decRef();
}
}

public void testCardinalityByTsid() {
final TimeSeriesAggregationBuilder timeSeries = new TimeSeriesAggregationBuilder("ts").subAggregation(
new CardinalityAggregationBuilder("dim_n_cardinality").field("dim_" + (numberOfDimensions - 1))
);
final SearchResponse aggregationResponse = client().prepareSearch("index").addAggregation(timeSeries).setSize(0).get();
try {
((InternalTimeSeries) aggregationResponse.getAggregations().get("ts")).getBuckets().forEach(bucket -> {
assertCardinality(bucket.getAggregations().get("dim_n_cardinality"), 1);
});
} finally {
aggregationResponse.decRef();
}
}

private static void assertTimeSeriesAggregation(final InternalTimeSeries timeSeriesAggregation) {
final var dimensions = timeSeriesAggregation.getBuckets().stream().map(InternalTimeSeries.InternalBucket::getKey).toList();
// NOTE: only two time series expected as a result of having just two distinct values for the last dimension
assertEquals(2, dimensions.size());

final Object firstTimeSeries = dimensions.get(0);
final Object secondTimeSeries = dimensions.get(1);

assertNotEquals(firstTimeSeries, secondTimeSeries);
}

private static void assertCardinality(final InternalCardinality cardinalityAggregation, int expectedCardinality) {
assertEquals(expectedCardinality, cardinalityAggregation.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.hamcrest.Matchers;
import org.junit.Before;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Supplier;
Expand Down Expand Up @@ -113,7 +110,6 @@ private CreateIndexResponse prepareTimeSeriesIndex(
.put("index.number_of_replicas", randomIntBetween(1, 3))
.put("time_series.start_time", startMillis)
.put("time_series.end_time", endMillis)
.put(MapperService.INDEX_MAPPING_DIMENSION_FIELDS_LIMIT_SETTING.getKey(), numberOfDimensions + 1)
.put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), 4192)
.build()
).setMapping(mapping).get();
Expand Down Expand Up @@ -209,27 +205,14 @@ public void testCardinalityByTsid() {
}

private static void assertTimeSeriesAggregation(final InternalTimeSeries timeSeriesAggregation) {
final List<Map<String, Object>> dimensions = timeSeriesAggregation.getBuckets()
.stream()
.map(InternalTimeSeries.InternalBucket::getKey)
.toList();
final var dimensions = timeSeriesAggregation.getBuckets().stream().map(InternalTimeSeries.InternalBucket::getKey).toList();
// NOTE: only two time series expected as a result of having just two distinct values for the last dimension
assertEquals(2, dimensions.size());

final Map<String, Object> firstTimeSeries = dimensions.get(0);
final Map<String, Object> secondTimeSeries = dimensions.get(1);
final Object firstTimeSeries = dimensions.get(0);
final Object secondTimeSeries = dimensions.get(1);

assertTsid(firstTimeSeries);
assertTsid(secondTimeSeries);
}

private static void assertTsid(final Map<String, Object> timeSeries) {
timeSeries.entrySet().stream().sorted(Map.Entry.comparingByKey()).limit(numberOfDimensions - 2).forEach(entry -> {
assertThat(entry.getValue().toString(), Matchers.equalTo(FOO_DIM_VALUE));
});
timeSeries.entrySet().stream().sorted(Map.Entry.comparingByKey()).skip(numberOfDimensions - 1).forEach(entry -> {
assertThat(entry.getValue().toString(), Matchers.oneOf(BAR_DIM_VALUE, BAZ_DIM_VALUE));
});
assertNotEquals(firstTimeSeries, secondTimeSeries);
}

private static void assertCardinality(final InternalCardinality cardinalityAggregation, int expectedCardinality) {
Expand Down
Loading