ElasticSearch 与java的结合使用(二)

不同类型商品数量

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;

平均数,最大数统计

java 复制代码
{
  "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;

统计不同类别的数量

java 复制代码
{
  "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代替

java 复制代码
{
  "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;

前缀搜索

java 复制代码
{
  "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());
相关推荐
DavidSoCool3 小时前
es 3期 第25节-运用Rollup减少数据存储
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客4 小时前
使用 Elasticsearch 导航检索增强生成图表
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Elastic 中国社区官方博客6 小时前
设计新的 Kibana 仪表板布局以支持可折叠部分等
大数据·数据库·elasticsearch·搜索引擎·信息可视化·全文检索·kibana
Dusk_橙子16 小时前
在elasticsearch中,document数据的写入流程如何?
大数据·elasticsearch·搜索引擎
喝醉酒的小白18 小时前
Elasticsearch 中,分片(Shards)数量上限?副本的数量?
大数据·elasticsearch·jenkins
熟透的蜗牛20 小时前
Elasticsearch 8.17.1 JAVA工具类
elasticsearch
九圣残炎1 天前
【ElasticSearch】 Java API Client 7.17文档
java·elasticsearch·搜索引擎
risc1234561 天前
【Elasticsearch】HNSW
elasticsearch
我的棉裤丢了1 天前
windows安装ES
大数据·elasticsearch·搜索引擎
乙卯年QAQ1 天前
【Elasticsearch】RestClient操作文档
java·大数据·elasticsearch·jenkins