我正在尝试将从Elastic搜索返回的数据解析为java类。弹性搜索以下面给出的形式返回数据:
"aggregations": {
"aggregationName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 439,
"doc_count": 7,
"nestedAggregationName": {
"value": 5
}
}
]
}
}
这里" aggregationName"和" nestedAggregationName"名字是动态的,意味着它们可以是任何东西,所以我如何管理这是我的班级?我想直接解析返回到DTO的数据。
编辑:我能够解析" aggregationName"通过下面的课程以通用方式:
public Class Aggregation{
private Map<String, AggregationModel> aggregation;
//with getters and setters.
}
无论&#34; aggregationName&#34;是,它被轻松解析,AggregationModel包含对应于&#34; aggregationName&#34;的字段。如何为&#34; nestedAggregationName&#34; 解决此问题?
请帮忙。
答案 0 :(得分:0)
我在一段时间后偶然发现了同样的问题,不幸的是java客户端不支持聚合响应(如果我错了请纠正我)。 以下是我在多轮追踪错误方法后想出的,如果有帮助,请告诉我。
private Map<String, List<Map.Entry<String, Long>>> buildAggregationsFromResult(SearchResults resultSet, Set aggregationsName) {
Map<String, List<Map.Entry<String, Long>>> aggregations = new LinkedHashMap<String, List<Map.Entry<String, Long>>>();
if (null != resultSet.getAggregations()) {
Map<String, Aggregation> aggregationMap = resultSet.getAggregations().asMap();
for (Map.Entry<String, Aggregation> aggregationEntry : aggregationMap.entrySet()) {
fetchBucketTermsFromAggregations(aggregationEntry,aggregations, aggregationsName);
}
}
return aggregations;
}
private List<Map.Entry<String, Long>> fetchBucketTermsFromAggregations(Map.Entry<String, Aggregation> aggregationEntry, Map<String, List<Map.Entry<String, Long>>> aggregations, Set<String> aggregationsName) {
Terms terms = null;
List<Map.Entry<String, Long>> pairs = new ArrayList<>();
if (aggregationEntry.getValue() instanceof InternalNested) {
for (Map.Entry<String, Aggregation> aggregationEntryNested : ((InternalNested) aggregationEntry.getValue()).getAggregations().asMap().entrySet()) {
pairs = fetchBucketTermsFromAggregations(aggregationEntryNested, aggregations, aggregationsName);
if (CollectionUtils.isNotEmpty(pairs) && aggregationsName.contains(aggregationEntry.getKey()))
aggregations.put(aggregationEntry.getKey(), pairs);
}
} else if (aggregationEntry.getValue() instanceof InternalFilters) {
for (InternalFilters.Bucket buckets : ((InternalFilters) aggregationEntry.getValue()).getBuckets()) {
for (Map.Entry<String, Aggregation> nestedAggregations : buckets.getAggregations().asMap().entrySet()) {
pairs = fetchBucketTermsFromAggregations(nestedAggregations, aggregations, aggregationsName);
if (CollectionUtils.isNotEmpty(pairs) && aggregationsName.contains(aggregationEntry.getKey()) )
aggregations.put(aggregationEntry.getKey(), pairs);
}
}
} else {
terms = (Terms) aggregationEntry.getValue();
if (null != terms ) {
Collection<Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
if (CollectionUtils.isNotEmpty(bucket.getAggregations().asList()))
pairs.add(new AbstractMap.SimpleEntry<String, Long>(bucket.getKeyAsString(), ((InternalReverseNested)bucket.getAggregations().asList().get(0)).getDocCount()));
else
pairs.add(new AbstractMap.SimpleEntry<String, Long>(bucket.getKeyAsString(), bucket.getDocCount()));
}
}
if (CollectionUtils.isNotEmpty(pairs) && aggregationsName.contains(aggregationEntry.getKey()))
aggregations.put(aggregationEntry.getKey(), pairs);
}
return pairs;
}
我知道这看起来有点笨拙,但可能适合你的情况。别的我知道了。