-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rollup] Remove builders from TermsGroupConfig #32507
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,10 @@ | |
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 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,27 +43,39 @@ | |
* ] | ||
* } | ||
*/ | ||
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; | ||
} | ||
|
||
TermsGroupConfig(StreamInput in) throws IOException { | ||
fields = in.readStringArray(); | ||
} | ||
|
||
/** | ||
* @return the names of the fields. Never {@code null}. | ||
*/ | ||
public String[] getFields() { | ||
return fields; | ||
} | ||
|
@@ -72,10 +85,6 @@ public String[] getFields() { | |
* 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 Map<String, Object> toAggCap() { | |
return map; | ||
} | ||
|
||
public Map<String, Object> getMetadata() { | ||
return Collections.emptyMap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note: this is a no-op for Terms, but the other groups (date, histo) use it to store the interval. So we'll need to preserve that functionality somehow/somewhere for the other groups There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, we'll keep this for the other groups. |
||
} | ||
|
||
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 void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa | |
|
||
@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 void writeTo(StreamOutput out) throws IOException { | |
} | ||
|
||
@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 String toString() { | |
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])); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also remove the other builders to be consistent. WDYT @polyfractal ? I can do that in a follow up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree and I'll take care of this if that's OK. I already have most of the changes in a local branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. go for it, thanks @tlrx ! |
||
public static TermsGroupConfig fromXContent(final XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other configs should also implement ToXContentObject and change their
toXContent
but let's do that in a follow up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree and I'll take care of this if that's OK. I already have most of the changes in a local branch.