This commit is contained in:
Andrei Stefan 2018-08-24 13:14:37 +03:00
commit a2f0a1a0cb
4 changed files with 70 additions and 9 deletions

View File

@ -211,10 +211,6 @@ public class DateHistogramGroupConfig implements Writeable, ToXContentObject {
return map; 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, public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
ActionRequestValidationException validationException) { ActionRequestValidationException validationException) {

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
@ -115,8 +116,8 @@ public class HistogramGroupConfig implements Writeable, ToXContentObject {
return map; return map;
} }
public Map<String, Object> getMetadata() { public Set<String> getAllFields() {
return Collections.singletonMap(RollupField.formatMetaField(RollupField.INTERVAL), interval); return Arrays.stream(fields).collect(Collectors.toSet());
} }
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse, 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.RollupField;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig; import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.GroupConfig; 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.IndexerState;
import org.elasticsearch.xpack.core.rollup.job.RollupJob; import org.elasticsearch.xpack.core.rollup.job.RollupJob;
import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig; import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig;
@ -392,15 +393,12 @@ public abstract class RollupIndexer {
private CompositeAggregationBuilder createCompositeBuilder(RollupJobConfig config) { private CompositeAggregationBuilder createCompositeBuilder(RollupJobConfig config) {
final GroupConfig groupConfig = config.getGroupConfig(); final GroupConfig groupConfig = config.getGroupConfig();
List<CompositeValuesSourceBuilder<?>> builders = new ArrayList<>(); 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 // Add all the agg builders to our request in order: date_histo -> histo -> terms
if (groupConfig != null) { if (groupConfig != null) {
builders.addAll(groupConfig.getDateHistogram().toBuilders()); builders.addAll(groupConfig.getDateHistogram().toBuilders());
metadata.putAll(groupConfig.getDateHistogram().getMetadata());
if (groupConfig.getHistogram() != null) { if (groupConfig.getHistogram() != null) {
builders.addAll(groupConfig.getHistogram().toBuilders()); builders.addAll(groupConfig.getHistogram().toBuilders());
metadata.putAll(groupConfig.getHistogram().getMetadata());
} }
if (groupConfig.getTerms() != null) { if (groupConfig.getTerms() != null) {
builders.addAll(groupConfig.getTerms().toBuilders()); builders.addAll(groupConfig.getTerms().toBuilders());
@ -409,6 +407,8 @@ public abstract class RollupIndexer {
CompositeAggregationBuilder composite = new CompositeAggregationBuilder(AGGREGATION_NAME, builders); CompositeAggregationBuilder composite = new CompositeAggregationBuilder(AGGREGATION_NAME, builders);
config.getMetricsConfig().forEach(m -> m.toBuilders().forEach(composite::subAggregation)); config.getMetricsConfig().forEach(m -> m.toBuilders().forEach(composite::subAggregation));
final Map<String, Object> metadata = createMetadata(groupConfig);
if (metadata.isEmpty() == false) { if (metadata.isEmpty() == false) {
composite.setMetaData(metadata); composite.setMetaData(metadata);
} }
@ -441,5 +441,20 @@ public abstract class RollupIndexer {
.format("epoch_millis"); .format("epoch_millis");
return query; 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()));
}
}