|
@@ -12,9 +12,10 @@ import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.Writeable;
|
|
|
-import org.elasticsearch.common.xcontent.ObjectParser;
|
|
|
-import org.elasticsearch.common.xcontent.ToXContentFragment;
|
|
|
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContentObject;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.index.mapper.KeywordFieldMapper;
|
|
|
import org.elasticsearch.index.mapper.TextFieldMapper;
|
|
|
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
|
|
@@ -24,13 +25,13 @@ import org.elasticsearch.xpack.core.rollup.RollupField;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
|
|
|
+
|
|
|
/**
|
|
|
* The configuration object for the histograms in the rollup config
|
|
|
*
|
|
@@ -42,20 +43,29 @@ import java.util.stream.Collectors;
|
|
|
* ]
|
|
|
* }
|
|
|
*/
|
|
|
-public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
- private static final String NAME = "term_group_config";
|
|
|
- public static final ObjectParser<TermsGroupConfig.Builder, Void> PARSER = new ObjectParser<>(NAME, TermsGroupConfig.Builder::new);
|
|
|
+public class TermsGroupConfig implements Writeable, ToXContentObject {
|
|
|
+
|
|
|
+ private static final String NAME = "terms";
|
|
|
+ private static final String FIELDS = "fields";
|
|
|
|
|
|
- private static final ParseField FIELDS = new ParseField("fields");
|
|
|
private static final List<String> FLOAT_TYPES = Arrays.asList("half_float", "float", "double", "scaled_float");
|
|
|
private static final List<String> NATURAL_TYPES = Arrays.asList("byte", "short", "integer", "long");
|
|
|
- private final String[] fields;
|
|
|
|
|
|
+ private static final ConstructingObjectParser<TermsGroupConfig, Void> PARSER;
|
|
|
static {
|
|
|
- PARSER.declareStringArray(TermsGroupConfig.Builder::setFields, FIELDS);
|
|
|
+ PARSER = new ConstructingObjectParser<>(NAME, args -> {
|
|
|
+ @SuppressWarnings("unchecked") List<String> fields = (List<String>) args[0];
|
|
|
+ return new TermsGroupConfig(fields != null ? fields.toArray(new String[fields.size()]) : null);
|
|
|
+ });
|
|
|
+ PARSER.declareStringArray(constructorArg(), new ParseField(FIELDS));
|
|
|
}
|
|
|
|
|
|
- private TermsGroupConfig(String[] fields) {
|
|
|
+ private final String[] fields;
|
|
|
+
|
|
|
+ public TermsGroupConfig(final String... fields) {
|
|
|
+ if (fields == null || fields.length == 0) {
|
|
|
+ throw new IllegalArgumentException("Fields must have at least one value");
|
|
|
+ }
|
|
|
this.fields = fields;
|
|
|
}
|
|
|
|
|
@@ -63,6 +73,9 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
fields = in.readStringArray();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @return the names of the fields. Never {@code null}.
|
|
|
+ */
|
|
|
public String[] getFields() {
|
|
|
return fields;
|
|
|
}
|
|
@@ -72,10 +85,6 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
* set of date histograms. Used by the rollup indexer to iterate over historical data
|
|
|
*/
|
|
|
public List<CompositeValuesSourceBuilder<?>> toBuilders() {
|
|
|
- if (fields.length == 0) {
|
|
|
- return Collections.emptyList();
|
|
|
- }
|
|
|
-
|
|
|
return Arrays.stream(fields).map(f -> {
|
|
|
TermsValuesSourceBuilder vsBuilder
|
|
|
= new TermsValuesSourceBuilder(RollupField.formatIndexerAggName(f, TermsAggregationBuilder.NAME));
|
|
@@ -94,14 +103,6 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- public Map<String, Object> getMetadata() {
|
|
|
- return Collections.emptyMap();
|
|
|
- }
|
|
|
-
|
|
|
- public Set<String> getAllFields() {
|
|
|
- return Arrays.stream(fields).collect(Collectors.toSet());
|
|
|
- }
|
|
|
-
|
|
|
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
|
|
|
ActionRequestValidationException validationException) {
|
|
|
|
|
@@ -138,8 +139,11 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
|
|
|
@Override
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
- builder.field(FIELDS.getPreferredName(), fields);
|
|
|
- return builder;
|
|
|
+ builder.startObject();
|
|
|
+ {
|
|
|
+ builder.field(FIELDS, fields);
|
|
|
+ }
|
|
|
+ return builder.endObject();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -148,18 +152,15 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean equals(Object other) {
|
|
|
+ public boolean equals(final Object other) {
|
|
|
if (this == other) {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
if (other == null || getClass() != other.getClass()) {
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
- TermsGroupConfig that = (TermsGroupConfig) other;
|
|
|
-
|
|
|
- return Arrays.equals(this.fields, that.fields);
|
|
|
+ final TermsGroupConfig that = (TermsGroupConfig) other;
|
|
|
+ return Arrays.equals(fields, that.fields);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -172,23 +173,7 @@ public class TermsGroupConfig implements Writeable, ToXContentFragment {
|
|
|
return Strings.toString(this, true, true);
|
|
|
}
|
|
|
|
|
|
- public static class Builder {
|
|
|
- private List<String> fields;
|
|
|
-
|
|
|
- public List<String> getFields() {
|
|
|
- return fields;
|
|
|
- }
|
|
|
-
|
|
|
- public TermsGroupConfig.Builder setFields(List<String> fields) {
|
|
|
- this.fields = fields;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public TermsGroupConfig build() {
|
|
|
- if (fields == null || fields.isEmpty()) {
|
|
|
- throw new IllegalArgumentException("Parameter [" + FIELDS + "] must have at least one value.");
|
|
|
- }
|
|
|
- return new TermsGroupConfig(fields.toArray(new String[0]));
|
|
|
- }
|
|
|
+ public static TermsGroupConfig fromXContent(final XContentParser parser) throws IOException {
|
|
|
+ return PARSER.parse(parser, null);
|
|
|
}
|
|
|
}
|