elasticsearch 通过JSON查询在Java客户端中进行聚合-无AggregationBuilder
我能够通过基于HTTP的
elasticsearch 通过JSON查询在Java客户端中进行聚合-无AggregationBuilder,
elasticsearch,tcp,
elasticsearch,Tcp,我能够通过基于HTTP的JEST客户端中的JSON查询实现聚合功能,但不能在基于TCP的Java客户端中实现 通过JEST客户端(基于HTTP REST),可以通过查询字符串实现聚合 笑话示例代码: JestClientFactory factory = new JestClientFactory(); HttpClientConfig httpClientConfig = new HttpClientConfig
JEST
客户端中的JSON
查询实现聚合
功能,但不能在基于TCP
的Java客户端中实现
通过JEST
客户端(基于HTTP REST),可以通过查询字符串实现聚合笑话示例代码:
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客户端的响应显示聚合结果
在
elasticsearch
中使用TCP客户端
,可以通过AggregationBuilder
进行聚合
当我尝试在TCP
中实现JSON查询时,它没有返回聚合结果
TCP不支持通过查询字符串进行聚合,但支持添加聚合选项,这有什么原因吗
TCP Java客户端示例代码:
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();
已编辑
已删除queryString周围的WrapperQueryBuilder
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");
此代码仅检索搜索结果和空聚合结果数据
编辑:
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();
任何能解释原因的参考资料都是很好的 在课程的主要文档中,说明: 一个查询生成器,允许在给定JSON字符串或二进制数据作为输入的情况下构建查询。当您希望使用Java Builder API,但手头仍有JSON查询字符串,希望与其他查询生成器组合时,这非常有用 这里的关键字是单词query,即发送到ES
\u search
端点的请求中名为query
的部分,即:
{
"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的情况下更新代码,但却错放在了这个地方。很抱歉,如果不使用WrapperQueryBuilder,您在这个问题上的建议会给您带来不便。根本没有办法从字符串构建聚合,就像您对查询部分所做的那样。-@Val,要添加更多详细信息,在我们的应用程序中,我们可以互换使用TCP和基于HTTP的客户端。因为JEST客户端支持基于查询的聚合,而TCP Java客户端不支持。所以,我需要任何有文档记录的参考资料来证明TCP不支持这一点。这不是TCP与HTTP的问题,只是Elasticsearch Java API不支持这一点。您的HTTP客户端是基于Jest的,这是一个提供不同JavaAPI的不同项目。