如何使用 spring-data-elasticsearch 中的聚合获得 elasticsearch json 响应?
How to get the elasticsearch json response using aggregations in spring-data-elasticsearch?
我有以下内容:
我注意到在 运行 代码的末尾,如果我打印出 aggregations.asMap().get('subjects');
我正进入(状态:
org.elasticsearch.search.aggregations.bucket.terms.StringTerms@6cff59fa
打印出 "aggregations" 得到:org.elasticsearch.search.aggregations.InternalAggregations@65cf321d
我真正想要的是整个 string/json 响应,如果您在 elasticsearch 上卷曲以获取聚合,通常会返回该响应。如何从聚合查询中获取原始响应?另外,有没有办法迭代并打印出那些 "wrapped up" 对象中的内容?
@Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
// given
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("articles").withTypes("article")
.addAggregation(terms("subjects").field("subject"))
.build();
// when
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
// then
System.out.println(aggregations); // gives me some cryptic InternalAggregations object, how do I get to the raw JSON normally returned by elasticsearch?
System.out.println(aggregations.asMap().get("subjects")); // gives me some StringTerms object I have no idea how to iterate over to get results
}
您无法通过这种方式获得原始 JSON 响应,因为 Spring Data Elasticsearch 会负责为您解析它,这就是重点。
如果你需要解析那些桶,你可以很容易地这样做:
...
StringTerms subjects = aggregations.asMap().get("subjects");
for (Terms.Bucket bucket : subjects.getBuckets()) {
String key = bucket.getKey();
long docCount = bucket.getDocCount();
// do something with the key and the doc count
}
如果你真的想看到 JSON 被返回,你可以做的是使用序列化将解析的 Aggregations 对象重新写入 JSON ,但这实际上没有帮助:
InternalAggregations aggregations = ...;
XContentBuilder jsonBuilder = JsonXContent.contentBuilder();
aggregations.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
String rawJson = jsonBuilder.string();
将 EsRequest 的大小设置为零
获取Esresponse.toString()
将字符串转换为 Json
从Json获取聚合字段。
我有以下内容: 我注意到在 运行 代码的末尾,如果我打印出 aggregations.asMap().get('subjects'); 我正进入(状态: org.elasticsearch.search.aggregations.bucket.terms.StringTerms@6cff59fa
打印出 "aggregations" 得到:org.elasticsearch.search.aggregations.InternalAggregations@65cf321d
我真正想要的是整个 string/json 响应,如果您在 elasticsearch 上卷曲以获取聚合,通常会返回该响应。如何从聚合查询中获取原始响应?另外,有没有办法迭代并打印出那些 "wrapped up" 对象中的内容?
@Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
// given
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("articles").withTypes("article")
.addAggregation(terms("subjects").field("subject"))
.build();
// when
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
// then
System.out.println(aggregations); // gives me some cryptic InternalAggregations object, how do I get to the raw JSON normally returned by elasticsearch?
System.out.println(aggregations.asMap().get("subjects")); // gives me some StringTerms object I have no idea how to iterate over to get results
}
您无法通过这种方式获得原始 JSON 响应,因为 Spring Data Elasticsearch 会负责为您解析它,这就是重点。
如果你需要解析那些桶,你可以很容易地这样做:
...
StringTerms subjects = aggregations.asMap().get("subjects");
for (Terms.Bucket bucket : subjects.getBuckets()) {
String key = bucket.getKey();
long docCount = bucket.getDocCount();
// do something with the key and the doc count
}
如果你真的想看到 JSON 被返回,你可以做的是使用序列化将解析的 Aggregations 对象重新写入 JSON ,但这实际上没有帮助:
InternalAggregations aggregations = ...;
XContentBuilder jsonBuilder = JsonXContent.contentBuilder();
aggregations.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
String rawJson = jsonBuilder.string();
将 EsRequest 的大小设置为零
获取Esresponse.toString()
将字符串转换为 Json
从Json获取聚合字段。