ElasticSearch 与java的结合使用(二)

不同类型商品数量

{
  "size": 0, 
  "aggs": {
    "type_group": {
      "terms": {
        "field": "type.keyword"
      }
    }
  }
}

  SearchRequest request = new SearchRequest();
        request.indices("product");
        // 按照类型分组统计数量
        AggregationBuilder type = AggregationBuilders
                .terms("type_group")
                .field("type.keyword");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.aggregation(type);
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Object> list = Lists.newArrayList();
        // 数据取出
        ParsedStringTerms typeGroup = response.getAggregations().get("type_group");
        for(Terms.Bucket bucket :  typeGroup.getBuckets()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key",bucket.getKey());
            jsonObject.put("count",bucket.getDocCount());
            list.add(jsonObject.toJSONString());
        }
        return list;

平均数,最大数统计

{
  "size": 0,
  "aggs": {
    "pice_avg": {
      "avg": {
        "field": "pice"
      }
    },
    "max_pice": {
      "max": {
        "field": "pice"
      }
    }
  }
}
  SearchRequest request = new SearchRequest();
        request.indices("product");
        // 统计平均值
        AggregationBuilder piceAvg = AggregationBuilders
                .avg("pice_avg")
                .field("pice");
         // 统计最大值
        AggregationBuilder piceMax = AggregationBuilders
                .max("pice_max")
                .field("pice");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.aggregation(piceAvg).aggregation(piceMax);
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Object> list = Lists.newArrayList();
        // 数据取出
        ParsedAvg parsedAvg = response.getAggregations().get("pice_avg");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pice_avg",parsedAvg.getValue());
        ParsedMax parsedMax = response.getAggregations().get("pice_max");
        jsonObject.put("pice_max",parsedMax.getValue());
        list.add(jsonObject.toJSONString());
        return list;

统计不同类别的数量

{
  "size": 0,
  "aggs": {
    "type_group": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "avg_pice": {
          "avg": {
            "field": "pice"
          }
        }
      }
    }
  }
}

 SearchRequest request = new SearchRequest();
        request.indices("product");
        // 按照类型分组统计数量
        AggregationBuilder typeGroup = AggregationBuilders
                .terms("type_group")
                .field("type.keyword");
        // 计算平均值
        AggregationBuilder piceAvg = AggregationBuilders
                .avg("pice_avg")
                .field("pice");
        typeGroup.subAggregation(piceAvg);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.aggregation(typeGroup);
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Object> list = Lists.newArrayList();
        // 数据取出
        ParsedStringTerms parsedType = response.getAggregations().get("type_group");
        for(Terms.Bucket bucket :  parsedType.getBuckets()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key",bucket.getKey());
            jsonObject.put("count",bucket.getDocCount());
            ParsedAvg parsedAvg = bucket.getAggregations().get("pice_avg");
            jsonObject.put("pice_avg",parsedAvg.getValue());
            list.add(jsonObject.toJSONString());
        }
        return list;

根据日期按天统计,没有数据则0代替

{
  "size": 0, 
  "aggs": {
    "create_time_histogram": {
      "date_histogram": {
        "field": "createtime",
        "calendar_interval": "month",
        "min_doc_count": 0,
        "format": "yyyy-MM", 
        "extended_bounds": {
          "min": "2020-01",
          "max": "2020-12"
        }
      }
    }
  }
}

 SearchRequest request = new SearchRequest();
        request.indices("product");
        // 设置世界范围
        LongBounds longBounds =new LongBounds("2020-01","2020-12");
        // 按照时间统计数据,间隔一天
        AggregationBuilder createtime = AggregationBuilders
                .dateHistogram("create_time_histogram")
                .field("createtime")
                .fixedInterval(DateHistogramInterval.DAY)
                .format("yyyy-MM")
                .minDocCount(0)
                .extendedBounds(longBounds);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.aggregation(createtime);
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Object> list = Lists.newArrayList();
        // 数据取出
        Histogram parsedType = response.getAggregations().get("create_time_histogram");
        for(Histogram.Bucket bucket :  parsedType.getBuckets()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key",bucket.getKeyAsString());
            jsonObject.put("count",bucket.getDocCount());
            list.add(jsonObject.toJSONString());
        }
        return list;

前缀搜索

{
  "query": {
    "prefix": {
      "name": {
        "value": "小米"
      }
    }
  }
}

 SearchRequest request = new SearchRequest();
        request.indices("product");
        // 前缀查询
        QueryBuilder queryBuilder = QueryBuilders.prefixQuery("name","小米");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(queryBuilder);
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Object> list = Lists.newArrayList();
        // 数据取出
        SearchHit[] hits = response.getHits().getHits();
        return Arrays.stream(hits).map(h -> h.getSourceAsString()).collect(Collectors.toList());

你可能感兴趣的:(ElasticSearch,elasticsearch)