Skip to content
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

Unable to use WeightedAvg aggregation with Rest High Level Client #36474

Closed
tKe opened this issue Dec 11, 2018 · 5 comments
Closed

Unable to use WeightedAvg aggregation with Rest High Level Client #36474

tKe opened this issue Dec 11, 2018 · 5 comments

Comments

@tKe
Copy link

tKe commented Dec 11, 2018

Elasticsearch version (bin/elasticsearch --version):
6.5.2

Plugins installed:
N/A

JVM version (java -version):
1.8.0_172

OS version (uname -a if on a Unix-like system):
Linux 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Description of the problem including expected versus actual behavior:
Adding a weighted_avg aggregation to a search source results in an exception when marshalling the source:
com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value

Steps to reproduce:

A minimal test case (based on the example weighted_avg usage in the documentation) for this is as follows:

import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

public class TestCase {
    @Test
    public void weightedAverageSerialization() throws JSONException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .size(0)
                .aggregation(
                        AggregationBuilders.weightedAvg("weighted_grade")
                                .value(new MultiValuesSourceFieldConfig.Builder()
                                        .setFieldName("grade")
                                        .build())
                                .weight(new MultiValuesSourceFieldConfig.Builder()
                                        .setFieldName("weight")
                                        .build())
                );

        String actualStr = sourceBuilder.toString();
        JSONAssert.assertEquals("{\n" +
                "  \"size\": 0,\n" +
                "  \"aggregations\" : {\n" +
                "    \"weighted_grade\": {\n" +
                "      \"weighted_avg\": {\n" +
                "        \"value\": {\n" +
                "          \"field\": \"grade\"\n" +
                "        },\n" +
                "        \"weight\": {\n" +
                "          \"field\": \"weight\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}", actualStr, JSONCompareMode.LENIENT);
    }
}

Provide logs (if relevant):

ElasticsearchException[com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
]; nested: JsonGenerationException[Can not write a field name, expecting a value];
	at org.elasticsearch.search.builder.SearchSourceBuilder.toString(SearchSourceBuilder.java:1565)
	at org.elasticsearch.search.builder.SearchSourceBuilder.toString(SearchSourceBuilder.java:1558)
	at TestCase.weightedAverageSerialization(TestCase.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
	at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1961)
	at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:188)
	at com.fasterxml.jackson.core.json.JsonGeneratorImpl.writeStringField(JsonGeneratorImpl.java:202)
	at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeStringField(JsonXContentGenerator.java:276)
	at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:636)
	at org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig.toXContent(MultiValuesSourceFieldConfig.java:120)
	at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:857)
	at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:850)
	at org.elasticsearch.common.xcontent.XContentBuilder.unknownValue(XContentBuilder.java:828)
	at org.elasticsearch.common.xcontent.XContentBuilder.map(XContentBuilder.java:888)
	at org.elasticsearch.common.xcontent.XContentBuilder.unknownValue(XContentBuilder.java:822)
	at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:804)
	at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:789)
	at org.elasticsearch.search.aggregations.support.MultiValuesSourceAggregationBuilder.internalXContent(MultiValuesSourceAggregationBuilder.java:228)
	at org.elasticsearch.search.aggregations.AbstractAggregationBuilder.toXContent(AbstractAggregationBuilder.java:154)
	at org.elasticsearch.search.aggregations.AggregatorFactories$Builder.toXContent(AggregatorFactories.java:448)
	at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:857)
	at org.elasticsearch.common.xcontent.XContentBuilder.value(XContentBuilder.java:850)
	at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:842)
	at org.elasticsearch.search.builder.SearchSourceBuilder.innerToXContent(SearchSourceBuilder.java:1269)
	at org.elasticsearch.search.builder.SearchSourceBuilder.toXContent(SearchSourceBuilder.java:1309)
	at org.elasticsearch.common.xcontent.XContentHelper.toXContent(XContentHelper.java:349)
	at org.elasticsearch.search.builder.SearchSourceBuilder.toString(SearchSourceBuilder.java:1563)
	... 41 more
	Suppressed: java.lang.IllegalStateException: Failed to close the XContentBuilder
		at org.elasticsearch.common.xcontent.XContentBuilder.close(XContentBuilder.java:1002)
		at org.elasticsearch.common.xcontent.XContentHelper.toXContent(XContentHelper.java:344)
		... 42 more
	Caused by: java.io.IOException: Unclosed object or array found
		at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.close(JsonXContentGenerator.java:469)
		at org.elasticsearch.common.xcontent.XContentBuilder.close(XContentBuilder.java:1000)
		... 43 more
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features

@matriv matriv added the >bug label Dec 11, 2018
@matriv
Copy link
Contributor

matriv commented Dec 11, 2018

Can still reproduce it on master.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo

@talevy talevy self-assigned this Dec 12, 2018
talevy added a commit to talevy/elasticsearch that referenced this issue Dec 13, 2018
This commit turns MultiValuesSourceFieldConfig into a proper
ToXContentObject for easy testing and verification of its
to/from XContent methods.

Closes elastic#36474.
talevy added a commit that referenced this issue Dec 13, 2018
This commit turns MultiValuesSourceFieldConfig into a proper
ToXContentObject for easy testing and verification of its
to/from XContent methods.

Closes #36474.
@batjko
Copy link

batjko commented Dec 13, 2018

Nice turnaround time! 👍

talevy added a commit that referenced this issue Dec 13, 2018
This commit turns MultiValuesSourceFieldConfig into a proper
ToXContentObject for easy testing and verification of its
to/from XContent methods.

Closes #36474.
talevy added a commit that referenced this issue Dec 13, 2018
This commit turns MultiValuesSourceFieldConfig into a proper
ToXContentObject for easy testing and verification of its
to/from XContent methods.

Closes #36474.
@talevy
Copy link
Contributor

talevy commented Dec 13, 2018

@tKe thanks for the report. Fix for this was pushed and should be made available in the versions tagged on the Pull Request. Hope all is resolved for you after upgrading when they are made available!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants