因此,我已经能够使用elasticsearch 高级restclient 在Java 中根据需要复制elasticsearch 查询。问题是我无法检索我想要的值。在给出代码之前,我想解决总体目标,以防有更简单的解决方案(看起来这不应该那么困难。)
总体目标:获取 'recommender' 字段中每个唯一值的 'visited'==true 的文档数。
我目前的状态:我已经能够在 kibana/elasticsearch 中编写具有所需输出的查询,但是当我在 Java 中复制此查询时,我无法访问我需要的数据。(使用 searchRequest.source().toString 验证())。
这是查询:
{
"aggs":{
"recommenderIDs": {
"terms": {
"field": "recommender"
},
"aggs": {
"visit_stats": {
"filters": {
"filters": {
"visited": {
"match":{
"visited": true
}
}
}
}
}
}
}
}
}
这就是我的java代码中的内容:
// ...
SearchRequest searchRequest = new SearchRequest(INDEX_REC_RECOMMENDATIONS);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
String aggregationName = "recommenderId";
String filterName = "wasVisited";
TermsAggregationBuilder aggQuery = AggregationBuilders
.terms(aggregationName)
.field(RecommendationRepoFieldNames.RECOMMENDER);
AggregationBuilder aggFilters = AggregationBuilders.filters(
filterName,
new FiltersAggregator.KeyedFilter(
RecommendationRepoFieldNames.RECOMMENDER,
QueryBuilders.termQuery(RecommendationRepoFieldNames.VISITED, true))
);
aggQuery.subAggregation(aggFilters);
searchSourceBuilder.aggregation(aggQuery);
searchRequest.source(searchSourceBuilder);
// System.out.println(searchRequest.source().toString());
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
Terms byRecommenderId = aggregations.get(aggregationName);
Filters filterResponses = searchResponse.getAggregations().get(aggregationName);
// for (Filters.Bucket entry : filterResponses.getBuckets()) {
// String key = entry.getKeyAsString();
// }
for (Terms.Bucket bucket : byRecommenderId.getBuckets()) {
String bucketKey = bucket.getKeyAsString();
long totalDocs = bucket.getDocCount();
Aggregation visitedDocs = bucket.getAggregations().get(filterName);
//long visitedDocsCount = visitedDocs.getValue();
System.out.println();
}
} catch (IOException e) { //...
我整天都在摆弄这个,没有任何进展。这尤其令人沮丧,因为当我在 IDE 中调试时,我可以看到每个推荐者存储桶的文档计数,但我不知道如何访问它。我意识到大约有 180 个类扩展了聚合,我尝试了一些,但每次都失败了。
此外,如果您知道弹性搜索 java 高级休息客户端的任何体面资源,请告诉我。谢谢!
---------编辑 5/4/21 -------------
elasticsearch 的示例输出:
// searchResponse (documents returned have been truncated to show only part we are interested in)
"aggregations": {
"sterms#recommenderId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AdjacentActivityRecommender",
"doc_count": 3,
"filters#wasVisited": {
"buckets": {
"recommender": {
"doc_count": 2
}
}
}
},
{
"key": "DefaultProfileDBRecommender",
"doc_count": 2,
"filters#wasVisited": {
"buckets": {
"recommender": {
"doc_count": 2
}
}
}
},
{
"key": "PSTR_SC_DI",
"doc_count": 2,
"filters#wasVisited": {
"buckets": {
"recommender": {
"doc_count": 1
}
}
}
},
{
"key": "SignificantCategories",
"doc_count": 2,
"filters#wasVisited": {
"buckets": {
"recommender": {
"doc_count": 2
}
}
}
}
]
}
}
然后将 searchResponse.getAggregations() 保存到聚合中。最终,我们能够遍历每个推荐者 ID 的存储桶,但我永远无法进入每个存储桶内部的聚合,这是我需要做的。