通过JSON查询在Java客户端中进行聚合-无需AggregationBuilder [英] Aggregations in Java client through JSON query - without AggregationBuilder

查看:120
本文介绍了通过JSON查询在Java客户端中进行聚合-无需AggregationBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过基于HTTP的 JEST 客户端中的 JSON 查询实现 aggregation 功能,但不能在 TCP 中实现基于Java的客户端.

I am able to implement aggregation functionality via JSON query in HTTP based JEST client but not in TCP based Java client.

通过 JEST 客户端(基于HTTP REST),可以通过查询字符串实现聚合.
JEST示例代码:

Through JEST client (HTTP REST based) it is possible to implement aggregation through query String.
JEST sample code:

        JestClientFactory factory = new JestClientFactory();
        HttpClientConfig httpClientConfig = new HttpClientConfig
                                    .Builder("http://localhost:9201")
                                    .build();
        factory.setHttpClientConfig(httpClientConfig);
        JestClient client = factory.getObject();

        String queryString ="{\"query\":{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }}";

        Search.Builder searchBuilder = new Search.Builder(queryString)
.addIndex("st1index")
    .addType("st1type");  

        SearchResult response = client.execute(searchBuilder.build());

        System.out.println(response.getJsonString());

        client.shutdownClient();

JEST客户端的打印响应显示聚合结果.

Printing response of JEST client shows aggregation results.

elasticsearch 中使用 TCP客户端,可以通过 AggregationBuilder 进行 aggregation .

Using TCP client in elasticsearch, aggregation is possible through AggregationBuilder.

当我尝试在 TCP 中实现JSON查询时,它没有返回聚合结果.

When I tried to implement JSON query in TCP, it did not return aggregation results.

TCP是否不支持通过查询字符串进行聚合但支持添加聚合选项的任何原因?

Is there any reason why TCP do not support aggregation through query string but supports with adding aggregation options?

TCP Java客户端示例代码:

已编辑删除了围绕queryString的 WrapperQueryBuilder .

Edited Removed WrapperQueryBuilder surrounding the queryString.

Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "javaEscluster")
                .put("node.name", "arivu").build();
Client client = new TransportClient(settings)
     .addTransportAddress(new InetSocketTransportAddress("localhost", 9303));

String queryString ="{\"match_all\": {},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }}";

SearchResponse response = client.prepareSearch("st1index").setTypes("st1type").setQuery(queryString).execute().actionGet();

System.out.println("Getresponse-->" +"Index-->"+ response.toString());

//closing node
client.close();
System.out.println("completed");

此代码仅检索搜索结果和空的聚合结果数据.

This code retrieves only search results and empty aggregation result data.

任何解释原因的参考资料都将是非常好的.

Any reference material which explains the reason would be great.

推荐答案

In the main documentation of the WrapperQueryBuilder class, it is stated:

一个查询构建器,它允许在给定作为输入提供的JSON字符串或二进制数据的情况下构建查询.当您要使用Java Builder API但仍要与其他查询构建器结合使用的JSON查询字符串时,此功能很有用.

A Query builder which allows building a query given JSON string or binary data provided as input. This is useful when you want to use the Java Builder API but still have JSON query strings at hand that you want to combine with other query builders.

此处的关键字是单词 query ,即您发送到ES _search 端点的请求中名为 query 的部分,即:

The keyword in here is the word query, i.e. the part named query in the request you send to the ES _search endpoint, i.e.:

{
    "sort": {
       ...          <--- whatever sorting definition you have goes here
    },
    "_source": {
       ...          <--- whatever source definition you have goes here
    },
    "query": {
       ...          <--- this is the content you can use with WrapperQueryBuilder
    },
    "aggs": {
       ...          <--- whatever aggs definition you have goes here
    }
}

WrapperQueryBuilder 只会考虑您可以在该 query 部分中使用的内容,因此您可以看到其中不包含聚合,而聚合是另一个顶级部分.

WrapperQueryBuilder will only ever consider whatever you can fit inside that query section, so as you can see that doesn't include aggregations, which are in another top-level section of the request.

因此,在您提供的JSON查询字符串中,将仅考虑 match_all ,因为这是允许在 query 部分中出现的唯一有效令牌, aggs:{...} 部分不是.

So, in the JSON query string you give, only the match_all will be considered, because that's the only valid token that is allowed to appear in the query section, the aggs:{...} part is not.

"{\"match_all\": {},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }}"
     ^                 ^
     |                 |
this is valid        this is NOT valid

这篇关于通过JSON查询在Java客户端中进行聚合-无需AggregationBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆