Skip to content

Commit

Permalink
Update Rollup Caps to allow unknown fields (#38446)
Browse files Browse the repository at this point in the history
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
Backport of #38339
  • Loading branch information
hub-cap authored Feb 5, 2019
1 parent 3e0819a commit 6d16374
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class RollableIndexCaps implements ToXContentFragment {
public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> {
@SuppressWarnings("unchecked")
ConstructingObjectParser<RollableIndexCaps, Void> p
= new ConstructingObjectParser<>(indexName,
= new ConstructingObjectParser<>(indexName, true,
a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0]));

p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.List;
import java.util.Map;
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
Expand All @@ -45,15 +45,12 @@ public class RollupJobCaps implements ToXContentObject {
private static final ParseField FIELDS = new ParseField("fields");
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 -> {
@SuppressWarnings("unchecked")
List<Tuple<String, RollupFieldCaps>> caps = (List<Tuple<String, RollupFieldCaps>>) a[3];
if (caps.isEmpty()) {
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], Collections.emptyMap());
}
Map<String, RollupFieldCaps> mapCaps = new HashMap<>(caps.size());
caps.forEach(c -> mapCaps.put(c.v1(), c.v2()));
Map<String, RollupFieldCaps> mapCaps =
new HashMap<>(caps.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)));
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps);
});

Expand Down Expand Up @@ -140,16 +137,6 @@ public static class RollupFieldCaps implements ToXContentFragment {
private static final String NAME = "rollup_field_caps";
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) {
this.aggs = Collections.unmodifiableList(Objects.requireNonNull(aggs));
}
Expand All @@ -170,13 +157,12 @@ public static RollupFieldCaps fromXContent(XContentParser parser) throws IOExcep
List<Map<String, Object>> aggs = new ArrayList<>();
if (parser.nextToken().equals(XContentParser.Token.START_ARRAY)) {
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
public boolean equals(Object other) {
if (this == other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.util.Map;
import java.util.function.Predicate;

public class GetRollupCapsResponseTests extends RollupCapsResponseTestCase<GetRollupCapsResponse> {

Expand All @@ -40,6 +41,15 @@ protected void toXContent(GetRollupCapsResponse response, XContentBuilder builde
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
protected GetRollupCapsResponse fromXContent(XContentParser parser) throws IOException {
return GetRollupCapsResponse.fromXContent(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.util.Map;
import java.util.function.Predicate;

public class GetRollupIndexCapsResponseTests extends RollupCapsResponseTestCase<GetRollupIndexCapsResponse> {

Expand All @@ -40,6 +41,15 @@ protected void toXContent(GetRollupIndexCapsResponse response, XContentBuilder b
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
protected GetRollupIndexCapsResponse fromXContent(XContentParser parser) throws IOException {
return GetRollupIndexCapsResponse.fromXContent(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
import org.elasticsearch.client.rollup.job.config.TermsGroupConfig;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
Expand All @@ -40,6 +41,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.util.Collections.singletonMap;
Expand All @@ -55,15 +57,23 @@ abstract class RollupCapsResponseTestCase<T> extends ESTestCase {

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 {
xContentTester(
this::createParser,
this::createTestInstance,
this::toXContent,
this::fromXContent)
.supportsUnknownFields(false)
.randomFieldsExcludeFilter(field ->
field.endsWith("job_id"))
.supportsUnknownFields(true)
.randomFieldsExcludeFilter(randomFieldsExcludeFilter())
.shuffleFieldsExceptions(shuffleFieldsExceptions())
.test();
}

Expand Down

0 comments on commit 6d16374

Please sign in to comment.