elasticsearch 多字段聚合或者对字段子串聚合
以下是字段子串聚合,截取 'your_field' 前八位进行聚合的
Script script = new Script("doc['your_field'].getValue().substring(0,8)"); CardinalityAggregationBuilder pointCardinality = AggregationBuilders.cardinality("pointCardinality").script(script).precisionThreshold(400000); SearchResponse totalPointResponse = client.prepareSearch("index_name").setTypes("type_name") .setQuery(boolQueryBuilder) .addAggregation(pointCardinality) .execute() .actionGet(); Cardinality cardinalityTotal = totalPointResponse.getAggregations().get("pointCardinality");
多字段聚合,就是需要修改Script内容(plainless),例如根据条件选取字段等:
eg:
if(doc['camera_dev_name'].size() != 0){doc['camera_dev_name'].getValue().substring(0,8)}else{doc['ap_dev_name.keyword'].getValue().substring(0,8)}
只需改变Script内容。