ElassticSearch基础语法与释义

1.查询索引映射

GET /user/_mapping  // user是索引名称,_mapping是映射参数

2.查看集群节点信息

GET _cat/nodes?v

3.查看集群健康状态

GET _cat/health?v

4.查看全部索引

GET _cat/indices?v

5.删除索引

DELETE /user

5.添加文档记录

// 这里没有添加映射,直接采用es默认映射。也可以单独创建映射,下面例子会有
// PUT添加索引时,必须指定唯一的资源id,然后更具id进行创建或更新
PUT /user/doc/1
{
  "id":"1",
  "name":"战狼",
  "description":"2008年",
  "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  "price": 22.55,
  "studymodel":"110"
}

6.match查询

// match模糊查询,可以分词。类似于mysql中的like,当字段属于text,text分词,
// 可以和分词中的每一个进行匹配,例如查询曹操,如果单独一个曹,就不能匹配。
// keyword不能分词,需要match分词中有一个能与其匹配。
GET /user/_search
{
  "query": {
    "match": {
      "name": "曹操"
    }
  }
}

7.term精确查询

// term精确查询,不分词。text字段会分词,不能聚合查询;keyword字段不会分词,能聚合
// 查询时候需要指定具体值:类似MySQL中where条件name="xxx"
// 若为text,则需要text中分完词关键字与term关键字匹配
// 若为keyword,keyword不能分词,则需要两者完全匹配才能获取结果值
GET /user/_search
{
  "query": {
    "term": {
      "name": "曹操"
    }
  }
}

8. 中文分词器ik插件分词

// ik中文分词器的analyzer设置为ik_max_word,能够细粒度的分词
GET /_analyze
{
  "analyzer": "ik_max_word",
  "text":"中华人民共和国"
}
// ik中文分词器的analyzer设置为ik_smart,能够细粒度的分词
GET /_analyze
{
  "analyzer": "ik_smart",
  "text":"中华人民共和国"
}

9.es内置的标准分词器standard

// 内置分词器
GET /_analyze
{
  "analyzer": "standard",
  "text": "中华人民共和国"
}

10.terms查询

#terms查询,类似数组。获取索引词条中的某几个记录进行精确匹配
# 吕布可以匹配到,但是周,司开头的不能匹配
GET /user/_search
{
  "query": {
    "terms": {
      "name": [
        "吕布",
        "周",
        "司",
        ""
      ]
    }
  }
}

11.ES的_mget参数

#根据_mget参数批量获取文档数据
// 1.GET请求中直接写_mget参数,在docs数组中指定索引和类型(类型在7.x中被废弃了)
GET _mget
{
  "docs":[
    {
      "_index":"user",
      "_id":"DraYJ4sB29ejqWC0fF3V"
    },
    {
      "_index":"user",
      "_id":"EbaYJ4sB29ejqWC0fF3V"
    }
  ]
}

#在GET请求之后,添加索引名称/_mget参数,根据id来批量获取文档记录
// 2.GET请求中直接写索引和_mget参数,在docs数组中指定字段进行文档记录获取
GET /user/_mget
{
  "docs":[
    {
      "_id":"E7aYJ4sB29ejqWC0fF3V"
    },
    {
      "_id":"F7aYJ4sB29ejqWC0fF3V"
    }
  ]
}

12. 聚合查询aggs

#聚合查询,文本类型的不能作为聚合查询。
// 根据价格字段price进行分组,获取到每种价格的数量有多少。price是浮点型。name是文
// 本类型的不能聚合
GET /user/_search
{
  "aggs": {
    "name_group": {
      "terms": {
        "field": "price",
        "size": 10
      }
    }
  }
}

#针对字段是keyword类型,如果是非整数和浮点型的话,只能进行分组查询
GET /user/_search
{
  "aggs": {
    "name_group": {
      "terms": {
        "field": "studymodel",
        "size": 10
      }
    }
  }


13.高亮查询

// 添加一个索引为1的文档
PUT /news_website/_doc/1
{
  "title":"这是我写的第一篇文章",
  "content":"大家好,这是我写的第一篇文章,特别喜欢这个门户网站!!!"
}
// 对【文章】做高亮查询,采用plain highlight
GET /news_website/_search
{
  "query": {
    "match": {
      "title": "文章"
    }
  },
  "highlight": {
    "pre_tags": ["<span color='red'>"],
    "post_tags": ["</span>"], 
    "fields": {
      "title": {
        "type":"plain"
      }
    }
  }
}

// 使用默认的高亮查询对关键词进行高亮显示:pre_tags:前拽标签 post_tags:后拽标签
GET /news_website/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "文章"
          }
        },
        {
          "match": {
            "content": "文章"
          }
        }
      ]
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:#F56C6C'>"],
    "post_tags": ["</span>"], 
    "fields": {
      "title": {},
      "content": {}
    }
  }
}

14.案例演示

#========================汽车Cars索引========================================================
#创建一个cars索引和映射,并且指定类型和分词器
PUT /cars
{
  "mappings": {
    "properties": {
      "price": {
        "type": "long"
      },
      "color": {
        "type": "keyword"
      },
      "brand": {
        "type": "keyword"
      },
      "model": {
        "type": "keyword"
      },
      "sold_date": {
        "type": "date",
        "format": "yyyy-MM-dd"
      },
      "remark": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}


// 批量插入数据,是用_bulk参数
POST /cars/_bulk
{"index":{}}
{"price" : 258000, "color" : "金色", "brand":"大众", "model" : "大众迈腾", "sold_date" : "2021-10-28","remark" : "大众中档车" }
{"index":{}}
{"price":123000,"color":"金色","brand":"大众","model":"大众速腾","sold_date" : "2021-11-05","remark":"大众神车"}
{"index":{}}
{"price":239800,"color":"白色","brand":"标志","model":"标志508","sold_date":"2021-05-18","remark":"标志品牌全球上市车型"}
{"index":{}}
{"price":148800,"color":"白色","brand":"标志","model":"标志408","sold_date":"2021-07-02","remark":"比较大的紧凑型车"}
{"index":{}}
{"price":1998000,"color":"黑色","brand":"大众","model":"大众辉腾","sold_date":"2021-08-19","remark":"大众最让人肝疼的车"}
{"index":{}}
{"price":218000,"color":"红色","brand":"奥迪","model":"奥迪A4","sold_date":"2021-11-05","remark":"小资车型"}
{"index":{}}
{"price":489000,"color":"黑色","brand":"奥迪","model":"奥迪A6","sold_date":"2022-01-01","remark":"政府专用?"}
{"index":{}}
{"price":1899000,"color":"黑色","brand":"奥迪","model":"奥迪A 8","sold_date":"2022-02-12","remark":"很贵的大A6。。。"}

// 获取索引数据
GET /cars/_search
{
  "query": {
    "match_all": {}
  } 
}

// 根据颜色分组,统计每种颜色车的数量,terms分组查询,order属性排序,根据颜色分组出来的车辆,按数量进行降序排序
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "group_color": {
      "terms": {
        "field": "color",
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}


// 统计不同颜色车辆的平均价格
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color",
        "order": {
          "avg_price": "asc"
        }
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

// size设置为0,表示不显示索引中的数据
GET /cars/_search
{
  "size": 0, 
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "brand_group": {
          "terms": {
            "field": "brand",
            "order": {
              "avg_price": "desc"
            }
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}


// 统计不同color下的最大,最小,总价,最普遍的聚合查询
GET /cars/_search
{
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "max_price": {
          "max": {
            "field": "price"
          }
        },
        "min_price":{
          "min": {
            "field": "price"
          }
        },
        "sum_price":{
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}


//top_hits属性:统计不同品牌的汽车中价格排名最高的车型
GET /cars/_search
{
  "size": 0,
  "aggs": {
    "group_brand": {
      "terms": {
        "field": "brand"
      },
      "aggs": {
        "top_car": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "price": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": ["model","price"]
            }
          }
        }
      }
    }
  }
}

// histogram区间统计:以100w为一个范围,统计不同范围内车辆的销售量和平均价格,doc_count为数量
GET /cars/_search
{
  "aggs": {
    "histogram_by_price": {
      "histogram": {
        "field": "price",
        "interval": 1000000
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

// date_histogram可以对date类型的field指行区间聚合分组,每月销量,每年销量
// 以月为单位,统计不同月份汽车的销售数量及销售总金额,7.x版本之后的
GET /cars/_search
{
  "aggs": {
    "date_histogram_by_date": {
      "date_histogram": {
        "field": "sold_date",
        "calendar_interval": "month",
        "format": "yyyy-MM-dd",
        "min_doc_count": 1,
        "extended_bounds": {
          "min": "2021-01-01",
          "max": "2022-12-31"
        }
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

// _global bucket 统计某品牌的车辆平均价格和所有车辆的平均价格
GET /cars/_search
{
  "size": 0,
  "query": {
    "match": {
      "brand": "大众"
    }
  },
  "aggs": {
    "volkswagen_of_avg_price": {
      "avg": {
        "field": "price"
      }
    },
    "all_avg_price":{
      "global": {}, 
      "aggs": {
        "all_avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

// aggs+order,统计每个品牌的汽车销量和销售总额
GET /cars/_search
{
  "aggs": {
    "brand_group_by": {
      "terms": {
        "field": "brand",
        "order": {
          "sum_price": "desc"
        }
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

// 统计每个品牌中每种颜色的车辆的销售总额
GET /cars/_search
{
  "aggs": {
    "brand_group_by": {
      "terms": {
        "field": "brand"
      },
      "aggs": {
        "color_group_by": {
          "terms": {
            "field": "color",
            "order": {
              "sum_price": "desc"
            }
          },
          "aggs": {
            "sum_price": {
              "sum": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

// search+aggs :聚合类似sql中的group by子句,search类似sql中的where子句
// 统计某品牌车辆每个季度的销量和销售额
GET /cars/_search
{
  "query": {
    "match": {
      "brand": "大众"
    }
  },
  "aggs": {
    "histogram_by_date": {
      "date_histogram": {
        "field": "sold_date",
        "calendar_interval": "quarter",
        "min_doc_count": 1
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

// filter+aggs:filter也可以和aggs组合使用,实现相对复杂的聚合分析
// 统计10万~50万之间的车辆的平均价格。
GET /cars/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 100000,
            "lte": 500000
          }
        }
      }
    }
  },
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

// 聚合也可以放在aggs内部,代表这个过滤器,代表这个过滤器只对query搜索得到的结果执行filter过滤
// 统计某品牌汽车最近一年的销售总额
GET /cars/_search
{
  "query": {
    "match": {
      "brand": "标志"
    }
  },
  "aggs": {
    "recent_last_year": {
      "filter": {
        "range": {
          "sold_date": {
            "gte": "now-12M"
          }
        }
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

15.Springboot整合ElasticSearch7.6.2,模拟案例

// 1.引入对应的依赖
		<!--引入我们对应的RestHighLevelClient客户端-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client-sniffer</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>
        
        <!--lombok工具用于生成对应的get,set方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <!--引入对应的springbootTest测试类,用于加载我们的@AutoWired注解-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    


//2. 以下进行数据测试

@SpringBootTest
public class CarEsAggrGrammerApplicationTests {

    @Autowired
    private RestHighLevelClient client;

    /**
     * 不创建索引和影射的前提下,直接向ES中插入文档记录信息
     */
    @Test
    public  void  createSingleDocumentTest() throws IOException {
        Cars cars = new Cars("",25800.0,"金色","大众","大众迈腾","2023-10-28","大众中档车");
        IndexRequest indexRequest=new IndexRequest("cars");
        // 该id可设置,可不设置
        indexRequest.id("1");
        indexRequest.timeout("1s");
        indexRequest.source(JSON.toJSONString(cars), XContentType.JSON);
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    @Test
    public  void  createBulkDocumentRequestTest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("20s");
        List<Cars> cars = new ArrayList<>();
        cars.add(new Cars("1",25800.0,"金色","大众","大众迈腾","2023-10-28","大众中档车"));
        cars.add(new Cars("2",123000.0,"金色","大众","大众速腾","2023-11-05","大众神车"));
        cars.add(new Cars("3",239800.0,"白色","标志","标志508","2023-05-18","标志品牌全球上市车型"));
        cars.add(new Cars("4",148800.0,"白色","标志","标志408","2023-07-02","比较大的紧凑型车"));
        cars.add(new Cars("5",1998000.0,"黑色","大众","大众辉腾","2023-08-19","大众最让人肝疼的车"));
        cars.add(new Cars("6",218000.0,"红色","奥迪","奥迪A4","2023-11-05","小资车型"));
        cars.add(new Cars("7",48900.0,"黑色","奥迪","奥迪A6","2022-01-01","政府专用"));
        cars.add(new Cars("8",1899000.0,"黑色","奥迪","奥迪A8","2022-02-12","很贵的大A6。。。"));
        cars.add(new Cars("9",25800.0,"白色","奔驰","奔驰E300L","2023-09-30","2018款奔驰时尚车型"));
        cars.add(new Cars("10",25800.0,"黑色","奔驰","奔驰E320L","2023-10-08","2015款奔驰E级运动型"));
        for (Cars car:cars){
            bulkRequest.add(new IndexRequest("cars").source(JSON.toJSONString(car),XContentType.JSON));
        }
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        assertFalse(bulkResponse.hasFailures());
    }

    /**
     * 删除文档记录
     * @throws IOException
     */
    @Test
    public void deleteDocumentRequestTest() throws IOException {
        DeleteRequest indexRequest=new DeleteRequest("cars","");
        indexRequest.timeout("1s");
        DeleteResponse response=client.delete(indexRequest,RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

    /**
     * 修改文档信息
     * @throws IOException
     */
    @Test
    public void updateDocumentRequestTest() throws IOException {
        Cars cars = new Cars("",25800.0,"金色","大众","大众迈腾","2023-10-28","大众中档车");
        UpdateRequest updateRequest=new UpdateRequest("cars","指定任意id");
        updateRequest.timeout("1s");
        updateRequest.doc(JSON.toJSONString(cars),XContentType.JSON);
        UpdateResponse response=client.update(updateRequest,RequestOptions.DEFAULT);
        System.out.println(response.status());
    }
    /**
     * 创建索引和映射
     */
    @Test
    public  void  createIndexAndMappings() throws IOException {

        //操作索引的对象
        IndicesClient indicesClient=client.indices();
        //创建索引请求
        CreateIndexRequest createIndexRequest=new CreateIndexRequest("cars");
        //设置索引的分片数和副本数
        createIndexRequest.settings(Settings.builder().put("number_of_shards","3").put("number_of_replicas","2"));
        // 创建映射
        createIndexRequest.mapping("doc", "{\n" +
                "                \"properties\": {\n" +
                "                    \"color\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"brand\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                   \"model\":{\n" +
                "                       \"type\":\"keyword\",\n" +
                "                       \"index\":false\n" +
                "                   },\n" +
                "                    \"price\": {\n" +
                "                        \"type\": \"float\"\n" +
                "                    },\n" +
                "                    \"remark\": {\n" +
                "                        \"type\": \"text\",\n" +
                "                        \"analyzer\": \"ik_max_word\"\n" +
                "                    },\n" +
                "                    \"sold_date\": {\n" +
                "                        \"type\": \"date\",\n" +
                "                        \"format\": \"yyyy-MM-dd\"\n" +
                "                    }\n" +
                "                }\n" +
                "            }", XContentType.JSON);

        CreateIndexResponse response = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
        boolean b = response.isAcknowledged();
        System.out.println(b);
    }

    // 获取全部文档记录
    @Test
    public  void  getMatchAllQueryDocument() throws IOException {
        // 搜索请求对象
        SearchRequest searchRequest = new SearchRequest("cars");
        // 指定类型:会报错Elasticsearch-7.6.2-ef48eb35cf30adf4db14086e8aabd07ef6fb113f "[types removal] Specifying types in search requests is deprecated."]
        // searchRequest.types("doc");
        // 搜索源构建对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
        searchSourceBuilder.fetchSource(new String[]{"id","brand","color","model","price","remark","sold_date"},new String[]{});
        // 向搜索请求对象中设置搜索源
        searchRequest.source(searchSourceBuilder);
        // 执行搜索,向ES发起http请求
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        // 搜索结果
        SearchHits hits = searchResponse.getHits();
        // 匹配到的总记录数
        //TotalHits hits1 = hits.getTotalHits();
        System.out.println("记录数"+hits.getHits().length);
        // 得到匹配度高的文档
        SearchHit[] searchHits = hits.getHits();

        for(SearchHit hit:searchHits) {
            // 文档的主键
            String id = hit.getId();
            // 源文档内容
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String brand = (String) sourceAsMap.get("brand");
            // 由于前边设置了源文档字段过虑,这时description是取不到的
            String color = (String) sourceAsMap.get("color");
            // 学习模式
            String model = (String) sourceAsMap.get("model");
            // 价格
            Double price = (Double) sourceAsMap.get("price");
            // remark
            String remark = (String) sourceAsMap.get("remark");
            // sold_date
            String sold_date = (String) sourceAsMap.get("sold_date");
            Cars cars = new Cars(id, price, color, brand, model, sold_date,remark);
            System.out.println(cars);
        }
    }

    // terms进行分组查询,获取分组后的数量,order进行降序排序
    // 根据颜色分组,获取每种颜色车的数量,
    @Test
    public void  getAggsColorGroupCarNum() throws IOException {

        // 搜索请求对象
        SearchRequest searchRequest = new SearchRequest("cars");
        // 搜索源构建对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("color").field("color"));
        // 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
        searchSourceBuilder.fetchSource(new String[]{"id","brand","color","model","price","remark","sold_date"},new String[]{});
        // 向搜索请求对象中设置搜索源
        searchRequest.source(searchSourceBuilder);
        // 执行搜索,向ES发起http请求
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);

        Terms terms = searchResponse.getAggregations().get("color");
        for (Terms.Bucket bucket : terms.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            System.out.println("color:"+key+",Car Count:"+count);
        }

    }

    // 统计不同颜色车辆的平均价格
    @Test
    public  void  getAggsColorGroupAvgPrice() throws IOException {
        // 搜索请求对象,用来发起请求
        SearchRequest  searchRequest = new SearchRequest("cars");
        // 搜索源对象,用来构建ES查询语句
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("color").field("color")
                .subAggregation(AggregationBuilders.avg("avg_price").field("price")));

        // 往搜索请求对象中设置搜索源对象
        searchRequest.source(searchSourceBuilder);
        // restHighLevelClient设置搜索源
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        // 获取聚合后的结果
        Terms terms = searchResponse.getAggregations().get("color");
        for (Terms.Bucket bucket:terms.getBuckets()){
            // 分组key
            String key = bucket.getKeyAsString();
            // 分组key的数量
            long count = bucket.getDocCount();
            Avg avg = bucket.getAggregations().get("avg_price");
            // 分组key之后的对其中属性求平均值
            double avg_price = avg.getValue();
            System.out.println("group key:"+key+",count:"+count+",平均值:"+avg_price);
        }
    }

    // 统计不同color下的最大,最小,总价
    @Test
    public  void getAggsMaxMinSumByColorGroup() throws IOException {

        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("group_color").field("color")
                .subAggregation(AggregationBuilders.min("min_price").field("price"))
                .subAggregation(AggregationBuilders.max("max_price").field("price"))
                .subAggregation(AggregationBuilders.sum("sum_price").field("price"))
        );

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get("group_color");
        for (Terms.Bucket bucket: terms.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Sum sum = bucket.getAggregations().get("sum_price");
            double sumValue = sum.getValue();
            Max max = bucket.getAggregations().get("max_price");
            double maxValue = max.getValue();
            Min min = bucket.getAggregations().get("min_price");
            double minValue = min.getValue();
            System.out.println("group key:"+key+",count:"+count+",sum:"+sumValue+",\t\t\t\tmax:"+maxValue+",\t\t\t\tmin:"+minValue);
        }
    }

    // 统计不同颜色,不同品牌下的汽车数量和平均价格
    @Test
    public  void  getAggsAvgPriceByGroupColorAndBrandGroup() throws IOException {
        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("group_color").field("color")
                .subAggregation(AggregationBuilders.terms("group_brand").field("brand")
                        .subAggregation(AggregationBuilders.avg("avg_price").field("price")).order(BucketOrder.key(false))
                        )
        );
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get("group_color");

        for (Terms.Bucket bucket: terms.getBuckets()){

            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Terms terms1 = bucket.getAggregations().get("group_brand");

            for (Terms.Bucket bucket1:terms1.getBuckets()){
                String key1 = bucket1.getKeyAsString();
                long count1 = bucket1.getDocCount();
                Avg avg= bucket1.getAggregations().get("avg_price");
                double avgValue = avg.getValue();
                System.out.println("group key:"+key+",count:"+count+",key1:"+key1+",count1:"+count1+",avgValue:"+avgValue);
            }
        }
    }

    // 统计不同品牌的汽车中价格排名最高的车型
    @Test
    public  void  getAggsCarOfPriceRankHigherByGroupBrand() throws IOException {
        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String[] strings = {"",""};
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("group_brand").field("brand")
                .subAggregation(AggregationBuilders.topHits("top_car").size(1)
                        .sort("price", SortOrder.DESC).fetchSource(new String[]{"model", "price"},new String[]{}))
        );

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get("group_brand");
        for (Terms.Bucket bucket: terms.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            TopHits topHits = bucket.getAggregations().get("top_car");
            Map<String, Object> sourceAsMap = topHits.getHits().getHits()[0].getSourceAsMap();
            String model = (String) sourceAsMap.get("model");
            Double price = (Double) sourceAsMap.get("price");
            System.out.println("group key:"+key+",count:"+count+",model:"+model+",price:"+price);
        }
    }

    //histogram区间统计:100w为一个范围,统计不同范围内车辆的销售量和平均价格
    @Test
    public  void  getAggsHistoGramSalesAndAvgPrice() throws IOException {
        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.histogram("histogram_by_price").field("price")
                .interval(1000000).subAggregation(AggregationBuilders.avg("avg_price").field("price")));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Histogram histogram = searchResponse.getAggregations().get("histogram_by_price");

        for (Histogram.Bucket bucket: histogram.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Avg avg = bucket.getAggregations().get("avg_price");
            double avgValue = avg.getValue();
            System.out.println("group key:"+key+",count:"+count+",avgValue:"+avgValue);
        }
    }

    // 以月为单位,统计不同月份汽车的销售数量及销售总金额:date_histogram
    @Test
    public  void  getAggsPerMonthSumByDateHistoGram() throws IOException {
        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // datehistogram日期区间范围查找,calendarInterval以什么为单位:这是指定月份
        searchSourceBuilder.aggregation(AggregationBuilders.dateHistogram("date_histogram_sum").field("sold_date")
                .calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MM-dd")
                .minDocCount(1).extendedBounds(new ExtendedBounds("2022-01-01", "2023-12-31"))
                .subAggregation(AggregationBuilders.sum("sum_price").field("price")));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Histogram histogram = searchResponse.getAggregations().get("date_histogram_sum");

        for (Histogram.Bucket bucket: histogram.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Sum sum = bucket.getAggregations().get("sum_price");
            double sumValue = sum.getValue();
            System.out.println("group key:"+key+",count:"+count+",avgValue:"+sumValue);
        }
    }

    // 统计每个品牌每种颜色的汽车销量和销售总额
    @Test
    public  void  getAggsSalesSumByGroupBrandAndGroupColor() throws IOException {
        SearchRequest  searchRequest = new SearchRequest("cars");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.aggregation(AggregationBuilders.terms("group_brand").field("brand")
                .subAggregation(AggregationBuilders.terms("group_color").field("color")
                        .subAggregation(AggregationBuilders.sum("sum_price").field("price")).order(BucketOrder.key(true))));

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        Terms terms = searchResponse.getAggregations().get("group_brand");

        for (Terms.Bucket bucket: terms.getBuckets()){
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            Terms terms1 = bucket.getAggregations().get("group_color");
            for (Terms.Bucket bucket1 : terms1.getBuckets()){
                String key1 = bucket1.getKeyAsString();
                long count1 = bucket1.getDocCount();
                Sum sum = bucket1.getAggregations().get("sum_price");
                double sumValue = sum.getValue();
                System.out.println("group key:"+key+",count:"+count+",key1:"+key1+",count1:"+count1+",sumValue:"+sumValue);
            }
        }
    }

以上案例都是经过本人亲测,如有疑惑,可以给我留言哦!!!,喜欢的话大家给我点关注点赞啊!!!
希望我们能互相进步哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值