子聚集结果未正确显示

时间:2016-11-22 12:56:55

标签: java spring elasticsearch spring-data-elasticsearch

我正在使用Java Springdata elasticsearch,我想使用子聚合并为以下查询建模。

{
  "from" : 0,
  "size" : 10,
  "sort" : [ {
    "_score" : {
      "order" : "desc"
    }
  } ],
  "aggregations" : {
    "parentAgg" : {
      "terms" : {
        "field" : "parentField",
        "size" : 0
      },
      "aggregations" : {
        "childAgg" : {
          "terms" : {
            "field" : "childField"
          }
        }
      }
    }
  }
} 

目前我使用了subaggregation(即Aggregation.subAggregation(subAggName))但是我得到的输出是 -

"aggregations": [
    {
      "field": "parentAgg",
      "values": [
        {
          "term": "val1",
          "docCount": 2
        },
        {
          "term": "val2",
          "docCount": 2
        },
        {
          "term": "val3",
          "docCount": 1
        }
      ]
    }
  ]

相关Java代码 -

for (Object aggregationField : request.getAggregationFields()) {
            TermsBuilder termBuilder = AggregationBuilders.terms(aggregationField.toString())
                    .field(aggregationField.toString()).size(0);
            if(aggregationField.toString().equals("parentField"))
            {
                TermsBuilder childBuilder = AggregationBuilders.terms("childAgg").field("childField").size(0);
                termBuilder.subAggregation(childBuilder);
            }               
            nativeSearchQueryBuilder.addAggregation(termBuilder);
        }

你能告诉我我错过了什么吗?

0 个答案:

没有答案