使用elasticsearch查询客户端java API获取字符串字段的不同值

时间:2018-01-04 14:12:13

标签: elasticsearch-5

我正在使用elasticsearch 5.4

我需要使用elasticsearch query

获取不同的字段值

我正在尝试此查询,但不是在考虑

GET /index/index_type/_search
{
  "size": 0,
  "aggs": {
    "distinct": {
      "terms": {
        "field": "status"
      }
    }
  }

谢谢

2 个答案:

答案 0 :(得分:0)

客户端java api elasticsearch 5.4的

解决方案

import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
TransportClient client;
SearchResponse response = client.prepareSearch(indexName).setTypes(typeName)
                .addAggregation(AggregationBuilders.terms("distinct_line_status").field("line_status.keyword"))
                .setSize(0).get();
        //AggregationProfileShardResult searchHits = response.getAggregations().getAsMap();
        System.out.println(response);
        Terms genders = response.getAggregations().get("distinct_line_status");

        // For each entry
        for (Terms.Bucket entry : genders.getBuckets()) {
            System.out.println(entry.getKey());      // Term
            System.out.println(entry.getDocCount()); // Doc count
        }

答案 1 :(得分:0)

您可以使用基数来实现此目的。

GET /index/index_type/_search
{
  "size": 0,
  "aggs": {
    "distinct": {
      "cardinality": {
        "field": "status"
      }
    }
  }