Elasticsearch复合聚合抛出“[x_content_parse_exception] [1:131] [composite] failed to parse field [sources]”

xurqigkl  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(398)

**Context:**在本地Elasticsearch 8.6.1部署上运行测试,使用弹性Java客户端(同版本)。索引Map非常简单-每个文档中有两个关键字字段(“strKey 1”和“strKey 1”)。
**问题:**我所有的复合聚合搜索查询都抛出异常:

[es/search] failed: [x_content_parse_exception] [1:131] [composite] failed to parse field [sources]
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [x_content_parse_exception] [1:131] [composite] failed to parse field [sources]
    at app//co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:282)
    at app//co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:148)
    at app//co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1833)

查询实现为:

var rqst = SearchRequest.of(b -> b
        .index(coords.getIndexName())
        .size(1000)
        .query(query)
        .aggregations("aggre", a -> a
                .composite(ca -> ca
                        .size(100)
                        .sources(Map.of("strKey1", strKey1, "strKey2", strKey2))
                )
        )
);
client.search(rqst, Void.class);

我查看了这个查询转换的实际http请求,看起来不错:

SearchRequest: POST /the_index/_search?typed_keys=true {"aggregations":{"aggre":{"composite":{"size":100,"sources":[{"strKey2":{"terms":{"field":"strKey2"}},"strKey1":{"terms":{"field":"strKey1"}}}]}}},"query":{"match_all":{}},"size":1000}

尝试使用curl手动执行此请求,成功了!返回预期的聚合结果!
我还通过Java客户端尝试了其他聚合类型(例如multiTerms),它们都按预期工作。
我做错了什么?Java客户端中的复合聚合是否存在特定的问题?

gdrx4gfi

gdrx4gfi1#

所以,这个错误很愚蠢。
您应该在单独的Map对象中设置每个源:

.composite(ca -> ca
        .size(100)
        .sources(Map.of("strKey1", strKey1), Map.of("strKey2", strKey2))
)

而不是:

.composite(ca -> ca
        .size(100)
        .sources(Map.of("strKey1", strKey1, "strKey2", strKey2))
)

相关问题