Update Rollup Caps to allow unknown fields (#38339)

This commit ensures that the parts of rollup caps that can allow unknown
fields will allow them. It also modifies the test such that we can use
the features we need for disallowing fields in spots where they would
not be allowed.

Relates #36938
This commit is contained in:
Michael Basnight 2019-02-05 10:08:08 -06:00 committed by GitHub
parent 0beb3c93d1
commit 8742db3afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 24 deletions

View File

@ -44,7 +44,7 @@ public class RollableIndexCaps implements ToXContentFragment {
public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> { public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ConstructingObjectParser<RollableIndexCaps, Void> p ConstructingObjectParser<RollableIndexCaps, Void> p
= new ConstructingObjectParser<>(indexName, = new ConstructingObjectParser<>(indexName, true,
a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0])); a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0]));
p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply, p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply,

View File

@ -33,7 +33,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.function.Function; import java.util.stream.Collectors;
/** /**
* Represents the Rollup capabilities for a specific job on a single rollup index * Represents the Rollup capabilities for a specific job on a single rollup index
@ -45,15 +45,12 @@ public class RollupJobCaps implements ToXContentObject {
private static final ParseField FIELDS = new ParseField("fields"); private static final ParseField FIELDS = new ParseField("fields");
private static final String NAME = "rollup_job_caps"; private static final String NAME = "rollup_job_caps";
public static final ConstructingObjectParser<RollupJobCaps, Void> PARSER = new ConstructingObjectParser<>(NAME, public static final ConstructingObjectParser<RollupJobCaps, Void> PARSER = new ConstructingObjectParser<>(NAME, true,
a -> { a -> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Tuple<String, RollupFieldCaps>> caps = (List<Tuple<String, RollupFieldCaps>>) a[3]; List<Tuple<String, RollupFieldCaps>> caps = (List<Tuple<String, RollupFieldCaps>>) a[3];
if (caps.isEmpty()) { Map<String, RollupFieldCaps> mapCaps =
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], Collections.emptyMap()); new HashMap<>(caps.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)));
}
Map<String, RollupFieldCaps> mapCaps = new HashMap<>(caps.size());
caps.forEach(c -> mapCaps.put(c.v1(), c.v2()));
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps); return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps);
}); });
@ -140,16 +137,6 @@ public class RollupJobCaps implements ToXContentObject {
private static final String NAME = "rollup_field_caps"; private static final String NAME = "rollup_field_caps";
private final List<Map<String, Object>> aggs; private final List<Map<String, Object>> aggs;
public static final Function<String, ConstructingObjectParser<RollupFieldCaps, Void>> PARSER = fieldName -> {
@SuppressWarnings("unchecked")
ConstructingObjectParser<RollupFieldCaps, Void> parser
= new ConstructingObjectParser<>(NAME, a -> new RollupFieldCaps((List<Map<String, Object>>) a[0]));
parser.declareObjectArray(ConstructingObjectParser.constructorArg(),
(p, c) -> p.map(), new ParseField(fieldName));
return parser;
};
RollupFieldCaps(final List<Map<String, Object>> aggs) { RollupFieldCaps(final List<Map<String, Object>> aggs) {
this.aggs = Collections.unmodifiableList(Objects.requireNonNull(aggs)); this.aggs = Collections.unmodifiableList(Objects.requireNonNull(aggs));
} }
@ -170,13 +157,12 @@ public class RollupJobCaps implements ToXContentObject {
List<Map<String, Object>> aggs = new ArrayList<>(); List<Map<String, Object>> aggs = new ArrayList<>();
if (parser.nextToken().equals(XContentParser.Token.START_ARRAY)) { if (parser.nextToken().equals(XContentParser.Token.START_ARRAY)) {
while (parser.nextToken().equals(XContentParser.Token.START_OBJECT)) { while (parser.nextToken().equals(XContentParser.Token.START_OBJECT)) {
aggs.add(Collections.unmodifiableMap(parser.map())); aggs.add(parser.map());
} }
} }
return new RollupFieldCaps(Collections.unmodifiableList(aggs)); return new RollupFieldCaps(aggs);
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) { if (this == other) {

View File

@ -23,6 +23,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate;
public class GetRollupCapsResponseTests extends RollupCapsResponseTestCase<GetRollupCapsResponse> { public class GetRollupCapsResponseTests extends RollupCapsResponseTestCase<GetRollupCapsResponse> {
@ -40,6 +41,15 @@ public class GetRollupCapsResponseTests extends RollupCapsResponseTestCase<GetRo
builder.endObject(); builder.endObject();
} }
@Override
protected Predicate<String> randomFieldsExcludeFilter() {
return (field) ->
// base cannot have extra things in it
"".equals(field)
// the field list expects to be a nested object of a certain type
|| field.contains("fields");
}
@Override @Override
protected GetRollupCapsResponse fromXContent(XContentParser parser) throws IOException { protected GetRollupCapsResponse fromXContent(XContentParser parser) throws IOException {
return GetRollupCapsResponse.fromXContent(parser); return GetRollupCapsResponse.fromXContent(parser);

View File

@ -23,6 +23,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate;
public class GetRollupIndexCapsResponseTests extends RollupCapsResponseTestCase<GetRollupIndexCapsResponse> { public class GetRollupIndexCapsResponseTests extends RollupCapsResponseTestCase<GetRollupIndexCapsResponse> {
@ -40,6 +41,15 @@ public class GetRollupIndexCapsResponseTests extends RollupCapsResponseTestCase<
builder.endObject(); builder.endObject();
} }
@Override
protected Predicate<String> randomFieldsExcludeFilter() {
return (field) ->
// base cannot have extra things in it
"".equals(field)
// the field list expects to be a nested object of a certain type
|| field.contains("fields");
}
@Override @Override
protected GetRollupIndexCapsResponse fromXContent(XContentParser parser) throws IOException { protected GetRollupIndexCapsResponse fromXContent(XContentParser parser) throws IOException {
return GetRollupIndexCapsResponse.fromXContent(parser); return GetRollupIndexCapsResponse.fromXContent(parser);

View File

@ -25,6 +25,7 @@ import org.elasticsearch.client.rollup.job.config.MetricConfig;
import org.elasticsearch.client.rollup.job.config.RollupJobConfig; import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests; import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
import org.elasticsearch.client.rollup.job.config.TermsGroupConfig; import org.elasticsearch.client.rollup.job.config.TermsGroupConfig;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
@ -40,6 +41,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
@ -55,15 +57,23 @@ abstract class RollupCapsResponseTestCase<T> extends ESTestCase {
protected abstract T fromXContent(XContentParser parser) throws IOException; protected abstract T fromXContent(XContentParser parser) throws IOException;
protected Predicate<String> randomFieldsExcludeFilter() {
return field -> false;
}
protected String[] shuffleFieldsExceptions() {
return Strings.EMPTY_ARRAY;
}
public void testFromXContent() throws IOException { public void testFromXContent() throws IOException {
xContentTester( xContentTester(
this::createParser, this::createParser,
this::createTestInstance, this::createTestInstance,
this::toXContent, this::toXContent,
this::fromXContent) this::fromXContent)
.supportsUnknownFields(false) .supportsUnknownFields(true)
.randomFieldsExcludeFilter(field -> .randomFieldsExcludeFilter(randomFieldsExcludeFilter())
field.endsWith("job_id")) .shuffleFieldsExceptions(shuffleFieldsExceptions())
.test(); .test();
} }