Skip to content

Conversation

@cbuescher
Copy link
Member

This adds parsing of the scripted_metric aggregation that we need for the high level java rest client.
While the parsing code itself is straight forward, it wasn't completely clear which kind of values we
should support to parse back from xContent. The documentation mentions: primitive types, String, Map, Array (containing elements of only the types listed here) so that's what is parsed and tested now. It might be possible that other kinds of values also currently work when using the transport client (e.g. StreamOutput#writeGenericValue() also supports GeoPoint, so that might work via transport but it gets rendered as a json object). I included one test to cover these cases with the example of GeoPoint as well.

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a few minors

@Override
public Object aggregation() {
if (aggregation.size() != 1) {
throw new IllegalStateException("aggregation was not reduced");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this error up-to-date?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, at least in InternalScriptedMetric. I think in the parsed version we can ommit it.

Copy link
Member

@javanna javanna May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea that was my point, it doesn't apply to the parsed version of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left an assert for tests and a comment though, otherwise this might look like a mistake later on.

if (token == XContentParser.Token.VALUE_NULL) {
return null;
} else if (token.isValue()) {
return XContentParserUtils.parseStoredFieldsValue(parser);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it feels weird to use this method from here, given its name, as it was really meant to parse stored fields values based on assumptions made around the possible types we have there. I wouldn't want changes made there to be reflected here in the future. Shall we copy what it does over here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thats what I had before. I agree that a little copy/paste is better than to introduce unneeded dependencies in this case. Glad to change back.


package org.elasticsearch.search.aggregations.metrics.scripted;

import com.google.common.base.Supplier;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this what you meant to use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, good catch. Seems to do the same thing as java.util.function.Supplier apparently though.

() -> new ArrayList<>() };



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove some of these empty lines?



@Override
@Before
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need @before here, the parent method already has it.

}

@SuppressWarnings("unchecked")
private static Object randomValue(Supplier<Object>[] valueTypes, int level) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth having this in RandomObjects instead. But given that it is only used here I don't have a strong opinion on that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is also very specialized given that before dance that creates the suppliers, maybe wise to leave it here for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

assertValues(aggregation.aggregation(), parsed.aggregation());
}

private void assertValues(Object expected, Object actual) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be static?

OBJECT_ARRAY_OR_STRING(START_OBJECT, START_ARRAY, VALUE_STRING),
VALUE(VALUE_BOOLEAN, VALUE_NULL, VALUE_EMBEDDED_OBJECT, VALUE_NUMBER, VALUE_STRING);
VALUE(VALUE_BOOLEAN, VALUE_NULL, VALUE_EMBEDDED_OBJECT, VALUE_NUMBER, VALUE_STRING),
VALUE_OBJECT_ARRAY(VALUE_BOOLEAN, VALUE_NULL, VALUE_EMBEDDED_OBJECT, VALUE_NUMBER, VALUE_STRING, START_OBJECT, START_ARRAY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that we add this to ObjectParser we should add tests for it as part of the object parser tests too. Maybe even commit it upstream from a separate PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this is necessary, those enums are merely combinations of token types and the parsing test added in this PR would fail if this wasn't working (indeed it did before I added this new constant includinf ARRAY_START and OBJECT_START). We also don't use any of the enums other than STRING and FLOAT in ObjectParserTests currently.
As for adding this as a separate PR upstream, that PR would only add an enum thats not used? I think we can add it together with merging the feature branch.

@cbuescher
Copy link
Member Author

@javanna thanks for the review, I pushed a commit that should address most of your comments and left some response for the others.

Copy link
Member

@tlrx tlrx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I left some comments but feel free to implement them or not.

@SuppressWarnings("unchecked")
private static Object randomValue(Supplier<Object>[] valueTypes, int level) {
Supplier<Object> sup = valueTypes[level];
Object value = sup.get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can save a line and go Object value = valueTypes[level].get(); directly

Supplier<Object> sup = valueTypes[level];
Object value = sup.get();
if (value instanceof Map) {
int elements = randomIntBetween(1,5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after comma

if (value instanceof Map) {
int elements = randomIntBetween(1,5);
for (int i = 0; i < elements; i++) {
((Map<String, Object>) value).put(randomAlphaOfLength(5), randomValue(valueTypes, level + 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you cast once before the for loop?

} else if (value instanceof List) {
int elements = randomIntBetween(1,5);
for (int i = 0; i < elements; i++) {
((List<Object>) value).add(randomValue(valueTypes, level + 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, cast before then use in loop?

GeoPoint point = (GeoPoint) expected;
Map<String, Object> pointMap = (Map<String, Object>) actual;
assertEquals(point.getLat(), pointMap.get("lat"));
assertEquals(point.getLon(), pointMap.get("lon"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we have a difference between transport client and our rest client. Would it make sense to add an extra post-parsing treatment for maps that in case of a map of 2 keys lat and lon converts it back to a GeoPoint?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, this won't be the only case but one of many. I don't think we should be forced to treat each of those as a special case in the rest client, the documentation says we don't support them anyway. Maybe @colings86 has an opinion about this though...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should draw a line here. Users need to understand that we are going through REST and some things have to change as the protocol is different. I am not sure that patching this to make things work like with the transport client will work, there will be edge cases that we don't handle anyways. After all it should be easy for people to migrate, and a few users may be hit by this right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, really all we can do is provide a best effort for compatibility between the two and there will inevitably be differences. I think this is a case where the extra effort to make the two the same is not worth it

Copy link
Member

@tlrx tlrx May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for me, I didn't have a strong feeling about it.

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cbuescher cbuescher merged commit 9fc9db2 into elastic:feature/client_aggs_parsing May 17, 2017
javanna pushed a commit to javanna/elasticsearch that referenced this pull request May 23, 2017
@cbuescher cbuescher deleted the addParsing-internalScriptedMetric branch November 27, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants