[Rollup] Move getMetadata() methods out of rollup config objects (#32579)

This committ removes the getMetadata() methods from the DateHistoGroupConfig 
and HistoGroupConfig objects. This way the configuration objects do not rely on RollupField.formatMetaField() anymore and do not expose a getMetadata() 
method that is tighlty coupled to the rollup indexer.
This commit is contained in:
Tanguy Leroux 2018-08-24 11:57:46 +02:00 committed by GitHub
parent 66e458b78b
commit 879a90b999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 9 deletions

View File

@ -211,10 +211,6 @@ public class DateHistogramGroupConfig implements Writeable, ToXContentObject {
return map;
}
public Map<String, Object> getMetadata() {
return Collections.singletonMap(RollupField.formatMetaField(RollupField.INTERVAL), interval.toString());
}
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
ActionRequestValidationException validationException) {

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
@ -115,8 +116,8 @@ public class HistogramGroupConfig implements Writeable, ToXContentObject {
return map;
}
public Map<String, Object> getMetadata() {
return Collections.singletonMap(RollupField.formatMetaField(RollupField.INTERVAL), interval);
public Set<String> getAllFields() {
return Arrays.stream(fields).collect(Collectors.toSet());
}
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,

View File

@ -23,6 +23,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xpack.core.rollup.RollupField;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.GroupConfig;
import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.IndexerState;
import org.elasticsearch.xpack.core.rollup.job.RollupJob;
import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig;
@ -392,15 +393,12 @@ public abstract class RollupIndexer {
private CompositeAggregationBuilder createCompositeBuilder(RollupJobConfig config) {
final GroupConfig groupConfig = config.getGroupConfig();
List<CompositeValuesSourceBuilder<?>> builders = new ArrayList<>();
Map<String, Object> metadata = new HashMap<>();
// Add all the agg builders to our request in order: date_histo -> histo -> terms
if (groupConfig != null) {
builders.addAll(groupConfig.getDateHistogram().toBuilders());
metadata.putAll(groupConfig.getDateHistogram().getMetadata());
if (groupConfig.getHistogram() != null) {
builders.addAll(groupConfig.getHistogram().toBuilders());
metadata.putAll(groupConfig.getHistogram().getMetadata());
}
if (groupConfig.getTerms() != null) {
builders.addAll(groupConfig.getTerms().toBuilders());
@ -409,6 +407,8 @@ public abstract class RollupIndexer {
CompositeAggregationBuilder composite = new CompositeAggregationBuilder(AGGREGATION_NAME, builders);
config.getMetricsConfig().forEach(m -> m.toBuilders().forEach(composite::subAggregation));
final Map<String, Object> metadata = createMetadata(groupConfig);
if (metadata.isEmpty() == false) {
composite.setMetaData(metadata);
}
@ -441,5 +441,20 @@ public abstract class RollupIndexer {
.format("epoch_millis");
return query;
}
static Map<String, Object> createMetadata(final GroupConfig groupConfig) {
final Map<String, Object> metadata = new HashMap<>();
if (groupConfig != null) {
// Add all the metadata in order: date_histo -> histo
final DateHistogramGroupConfig dateHistogram = groupConfig.getDateHistogram();
metadata.put(RollupField.formatMetaField(RollupField.INTERVAL), dateHistogram.getInterval().toString());
final HistogramGroupConfig histogram = groupConfig.getHistogram();
if (histogram != null) {
metadata.put(RollupField.formatMetaField(RollupField.INTERVAL), histogram.getInterval());
}
}
return metadata;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.rollup.job;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.GroupConfig;
import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
public class RollupIndexerTests extends ESTestCase {
public void testCreateMetadataNoGroupConfig() {
final Map<String, Object> metadata = RollupIndexer.createMetadata(null);
assertNotNull(metadata);
assertTrue(metadata.isEmpty());
}
public void testCreateMetadataWithDateHistogramGroupConfigOnly() {
final DateHistogramGroupConfig dateHistogram = ConfigTestHelpers.randomDateHistogramGroupConfig(random());
final GroupConfig groupConfig = new GroupConfig(dateHistogram);
final Map<String, Object> metadata = RollupIndexer.createMetadata(groupConfig);
assertEquals(1, metadata.size());
assertTrue(metadata.containsKey("_rollup.interval"));
Object value = metadata.get("_rollup.interval");
assertThat(value, equalTo(dateHistogram.getInterval().toString()));
}
public void testCreateMetadata() {
final DateHistogramGroupConfig dateHistogram = ConfigTestHelpers.randomDateHistogramGroupConfig(random());
final HistogramGroupConfig histogram = ConfigTestHelpers.randomHistogramGroupConfig(random());
final GroupConfig groupConfig = new GroupConfig(dateHistogram, histogram, null);
final Map<String, Object> metadata = RollupIndexer.createMetadata(groupConfig);
assertEquals(1, metadata.size());
assertTrue(metadata.containsKey("_rollup.interval"));
Object value = metadata.get("_rollup.interval");
assertThat(value, equalTo(histogram.getInterval()));
}
}