RabbitMQ介紹
微服務間通訊有同步和異步兩種方式:
異步通訊:就像發郵件,不需要馬上回復(RabbitMQ)。
RabbitMQ中的一些角色:
-
publisher:生產者
-
consumer:消費者
-
exchange個:交換機,負責消息路由
-
queue:隊列,存儲消息
-
virtualHost:虛擬主機,隔離不同租戶的exchange、queue、消息的隔離
RabbitMQ安裝(基於Docker)
從docker倉庫拉去
docker pull rabbitmq:3-management
安裝
docker run \ -e RABBITMQ_DEFAULT_USER=itcast \ -e RABBITMQ_DEFAULT_PASS=123321 \ --name mq \ --hostname mq1 \ -p 15672:15672 \ -p 5672:5672 \ -d \ rabbitmq:3-management
這里第二行是設置登錄管理界面的用戶名,第三行是密碼,第四行是容器名字,第五行是主機名稱,第六行是管理界面所需要暴露的端口,第七行是RabbitMQ進程端口。
Elasticsearch 使用一種稱為 倒排索引 的結構,它適用於快速的全文搜索。一個倒排索引由文檔中所有不重復詞的列表構成,對於其中每個詞,有一個包含它的文檔列表。
elasticsearch是面向文檔(Document)存儲的,可以是數據庫中的一條商品數據,一個訂單信息。文檔數據會被序列化為json格式后存儲在elasticsearch中,
和MYSQL對比:
docker network create es-net
kibana拉取
docker pull kibana:7.12.1
安裝
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=es-net \
-p 5601:5601 \
kibana:7.12.1
第三行是kibana的地址,es是上面創建的網絡,來確保兩個在同一網絡環境
es拉取
docker pull elasticsearch:7.12.1
es安裝
docker run -d \ --name es \ -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \ -e "discovery.type=single-node" \ -v es-data:/usr/share/elasticsearch/data \ -v es-plugins:/usr/share/elasticsearch/plugins \ --privileged \ --network es-net \ -p 9200:9200 \ -p 9300:9300 \ elasticsearch:7.12.1
第三行是指定es的運行內存大小,可根據自己的機器修改,第四行是指定為非集群模式,五六行是掛在數據卷的位置。
RabbitMQ使用
通過SpringAMQP是基於RabbitMQ封裝的一套模板,並且還利用SpringBoot對其實現了自動裝配。
導入SpringAMQP依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
rabbitMQ---yml配置
spring:
rabbitmq:
host: 你的服務器ip地址
port: 5672
username: 配置時設置的用戶名
password: 密碼
virtual-host: /
消息發送(publisher)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
@Autowired private RabbitTemplate rabbitTemplate; @Test public void testSimpleQueue() { // 隊列名稱 String queueName = "simple.queue"; // 消息 String message = "hello, spring amqp!"; // 發送消息 rabbitTemplate.convertAndSend(queueName, message); }
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class SpringRabbitListener {
@RabbitListener(queues = "simple.queue")
public void listenSimpleQueueMessage(String msg) throws InterruptedException {
System.out.println("spring 消費者接收到消息:【" + msg + "】");
}
}
消息發送
@Test
public void testWorkQueue() throws InterruptedException {
// 隊列名稱
String queueName = "simple.queue";
// 消息
String message = "hello, message_";
for (int i = 0; i < 50; i++) {
// 發送消息
rabbitTemplate.convertAndSend(queueName, message + i);
Thread.sleep(20);
}
}
消息接收
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue1(String msg) throws InterruptedException {
System.out.println("消費者1接收到消息:【" + msg + "】" + LocalTime.now());
Thread.sleep(20);
}
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue2(String msg) throws InterruptedException {
System.err.println("消費者2........接收到消息:【" + msg + "】" + LocalTime.now());
Thread.sleep(200);
}
綁定隊列和交換機在消費者consumer服務中
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConfig {
/**
* 聲明交換機
* @return Fanout類型交換機
*/
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("itcast.fanout");
}
/**
* 第1個隊列
*/
@Bean
public Queue fanoutQueue1(){
return new Queue("fanout.queue1");
}
/**
* 綁定隊列和交換機
*/
@Bean
public Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
}
/**
* 第2個隊列
*/
@Bean
public Queue fanoutQueue2(){
return new Queue("fanout.queue2");
}
/**
* 綁定隊列和交換機
*/
@Bean
public Binding bindingQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
}
}
消息發送
@Test
public void testFanoutExchange() {
// 隊列名稱
String exchangeName = "itcast.fanout";
// 消息
String message = "hello, everyone!";
rabbitTemplate.convertAndSend(exchangeName, "", message);
}
消息接收
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String msg) {
System.out.println("消費者1接收到Fanout消息:【" + msg + "】");
}
@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String msg) {
System.out.println("消費者2接收到Fanout消息:【" + msg + "】");
}

@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue1"),
exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
key = {"red", "blue"}
))
public void listenDirectQueue1(String msg){
System.out.println("消費者接收到direct.queue1的消息:【" + msg + "】");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue2"),
exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
key = {"red", "yellow"}
))
public void listenDirectQueue2(String msg){
System.out.println("消費者接收到direct.queue2的消息:【" + msg + "】");
}
@Test
public void testSendDirectExchange() {
// 交換機名稱
String exchangeName = "itcast.direct";
// 消息
String message = "message ";
// 發送消息
rabbitTemplate.convertAndSend(exchangeName, "red", message);
}
顯然,JDK序列化方式並不合適。我們希望消息體的體積更小、可讀性更高,因此可以使用JSON方式來做序列化和反序列化。
在publisher和consumer兩個服務中都引入依賴:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.10</version>
</dependency>
配置消息轉換器。
在啟動類中添加一個Bean即可:
@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}
小結:服務者publisher消息發送只需要管要發送的交換機名字、Binding和消息,而消費者需要監聽是否收到消息
首先打開ip:5601

創建索引庫和映射
PUT /索引庫名稱
{
"mappings": {
"properties": {
"字段名":{
"type": "text",
"analyzer": "ik_smart"
},
"字段名2":{
"type": "keyword",
"index": "false"
},
"字段名3":{
"properties": {
"子字段": {
"type": "keyword"
}
}
},
// ...略
}
}
}
GET /索引庫名
修改索引庫
PUT /索引庫名/_mapping
{
"properties": {
"新字段名":{
"type": "integer"
}
}
}
DELETE /索引庫名
文檔操作
新增文檔
POST /索引庫名/_doc/文檔id
{
"字段1": "值1",
"字段2": "值2",
"字段3": {
"子屬性1": "值3",
"子屬性2": "值4"
},
// ...
}
例:
POST /test/_doc/1
{
"info": "Java講師",
"email": "123@qq.cn",
"name": {
"firstName": "雲",
"lastName": "趙"
}
}
查詢文檔
GET /{索引庫名稱}/_doc/{id}
例:
GET /test/_doc/1
刪除文檔
DELETE /{索引庫名}/_doc/id值
PUT /{索引庫名}/_doc/文檔id
{
"字段1": "值1",
"字段2": "值2",
// ... 略
}
POST /heima/_update/1
{
"doc": {
"email": "ZhaoYun@qq.cn"
}
}
PUT /hotel
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword",
"copy_to": "all"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
-
location:地理坐標,里面包含精度、緯度
-
all:一個組合字段,其目的是將多字段的值 利用copy_to合並,提供給用戶搜索
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
因為SpringBoot默認的ES版本是7.6.2,所以我們需要覆蓋默認的ES版本
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
初始化RestHighLevelClient
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class HotelIndexTest {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
@Test
void createHotelIndex() throws IOException {
// 1.創建Request對象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.准備請求的參數:DSL語句
request.source(*DSL語句*, XContentType.JSON);
// 3.發送請求
client.indices().create(request, RequestOptions.DEFAULT);
}
@Test
void testDeleteHotelIndex() throws IOException {
// 1.創建Request對象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 2.發送請求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@Test
void testExistsHotelIndex() throws IOException {
// 1.創建Request對象
GetIndexRequest request = new GetIndexRequest("hotel");
// 2.發送請求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 3.輸出
System.err.println(exists ? "索引庫已經存在!" : "索引庫不存在!");
}
@Test
void testAddDocument() throws IOException {
// 1.根據id查詢酒店數據
Hotel hotel = hotelService.getById(61083L);
// 2.將hotel 轉json
String json = JSON.toJSONString(hotel );
// 1.准備Request對象
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
// 2.准備Json文檔
request.source(json, XContentType.JSON);
// 3.發送請求
client.index(request, RequestOptions.DEFAULT);
}
@Test
void testGetDocumentById() throws IOException {
// 1.准備Request
GetRequest request = new GetRequest("hotel", "61082");
// 2.發送請求,得到響應
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 3.解析響應結果
String json = response.getSourceAsString();
HotelDoc hotel = JSON.parseObject(json, Hotel.class);
System.out.println(hotel);
}
@Test
void testDeleteDocument() throws IOException {
// 1.准備Request
DeleteRequest request = new DeleteRequest("hotel", "61083");
// 2.發送請求
client.delete(request, RequestOptions.DEFAULT);
}
@Test
void testUpdateDocument() throws IOException {
// 1.准備Request
UpdateRequest request = new UpdateRequest("hotel", "61083");
// 2.准備請求參數
request.doc(
"price", "952",
"starName", "四鑽"
);
// 3.發送請求
client.update(request, RequestOptions.DEFAULT);
}
@Test
void testBulkRequest() throws IOException {
// 批量查詢酒店數據
List<Hotel> hotels = hotelService.list();
// 1.創建Request
BulkRequest request = new BulkRequest();
// 2.准備參數,添加多個新增的Request
for (Hotel hotel : hotels) {
// 2.1.創建新增文檔的Request對象
request.add(new IndexRequest("hotel")
.id(hotel.getId().toString())
.source(JSON.toJSONString(hotel), XContentType.JSON));
}
// 3.發送請求
client.bulk(request, RequestOptions.DEFAULT);
}
-
-
全文檢索(full text)查詢:利用分詞器對用戶輸入內容分詞,然后去倒排索引庫中匹配。例如:
-
match查詢:單字段查詢
-
multi_match查詢:多字段查詢,任意一個字段符合條件就算符合查詢條件
-
match和multi_match的區別是什么?
-
match:根據一個字段查詢
-
-
-
-
-
精確查詢:根據精確詞條值查找數據,一般是查找keyword、數值、日期、boolean等類型字段。例如:
-
ids 根據id查詢
-
range 根據值的范圍查詢
-
term 根據詞條精確值查詢
-
-
地理(geo)查詢:根據經緯度查詢。例如:
-
geo_distance 附近查詢,也叫做距離查詢
-
geo_bounding_box 矩形范圍查詢
-
-
復合(compound)查詢:復合查詢可以將上述各種查詢條件組合起來,合並查詢條件。例如:
function score 查詢中包含四部分內容:
-
原始查詢條件:query部分,基於這個條件搜索文檔,並且基於BM25算法給文檔打分,原始算分(query score)
-
過濾條件:filter部分,符合該條件的文檔才會重新算分
-
算分函數:符合filter條件的文檔要根據這個函數做運算,得到的函數算分(function score),有四種函數
-
weight:函數結果是常量
-
field_value_factor:以文檔中的某個字段值作為函數結果
-
random_score:以隨機數作為函數結果
-
script_score:自定義算分函數算法
-
-
運算模式:算分函數的結果、原始查詢的相關性算分,兩者之間的運算方式,包括:
-
multiply:相乘
-
replace:用function score替換query score
-
其它,例如:sum、avg、max、min
-
function score的運行流程如下:
-
1)根據原始條件查詢搜索文檔,並且計算相關性算分,稱為原始算分(query score)
-
2)根據過濾條件,過濾文檔
-
3)符合過濾條件的文檔,基於算分函數運算,得到函數算分(function score)
-
4)將原始算分(query score)和函數算分(function score)基於運算模式做運算,得到最終結果,作為相關性算分。
因此,其中的關鍵點是:
-
過濾條件:決定哪些文檔的算分被修改
-
算分函數:決定函數算分的算法
-
運算模式:決定最終算分結果
示例:
GET /hotel/_search
{
"query": {
"function_score": {
"query": { .... }, // 原始查詢,可以是任意條件
"functions": [ // 算分函數
{
"filter": { // 滿足的條件,品牌必須是如家
"term": {
"brand": "如家"
}
},
"weight": 2 // 算分權重為2
}
],
"boost_mode": "sum" // 加權模式,求和
}
}
}
布爾查詢(bool)
布爾查詢是一個或多個查詢子句的組合,每一個子句就是一個子查詢。子查詢的組合方式有:
-
must:必須匹配每個子查詢,類似“與”
-
should:選擇性匹配子查詢,類似“或”
-
must_not:必須不匹配,不參與算分,類似“非”
-
filter:必須匹配,不參與算分
每一個不同的字段,其查詢的條件、方式都不一樣,必須是多個不同的查詢,而要組合這些查詢,就必須用bool查詢了。
需要注意的是,搜索時,參與打分的字段越多,查詢的性能也越差。因此這種多條件查詢時,建議這樣做:
-
搜索框的關鍵字搜索,是全文檢索查詢,使用must查詢,參與算分
-
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {"city": "上海" }}
],
"should": [
{"term": {"brand": "皇冠假日" }},
{"term": {"brand": "華美達" }}
],
"must_not": [
{ "range": { "price": { "lte": 500 } }}
],
"filter": [
{ "range": {"score": { "gte": 45 } }}
]
}
}
}
查詢的語法基本一致:
GET /indexName/_search
{
"query": {
"查詢類型": {
"查詢條件": "條件值"
}
}
}
搜索結果處理
排序
普通字段排序
排序條件是一個數組,也就是可以寫多個排序條件。按照聲明的順序,當第一個條件相等時,再按照第二個條件排序,以此類推
GET /indexName/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"FIELD": "desc" // 排序字段、排序方式ASC、DESC
}
]
}
地理坐標排序
GET /indexName/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance" : {
"FIELD" : "緯度,經度", // 文檔中geo_point類型的字段名、目標坐標點
"order" : "asc", // 排序方式
"unit" : "km" // 排序的距離單位
}
}
]
}
這個查詢的含義是:
-
指定一個坐標,作為目標點
-
計算每一個文檔中,指定字段(必須是geo_point類型)的坐標 到目標點的距離是多少
-
根據距離排序
基本的分頁
GET /hotel/_search { "query": { "match_all": {} }, "from": 0, // 分頁開始的位置,默認為0 "size": 10, // 期望獲取的文檔總數 "sort": [ {"price": "asc"} ] }
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 990, // 分頁開始的位置,默認為0
"size": 10, // 期望獲取的文檔總數
"sort": [
{"price": "asc"}
]
}
GET /hotel/_search
{
"query": {
"match": {
"FIELD": "TEXT" // 查詢條件,高亮一定要使用全文檢索查詢
}
},
"highlight": {
"fields": { // 指定要高亮的字段
"FIELD": {
"pre_tags": "<em>", // 用來標記高亮字段的前置標簽
"post_tags": "</em>" // 用來標記高亮字段的后置標簽
}
}
}
}
-
-
第二步,利用
request.source()構建DSL,DSL中可以包含查詢、分頁、排序、高亮等-
query():代表查詢條件,利用QueryBuilders.matchAllQuery()構建一個match_all查詢的DSL
-
-
示例:
@Test void testMatchAll() throws IOException { // 1.准備Request SearchRequest request = new SearchRequest("hotel"); // 2.准備DSL request.source() .query(QueryBuilders.matchAllQuery()); // 3.發送請求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); // 4.解析響應 handleResponse(response); } private void handleResponse(SearchResponse response) { // 4.解析響應 SearchHits searchHits = response.getHits(); // 4.1.獲取總條數 long total = searchHits.getTotalHits().value; System.out.println("共搜索到" + total + "條數據"); // 4.2.文檔數組 SearchHit[] hits = searchHits.getHits(); // 4.3.遍歷 for (SearchHit hit : hits) { // 獲取文檔source String json = hit.getSourceAsString(); // 反序列化 HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); System.out.println("hotelDoc = " + hotelDoc); } }
@Test
void testMatch() throws IOException {
// 1.准備Request
SearchRequest request = new SearchRequest("hotel");
// 2.准備DSL
request.source()
.query(QueryBuilders.matchQuery("all", "如家"));
// 3.發送請求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析響應
handleResponse(response);
}
QueryBuilders.termQuery(“字段”,“值”);
QueryBuilders.rangeQuery(“字段”).gte(min).lte(max);
布爾查詢
與其它查詢的差別同樣是在查詢條件的構建,QueryBuilders,結果解析等其他代碼完全不變。
@Test
void testBool() throws IOException {
// 1.准備Request
SearchRequest request = new SearchRequest("hotel");
// 2.准備DSL
// 2.1.准備BooleanQuery
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 2.2.添加term
boolQuery.must(QueryBuilders.termQuery("city", "杭州"));
// 2.3.添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
request.source().query(boolQuery);
// 3.發送請求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析響應
handleResponse(response);
}
@Test
void testPageAndSort() throws IOException {
// 頁碼,每頁大小
int page = 1, size = 5;
// 1.准備Request
SearchRequest request = new SearchRequest("hotel");
// 2.准備DSL
// 2.1.query
request.source().query(QueryBuilders.matchAllQuery());
// 2.2.排序 sort
request.source().sort("price", SortOrder.ASC);
// 2.3.分頁 from、size
request.source().from((page - 1) * size).size(5);
// 3.發送請求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析響應
handleResponse(response);
}
@Test
void testHighlight() throws IOException {
// 1.准備Request
SearchRequest request = new SearchRequest("hotel");
// 2.准備DSL
// 2.1.query
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 2.2.高亮
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
// 3.發送請求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析響應
handleResponse(response);
}
高粱結果解析
private void handleResponse(SearchResponse response) {
// 4.解析響應
SearchHits searchHits = response.getHits();
// 4.1.獲取總條數
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "條數據");
// 4.2.文檔數組
SearchHit[] hits = searchHits.getHits();
// 4.3.遍歷
for (SearchHit hit : hits) {
// 獲取文檔source
String json = hit.getSourceAsString();
// 反序列化
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
// 獲取高亮結果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (!CollectionUtils.isEmpty(highlightFields)) {
// 根據字段名獲取高亮結果
HighlightField highlightField = highlightFields.get("name");
if (highlightField != null) {
// 獲取高亮值
String name = highlightField.getFragments()[0].string();
// 覆蓋非高亮結果
hotelDoc.setName(name);
}
}
System.out.println("hotelDoc = " + hotelDoc);
}
}
可以讓我們極其方便的實現對數據的統計、分析、運算。例如:
-
什么品牌的手機最受歡迎?
-
這些手機的平均價格、最高價格、最低價格?
-
這些手機每月的銷售情況如何?
實現這些統計功能的比數據庫的sql要方便的多,而且查詢速度非常快,可以實現近實時搜索效果。
聚合常見的有三類:
-
桶(Bucket)聚合:用來對文檔做分組
-
TermAggregation:按照文檔字段值分組,例如按照品牌值分組、按照國家分組
-
Date Histogram:按照日期階梯分組,例如一周為一組,或者一月為一組
-
-
度量(Metric)聚合:用以計算一些值,比如:最大值、最小值、平均值等
-
Avg:求平均值
-
Max:求最大值
-
Min:求最小值
-
Stats:同時求max、min、avg、sum等
-
-
管道(pipeline)聚合:其它聚合的結果為基礎做聚合
注意:參加聚合的字段必須是keyword、日期、數值、布爾類型
DSL實現
GET /hotel/_search
{
"size": 0, // 設置size為0,結果中不包含文檔,只包含聚合結果
"aggs": { // 定義聚合
"brandAgg": { //給聚合起個名字
"terms": { // 聚合的類型,按照品牌值聚合,所以選擇term
"field": "brand", // 參與聚合的字段
"size": 20 // 希望獲取的聚合結果數量
}
}
}
}
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"order": {
"_count": "asc" // 按照_count升序排列
},
"size": 20
}
}
}
}
GET /hotel/_search
{
"query": {
"range": {
"price": {
"lte": 200 // 只對200元以下的文檔聚合
}
}
},
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 20
}
}
}
}
RestAPI
@Override
public Map<String, List<String>> filters(RequestParams params) {
try {
// 1.准備Request
SearchRequest request = new SearchRequest("hotel");
// 2.准備DSL
// 2.1.query
buildBasicQuery(params, request);
// 2.2.設置size
request.source().size(0);
// 2.3.聚合
buildAggregation(request);
// 3.發出請求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析結果
Map<String, List<String>> result = new HashMap<>();
Aggregations aggregations = response.getAggregations();
// 4.1.根據品牌名稱,獲取品牌結果
List<String> brandList = getAggByName(aggregations, "brandAgg");
result.put("品牌", brandList);
// 4.2.根據品牌名稱,獲取品牌結果
List<String> cityList = getAggByName(aggregations, "cityAgg");
result.put("城市", cityList);
// 4.3.根據品牌名稱,獲取品牌結果
List<String> starList = getAggByName(aggregations, "starAgg");
result.put("星級", starList);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void buildAggregation(SearchRequest request) {
request.source().aggregation(AggregationBuilders
.terms("brandAgg")
.field("brand")
.size(100)
);
request.source().aggregation(AggregationBuilders
.terms("cityAgg")
.field("city")
.size(100)
);
request.source().aggregation(AggregationBuilders
.terms("starAgg")
.field("starName")
.size(100)
);
}
private List<String> getAggByName(Aggregations aggregations, String aggName) {
// 4.1.根據聚合名稱獲取聚合結果
Terms brandTerms = aggregations.get(aggName);
// 4.2.獲取buckets
List<? extends Terms.Bucket> buckets = brandTerms.getBuckets();
// 4.3.遍歷
List<String> brandList = new ArrayList<>();
for (Terms.Bucket bucket : buckets) {
// 4.4.獲取key
String key = bucket.getKeyAsString();
brandList.add(key);
}
return brandList;
}

