springboot項目綁定域名,springboot實體映射到數據庫_SpringBoot 操作 ElasticSearch 詳解

 2023-10-13 阅读 28 评论 0

摘要:點擊上方?SpringForAll社區?輕松關注!及時獲取有趣有料的技術文章本文來源:http://www.mydlq.club/article/64/springboot項目綁定域名,一、ElasticSearch 簡介. 1、簡介靜態資源映射?. 2、特性. 3、使用場景import Java,. 二、ElasticSearch 基礎概念. 1、
點擊上方?SpringForAll社區?輕松關注!及時獲取有趣有料的技術文章

本文來源:http://www.mydlq.club/article/64/

springboot項目綁定域名,一、ElasticSearch 簡介

. 1、簡介

靜態資源映射?. 2、特性

. 3、使用場景

import Java,. 二、ElasticSearch 基礎概念

. 1、ElaticSearch 和 DB 的關系

. 2、索引

. 3、文檔

. 4、映射

. 三、SpringBoot 項目引入 ElasticSearch 依賴

. 1、Maven 引入相關依賴

. 2、ElasticSearch 連接配置

. 四、索引操作示例

. 1、Restful 操作示例

. 2、Java 代碼示例

. 五、文檔操作示例

. 1、Restful 操作示例

. 2、Java 代碼示例

. 六、插入初始化數據

. 1、單條插入

. 2、批量插入

. 3、查詢數據

. 七、查詢操作示例

. 1、精確查詢(term)

. 2、匹配查詢(match)

. 3、模糊查詢(fuzzy)

. 4、范圍查詢(range)

. 5、通配符查詢(wildcard)

. 6、布爾查詢(bool)

. 八、聚合查詢操作示例

. 1、Metric 聚合分析

. 2、Bucket 聚合分析

. 3、Metric 與 Bucket 聚合分析


參考地址:

  • 百度百科

  • 博文示例項目 Github 地址:https://github.com/my-dlq/blog-example/tree/master/springboot/springboot-elasticsearch-example

系統環境:

  • SpringBoot 版本:2.2.4

  • ElasticSearch 版本:6.5.3

一、ElasticSearch 簡介

1、簡介

ElasticSearch 是一個基于 Lucene 的搜索服務器。它提供了一個分布式多員工能力的全文搜索引擎,基于 RESTful web 接口。Elasticsearch 是用 Java 語言開發的,并作為 Apache 許可條款下的開放源碼發布,是一種流行的企業級搜索引擎。

ElasticSearch 用于云計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。

2、特性

  • 分布式的文檔存儲引擎

  • 分布式的搜索引擎和分析引擎

  • 分布式,支持PB級數據

3、使用場景

  • 搜索領域:?如百度、谷歌,全文檢索等。

  • 門戶網站:?訪問統計、文章點贊、留言評論等。

  • 廣告推廣:?記錄員工行為數據、消費趨勢、員工群體進行定制推廣等。

  • 信息采集:?記錄應用的埋點數據、訪問日志數據等,方便大數據進行分析。

二、ElasticSearch 基礎概念

1、ElaticSearch 和 DB 的關系

在?Elasticsearch?中,文檔歸屬于一種類型?type,而這些類型存在于索引?index?中,我們可以列一些簡單的不同點,來類比傳統關系型數據庫:

  • Relational DB -> Databases -> Tables -> Rows -> Columns

  • Elasticsearch -> Indices -> Types -> Documents -> Fields

Elasticsearch 集群可以包含多個索引?indices,每一個索引可以包含多個類型?types,每一個類型包含多個文檔?documents,然后每個文檔包含多個字段?Fields。而在 DB 中可以有多個數據庫?Databases,每個庫中可以有多張表?Tables,沒個表中又包含多行Rows,每行包含多列Columns

2、索引

  • 索引基本概念(indices):

索引是含義相同屬性的文檔集合,是 ElasticSearch 的一個邏輯存儲,可以理解為關系型數據庫中的數據庫,ElasticSearch 可以把索引數據存放到一臺服務器上,也可以 sharding 后存到多臺服務器上,每個索引有一個或多個分片,每個分片可以有多個副本。

  • 索引類型(index_type):

索引可以定義一個或多個類型,文檔必須屬于一個類型。在?ElasticSearch?中,一個索引對象可以存儲多個不同用途的對象,通過索引類型可以區分單個索引中的不同對象,可以理解為關系型數據庫中的表。每個索引類型可以有不同的結構,但是不同的索引類型不能為相同的屬性設置不同的類型。

3、文檔

  • 文檔(document):

文檔是可以被索引的基本數據單位。存儲在 ElasticSearch 中的主要實體叫文檔?document,可以理解為關系型數據庫中表的一行記錄。每個文檔由多個字段構成,ElasticSearch 是一個非結構化的數據庫,每個文檔可以有不同的字段,并且有一個唯一的標識符。

4、映射

  • 映射(mapping):

ElasticSearch?的?Mapping?非常類似于靜態語言中的數據類型:聲明一個變量為 int 類型的變量,以后這個變量都只能存儲?int?類型的數據。同樣的,一個?number?類型的?mapping?字段只能存儲?number?類型的數據。

同語言的數據類型相比,Mapping?還有一些其他的含義,Mapping?不僅告訴 ElasticSearch 一個?Field?中是什么類型的值, 它還告訴 ElasticSearch 如何索引數據以及數據是否能被搜索到。

ElaticSearch 默認是動態創建索引和索引類型的 Mapping 的。這就相當于無需定義?Solr?中的?Schema,無需指定各個字段的索引規則就可以索引文件,很方便。但有時方便就代表著不靈活。比如,ElasticSearch 默認一個字段是要做分詞的,但我們有時要搜索匹配整個字段卻不行。如有統計工作要記錄每個城市出現的次數。對于?name?字段,若記錄?new york?文本,ElasticSearch 可能會把它拆分成?new?和?york?這兩個詞,分別計算這個兩個單詞的次數,而不是我們期望的?new york

三、SpringBoot 項目引入 ElasticSearch 依賴

下面介紹下 SpringBoot 如何通過?elasticsearch-rest-high-level-client?工具操作?ElasticSearch,這里需要說一下,為什么沒有使用 Spring 家族封裝的?spring-data-elasticsearch

主要原因是靈活性和更新速度,Spring 將 ElasticSearch 過度封裝,讓開發者很難跟 ES 的 DSL 查詢語句進行關聯。再者就是更新速度,ES 的更新速度是非常快,但是 spring-data-elasticsearch 更新速度比較緩慢。

由于上面兩點,所以選擇了官方推出的 Java 客戶端?elasticsearch-rest-high-level-client,它的代碼寫法跟 DSL 語句很相似,懂 ES 查詢的使用其上手很快。

示例項目地址:https://github.com/my-dlq/blog-example/tree/master/springboot/springboot-elasticsearch-example

1、Maven 引入相關依賴

  • lombok:?lombok 工具依賴。

  • fastjson:?用于將 JSON 轉換對象的依賴。

  • spring-boot-starter-web:?SpringBoot 的 Web 依賴。

  • elasticsearch:ElasticSearch:?依賴,需要和 ES 版本保持一致。

  • elasticsearch-rest-high-level-client:?用于操作 ES 的 Java 客戶端。

 1<?xml ?version="1.0"?encoding="UTF-8"?>
2<project?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3?????????xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?https://maven.apache.org/xsd/maven-4.0.0.xsd">
4????<modelVersion>4.0.0modelVersion>
5????<parent>
6????????<groupId>org.springframework.bootgroupId>
7????????<artifactId>spring-boot-starter-parentartifactId>
8????????<version>2.2.4.RELEASEversion>
9????????<relativePath/>?
10????parent>
11????<groupId>club.mydlqgroupId>
12????<artifactId>springboot-elasticsearch-exampleartifactId>
13????<version>0.0.1-SNAPSHOTversion>
14????<name>springboot-elasticsearch-examplename>
15????<description>Demo?project?for?Spring?Boot?ElasticSearchdescription>
16
17????<properties>
18????????<java.version>1.8java.version>
19????properties>
20????<dependencies>
21????????
22????????<dependency>
23????????????<groupId>org.springframework.bootgroupId>
24????????????<artifactId>spring-boot-starter-webartifactId>
25????????dependency>
26????????
27????????<dependency>
28????????????<groupId>org.projectlombokgroupId>
29????????????<artifactId>lombokartifactId>
30????????????<optional>trueoptional>
31????????dependency>
32????????
33????????<dependency>
34????????????<groupId>com.alibabagroupId>
35????????????<artifactId>fastjsonartifactId>
36????????????<version>1.2.61version>
37????????dependency>
38????????
39????????<dependency>
40????????????<groupId>org.elasticsearch.clientgroupId>
41????????????<artifactId>elasticsearch-rest-high-level-clientartifactId>
42????????????<version>6.5.4version>
43????????dependency>
44????????<dependency>
45????????????<groupId>org.elasticsearchgroupId>
46????????????<artifactId>elasticsearchartifactId>
47????????????<version>6.5.4version>
48????????dependency>
49????dependencies>
50
51????<build>
52????????<plugins>
53????????????<plugin>
54????????????????<groupId>org.springframework.bootgroupId>
55????????????????<artifactId>spring-boot-maven-pluginartifactId>
56????????????plugin>
57????????plugins>
58????build>
59
60project>

2、ElasticSearch 連接配置

(1)、application.yml 配置文件

為了方便更改連接 ES 的連接配置,所以我們將配置信息放置于 application.yaml 中:

 1#base
2server:
3??port:?8080
4#spring
5spring:
6??application:
7????name:?springboot-elasticsearch-example
8#elasticsearch
9elasticsearch:
10??schema:?http
11??address:?127.0.0.1:9200
12??connectTimeout:?5000
13??socketTimeout:?5000
14??connectionRequestTimeout:?5000
15??maxConnectNum:?100
16??maxConnectPerRoute:?100

(2)、java 連接配置類

這里需要寫一個 Java 配置類讀取 application 中的配置信息:

 1import?org.apache.http.HttpHost;
2import?org.elasticsearch.client.RestClient;
3import?org.elasticsearch.client.RestClientBuilder;
4import?org.elasticsearch.client.RestHighLevelClient;
5import?org.springframework.beans.factory.annotation.Value;
6import?org.springframework.context.annotation.Bean;
7import?org.springframework.context.annotation.Configuration;
8import?java.util.ArrayList;
9import?java.util.List;
10
11/**12?*?ElasticSearch?配置13?*/
14@Configuration
15public?class?ElasticSearchConfig?{
16
17????/**?協議?*/
18????@Value("${elasticsearch.schema:http}")
19????private?String?schema;
20
21????/**?集群地址,如果有多個用“,”隔開?*/
22????@Value("${elasticsearch.address}")
23????private?String?address;
24
25????/**?連接超時時間?*/
26????@Value("${elasticsearch.connectTimeout:5000}")
27????private?int?connectTimeout;
28
29????/**?Socket?連接超時時間?*/
30????@Value("${elasticsearch.socketTimeout:10000}")
31????private?int?socketTimeout;
32
33????/**?獲取連接的超時時間?*/
34????@Value("${elasticsearch.connectionRequestTimeout:5000}")
35????private?int?connectionRequestTimeout;
36
37????/**?最大連接數?*/
38????@Value("${elasticsearch.maxConnectNum:100}")
39????private?int?maxConnectNum;
40
41????/**?最大路由連接數?*/
42????@Value("${elasticsearch.maxConnectPerRoute:100}")
43????private?int?maxConnectPerRoute;
44
45????@Bean
46????public?RestHighLevelClient?restHighLevelClient()?{
47????????//?拆分地址
48????????List?hostLists?=?new?ArrayList<>();49????????String[]?hostList?=?address.split(",");50????????for?(String?addr?:?hostList)?{51????????????String?host?=?addr.split(":")[0];52????????????String?port?=?addr.split(":")[1];53????????????hostLists.add(new?HttpHost(host,?Integer.parseInt(port),?schema));54????????}55????????//?轉換成?HttpHost?數組56????????HttpHost[]?httpHost?=?hostLists.toArray(new?HttpHost[]{});57????????//?構建連接對象58????????RestClientBuilder?builder?=?RestClient.builder(httpHost);59????????//?異步連接延時配置60????????builder.setRequestConfigCallback(requestConfigBuilder?->?{61????????????requestConfigBuilder.setConnectTimeout(connectTimeout);62????????????requestConfigBuilder.setSocketTimeout(socketTimeout);63????????????requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);64????????????return?requestConfigBuilder;65????????});66????????//?異步連接數配置67????????builder.setHttpClientConfigCallback(httpClientBuilder?->?{68????????????httpClientBuilder.setMaxConnTotal(maxConnectNum);69????????????httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);70????????????return?httpClientBuilder;71????????});72????????return?new?RestHighLevelClient(builder);73????}7475}

四、索引操作示例

這里示例會指出通過 Kibana 的 Restful 工具操作與對應的 Java 代碼操作的兩個示例。

1、Restful 操作示例

創建索引

創建名為?mydlq-user?的索引與對應 Mapping。

 1PUT?/mydlq-user
2{
3??"mappings":?{
4????"doc":?{
5??????"dynamic":?true,
6??????"properties":?{
7????????"name":?{
8??????????"type":?"text",
9??????????"fields":?{
10????????????"keyword":?{
11??????????????"type":?"keyword"
12????????????}
13??????????}
14????????},
15????????"address":?{
16??????????"type":?"text",
17??????????"fields":?{
18????????????"keyword":?{
19??????????????"type":?"keyword"
20????????????}
21??????????}
22????????},
23????????"remark":?{
24??????????"type":?"text",
25??????????"fields":?{
26????????????"keyword":?{
27??????????????"type":?"keyword"
28????????????}
29??????????}
30????????},
31????????"age":?{
32??????????"type":?"integer"
33????????},
34????????"salary":?{
35??????????"type":?"float"
36????????},
37????????"birthDate":?{
38??????????"type":?"date",
39??????????"format":?"yyyy-MM-dd"
40????????},
41????????"createTime":?{
42??????????"type":?"date"
43????????}
44??????}
45????}
46??}
47}

刪除索引

刪除 mydlq-user 索引。

1DELETE?/mydlq-user

2、Java 代碼示例

  1import?lombok.extern.slf4j.Slf4j;
2import?org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3import?org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
4import?org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
5import?org.elasticsearch.action.support.master.AcknowledgedResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.common.settings.Settings;
9import?org.elasticsearch.common.xcontent.XContentBuilder;
10import?org.elasticsearch.common.xcontent.XContentFactory;
11import?org.springframework.beans.factory.annotation.Autowired;
12import?org.springframework.stereotype.Service;
13import?java.io.IOException;
14
15@Slf4j
16@Service
17public?class?IndexService2?{
18
19????@Autowired
20????private?RestHighLevelClient?restHighLevelClient;
21
22????/** 23?????*?創建索引 24?????*/
25????public?void?createIndex()?{
26????????try?{
27????????????//?創建?Mapping
28????????????XContentBuilder?mapping?=?XContentFactory.jsonBuilder()
29????????????????.startObject()
30????????????????????.field("dynamic",?true)
31????????????????????.startObject("properties")
32????????????????????????.startObject("name")
33????????????????????????????.field("type","text")
34????????????????????????????.startObject("fields")
35????????????????????????????????.startObject("keyword")
36????????????????????????????????????.field("type","keyword")
37????????????????????????????????.endObject()
38????????????????????????????.endObject()
39????????????????????????.endObject()
40????????????????????????.startObject("address")
41????????????????????????????.field("type","text")
42????????????????????????????.startObject("fields")
43????????????????????????????????.startObject("keyword")
44????????????????????????????????????.field("type","keyword")
45????????????????????????????????.endObject()
46????????????????????????????.endObject()
47????????????????????????.endObject()
48????????????????????????.startObject("remark")
49????????????????????????????.field("type","text")
50????????????????????????????.startObject("fields")
51????????????????????????????????.startObject("keyword")
52????????????????????????????????????.field("type","keyword")
53????????????????????????????????.endObject()
54????????????????????????????.endObject()
55????????????????????????.endObject()
56????????????????????????.startObject("age")
57????????????????????????????.field("type","integer")
58????????????????????????.endObject()
59????????????????????????.startObject("salary")
60????????????????????????????.field("type","float")
61????????????????????????.endObject()
62????????????????????????.startObject("birthDate")
63????????????????????????????.field("type","date")
64????????????????????????????.field("format",?"yyyy-MM-dd")
65????????????????????????.endObject()
66????????????????????????.startObject("createTime")
67????????????????????????????.field("type","date")
68????????????????????????.endObject()
69????????????????????.endObject()
70????????????????.endObject();
71????????????//?創建索引配置信息,配置
72????????????Settings?settings?=?Settings.builder()
73????????????????????.put("index.number_of_shards",?1)
74????????????????????.put("index.number_of_replicas",?0)
75????????????????????.build();
76????????????//?新建創建索引請求對象,然后設置索引類型(ES?7.0?將不存在索引類型)和?mapping?與?index?配置
77????????????CreateIndexRequest?request?=?new?CreateIndexRequest("mydlq-user",?settings);
78????????????request.mapping("doc",?mapping);
79????????????//?RestHighLevelClient?執行創建索引
80????????????CreateIndexResponse?createIndexResponse?=?restHighLevelClient.indices().create(request,?RequestOptions.DEFAULT);
81????????????//?判斷是否創建成功
82????????????boolean?isCreated?=?createIndexResponse.isAcknowledged();
83????????????log.info("是否創建成功:{}",?isCreated);
84????????}?catch?(IOException?e)?{
85????????????log.error("",?e);
86????????}
87????}
88
89????/** 90?????*?刪除索引 91?????*/
92????public?void?deleteIndex()?{
93????????try?{
94????????????//?新建刪除索引請求對象
95????????????DeleteIndexRequest?request?=?new?DeleteIndexRequest("mydlq-user");
96????????????//?執行刪除索引
97????????????AcknowledgedResponse?acknowledgedResponse?=?restHighLevelClient.indices().delete(request,?RequestOptions.DEFAULT);
98????????????//?判斷是否刪除成功
99????????????boolean?siDeleted?=?acknowledgedResponse.isAcknowledged();
100????????????log.info("是否刪除成功:{}",?siDeleted);
101????????}?catch?(IOException?e)?{
102????????????log.error("",?e);
103????????}
104????}
105
106}

五、文檔操作示例

1、Restful 操作示例

增加文檔信息

在索引?mydlq-user?中增加一條文檔信息。

 1POST?/mydlq-user/doc
2{
3????"address":?"北京市",
4????"age":?29,
5????"birthDate":?"1990-01-10",
6????"createTime":?1579530727699,
7????"name":?"張三",
8????"remark":?"來自北京市的張先生",
9????"salary":?100
10}

獲取文檔信息

獲取?mydlq-user?的索引 id=1 的文檔信息。

1GET?/mydlq-user/doc/1

更新文檔信息

更新之前創建的 id=1 的文檔信息。

 1PUT?/mydlq-user/doc/1
2{
3????"address":?"北京市海淀區",
4????"age":?29,
5????"birthDate":?"1990-01-10",
6????"createTime":?1579530727699,
7????"name":?"張三",
8????"remark":?"來自北京市的張先生",
9????"salary":?100
10}

刪除文檔信息

刪除之前創建的 id=1 的文檔信息。

1DELETE?/mydlq-user/doc/1

2、Java 代碼示例

  1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.delete.DeleteRequest;
5import?org.elasticsearch.action.delete.DeleteResponse;
6import?org.elasticsearch.action.get.GetRequest;
7import?org.elasticsearch.action.get.GetResponse;
8import?org.elasticsearch.action.index.IndexRequest;
9import?org.elasticsearch.action.index.IndexResponse;
10import?org.elasticsearch.action.update.UpdateRequest;
11import?org.elasticsearch.action.update.UpdateResponse;
12import?org.elasticsearch.client.RequestOptions;
13import?org.elasticsearch.client.RestHighLevelClient;
14import?org.elasticsearch.common.xcontent.XContentType;
15import?org.springframework.beans.factory.annotation.Autowired;
16import?org.springframework.stereotype.Service;
17import?java.io.IOException;
18import?java.util.Date;
19
20@Slf4j
21@Service
22public?class?IndexService?{
23
24????@Autowired
25????private?RestHighLevelClient?restHighLevelClient;
26
27????/** 28?????*?增加文檔信息 29?????*/
30????public?void?addDocument()?{
31????????try?{
32????????????//?創建索引請求對象
33????????????IndexRequest?indexRequest?=?new?IndexRequest("mydlq-user",?"doc",?"1");
34????????????//?創建員工信息
35????????????UserInfo?userInfo?=?new?UserInfo();
36????????????userInfo.setName("張三");
37????????????userInfo.setAge(29);
38????????????userInfo.setSalary(100.00f);
39????????????userInfo.setAddress("北京市");
40????????????userInfo.setRemark("來自北京市的張先生");
41????????????userInfo.setCreateTime(new?Date());
42????????????userInfo.setBirthDate("1990-01-10");
43????????????//?將對象轉換為?byte?數組
44????????????byte[]?json?=?JSON.toJSONBytes(userInfo);
45????????????//?設置文檔內容
46????????????indexRequest.source(json,?XContentType.JSON);
47????????????//?執行增加文檔
48????????????IndexResponse?response?=?restHighLevelClient.index(indexRequest,?RequestOptions.DEFAULT);
49????????????log.info("創建狀態:{}",?response.status());
50????????}?catch?(Exception?e)?{
51????????????log.error("",?e);
52????????}
53????}
54
55????/** 56?????*?獲取文檔信息 57?????*/
58????public?void?getDocument()?{
59????????try?{
60????????????//?獲取請求對象
61????????????GetRequest?getRequest?=?new?GetRequest("mydlq-user",?"doc",?"1");
62????????????//?獲取文檔信息
63????????????GetResponse?getResponse?=?restHighLevelClient.get(getRequest,?RequestOptions.DEFAULT);
64????????????//?將?JSON?轉換成對象
65????????????if?(getResponse.isExists())?{
66????????????????UserInfo?userInfo?=?JSON.parseObject(getResponse.getSourceAsBytes(),?UserInfo.class);
67????????????????log.info("員工信息:{}",?userInfo);
68????????????}
69????????}?catch?(IOException?e)?{
70????????????log.error("",?e);
71????????}
72????}
73
74????/** 75?????*?更新文檔信息 76?????*/
77????public?void?updateDocument()?{
78????????try?{
79????????????//?創建索引請求對象
80????????????UpdateRequest?updateRequest?=?new?UpdateRequest("mydlq-user",?"doc",?"1");
81????????????//?設置員工更新信息
82????????????UserInfo?userInfo?=?new?UserInfo();
83????????????userInfo.setSalary(200.00f);
84????????????userInfo.setAddress("北京市海淀區");
85????????????//?將對象轉換為?byte?數組
86????????????byte[]?json?=?JSON.toJSONBytes(userInfo);
87????????????//?設置更新文檔內容
88????????????updateRequest.doc(json,?XContentType.JSON);
89????????????//?執行更新文檔
90????????????UpdateResponse?response?=?restHighLevelClient.update(updateRequest,?RequestOptions.DEFAULT);
91????????????log.info("創建狀態:{}",?response.status());
92????????}?catch?(Exception?e)?{
93????????????log.error("",?e);
94????????}
95????}
96
97????/** 98?????*?刪除文檔信息 99?????*/
100????public?void?deleteDocument()?{
101????????try?{
102????????????//?創建刪除請求對象
103????????????DeleteRequest?deleteRequest?=?new?DeleteRequest("mydlq-user",?"doc",?"1");
104????????????//?執行刪除文檔
105????????????DeleteResponse?response?=?restHighLevelClient.delete(deleteRequest,?RequestOptions.DEFAULT);
106????????????log.info("刪除狀態:{}",?response.status());
107????????}?catch?(IOException?e)?{
108????????????log.error("",?e);
109????????}
110????}
111
112}

六、插入初始化數據

執行查詢示例前,先往索引中插入一批數據:

1、單條插入

  • POST mydlq-user/_doc

1{"name":"零零","address":"北京市豐臺區","remark":"低層員工","age":29,"salary":3000,"birthDate":"1990-11-11","createTime":"2019-11-11T08:18:00.000Z"}

2、批量插入

  • POST _bulk

 1{"index":{"_index":"mydlq-user","_type":"doc"}}
2{"name":"劉一","address":"北京市豐臺區","remark":"低層員工","age":30,"salary":3000,"birthDate":"1989-11-11","createTime":"2019-03-15T08:18:00.000Z"}
3{"index":{"_index":"mydlq-user","_type":"doc"}}
4{"name":"陳二","address":"北京市昌平區","remark":"中層員工","age":27,"salary":7900,"birthDate":"1992-01-25","createTime":"2019-11-08T11:15:00.000Z"}
5{"index":{"_index":"mydlq-user","_type":"doc"}}
6{"name":"張三","address":"北京市房山區","remark":"中層員工","age":28,"salary":8800,"birthDate":"1991-10-05","createTime":"2019-07-22T13:22:00.000Z"}
7{"index":{"_index":"mydlq-user","_type":"doc"}}
8{"name":"李四","address":"北京市大興區","remark":"高層員工","age":26,"salary":9000,"birthDate":"1993-08-18","createTime":"2019-10-17T15:00:00.000Z"}
9{"index":{"_index":"mydlq-user","_type":"doc"}}
10{"name":"王五","address":"北京市密云區","remark":"低層員工","age":31,"salary":4800,"birthDate":"1988-07-20","createTime":"2019-05-29T09:00:00.000Z"}
11{"index":{"_index":"mydlq-user","_type":"doc"}}
12{"name":"趙六","address":"北京市通州區","remark":"中層員工","age":32,"salary":6500,"birthDate":"1987-06-02","createTime":"2019-12-10T18:00:00.000Z"}
13{"index":{"_index":"mydlq-user","_type":"doc"}}
14{"name":"孫七","address":"北京市朝陽區","remark":"中層員工","age":33,"salary":7000,"birthDate":"1986-04-15","createTime":"2019-06-06T13:00:00.000Z"}
15{"index":{"_index":"mydlq-user","_type":"doc"}}
16{"name":"周八","address":"北京市西城區","remark":"低層員工","age":32,"salary":5000,"birthDate":"1987-09-26","createTime":"2019-01-26T14:00:00.000Z"}
17{"index":{"_index":"mydlq-user","_type":"doc"}}
18{"name":"吳九","address":"北京市海淀區","remark":"高層員工","age":30,"salary":11000,"birthDate":"1989-11-25","createTime":"2019-09-07T13:34:00.000Z"}
19{"index":{"_index":"mydlq-user","_type":"doc"}}
20{"name":"鄭十","address":"北京市東城區","remark":"低層員工","age":29,"salary":5000,"birthDate":"1990-12-25","createTime":"2019-03-06T12:08:00.000Z"}
21{"index":{"_index":"mydlq-user","_type":"doc"}}
22{"name":"蕭十一","address":"北京市平谷區","remark":"低層員工","age":29,"salary":3300,"birthDate":"1990-11-11","createTime":"2019-03-10T08:17:00.000Z"}
23{"index":{"_index":"mydlq-user","_type":"doc"}}
24{"name":"曹十二","address":"北京市懷柔區","remark":"中層員工","age":27,"salary":6800,"birthDate":"1992-01-25","createTime":"2019-12-03T11:09:00.000Z"}
25{"index":{"_index":"mydlq-user","_type":"doc"}}
26{"name":"吳十三","address":"北京市延慶區","remark":"中層員工","age":25,"salary":7000,"birthDate":"1994-10-05","createTime":"2019-07-27T14:22:00.000Z"}
27{"index":{"_index":"mydlq-user","_type":"doc"}}
28{"name":"馮十四","address":"北京市密云區","remark":"低層員工","age":25,"salary":3000,"birthDate":"1994-08-18","createTime":"2019-04-22T15:00:00.000Z"}
29{"index":{"_index":"mydlq-user","_type":"doc"}}
30{"name":"蔣十五","address":"北京市通州區","remark":"低層員工","age":31,"salary":2800,"birthDate":"1988-07-20","createTime":"2019-06-13T10:00:00.000Z"}
31{"index":{"_index":"mydlq-user","_type":"doc"}}
32{"name":"苗十六","address":"北京市門頭溝區","remark":"高層員工","age":32,"salary":11500,"birthDate":"1987-06-02","createTime":"2019-11-11T18:00:00.000Z"}
33{"index":{"_index":"mydlq-user","_type":"doc"}}
34{"name":"魯十七","address":"北京市石景山區","remark":"高員工","age":33,"salary":9500,"birthDate":"1986-04-15","createTime":"2019-06-06T14:00:00.000Z"}
35{"index":{"_index":"mydlq-user","_type":"doc"}}
36{"name":"沈十八","address":"北京市朝陽區","remark":"中層員工","age":31,"salary":8300,"birthDate":"1988-09-26","createTime":"2019-09-25T14:00:00.000Z"}
37{"index":{"_index":"mydlq-user","_type":"doc"}}
38{"name":"呂十九","address":"北京市西城區","remark":"低層員工","age":31,"salary":4500,"birthDate":"1988-11-25","createTime":"2019-09-22T13:34:00.000Z"}
39{"index":{"_index":"mydlq-user","_type":"doc"}}
40{"name":"丁二十","address":"北京市東城區","remark":"低層員工","age":33,"salary":2100,"birthDate":"1986-12-25","createTime":"2019-03-07T12:08:00.000Z"}

3、查詢數據

插入完成后再查詢數據,查看之前插入的數據是否存在:

1GET?mydlq-user/_search

執行后得到下面記錄:

  1{
2??"took":?2,
3??"timed_out":?false,
4??"_shards":?{
5????"total":?1,
6????"successful":?1,
7????"skipped":?0,
8????"failed":?0
9??},
10??"hits":?{
11????"total":?20,
12????"max_score":?1,
13????"hits":?[
14??????{
15????????"_index":?"mydlq-user",
16????????"_type":?"_doc",
17????????"_id":?"BeN0BW8B7BNodGwRFTRj",
18????????"_score":?1,
19????????"_source":?{
20??????????"name":?"劉一",
21??????????"address":?"北京市豐臺區",
22??????????"remark":?"低層員工",
23??????????"age":?30,
24??????????"salary":?3000,
25??????????"birthDate":?"1989-11-11",
26??????????"createTime":?"2019-03-15T08:18:00.000Z"
27????????}
28??????},
29??????{
30????????"_index":?"mydlq-user",
31????????"_type":?"_doc",
32????????"_id":?"BuN0BW8B7BNodGwRFTRj",
33????????"_score":?1,
34????????"_source":?{
35??????????"name":?"陳二",
36??????????"address":?"北京市昌平區",
37??????????"remark":?"中層員工",
38??????????"age":?27,
39??????????"salary":?7900,
40??????????"birthDate":?"1992-01-25",
41??????????"createTime":?"2019-11-08T11:15:00.000Z"
42????????}
43??????},
44??????{
45????????"_index":?"mydlq-user",
46????????"_type":?"_doc",
47????????"_id":?"B-N0BW8B7BNodGwRFTRj",
48????????"_score":?1,
49????????"_source":?{
50??????????"name":?"張三",
51??????????"address":?"北京市房山區",
52??????????"remark":?"中層員工",
53??????????"age":?28,
54??????????"salary":?8800,
55??????????"birthDate":?"1991-10-05",
56??????????"createTime":?"2019-07-22T13:22:00.000Z"
57????????}
58??????},
59??????{
60????????"_index":?"mydlq-user",
61????????"_type":?"_doc",
62????????"_id":?"CON0BW8B7BNodGwRFTRj",
63????????"_score":?1,
64????????"_source":?{
65??????????"name":?"李四",
66??????????"address":?"北京市大興區",
67??????????"remark":?"高層員工",
68??????????"age":?26,
69??????????"salary":?9000,
70??????????"birthDate":?"1993-08-18",
71??????????"createTime":?"2019-10-17T15:00:00.000Z"
72????????}
73??????},
74??????{
75????????"_index":?"mydlq-user",
76????????"_type":?"_doc",
77????????"_id":?"CeN0BW8B7BNodGwRFTRj",
78????????"_score":?1,
79????????"_source":?{
80??????????"name":?"王五",
81??????????"address":?"北京市密云區",
82??????????"remark":?"低層員工",
83??????????"age":?31,
84??????????"salary":?4800,
85??????????"birthDate":?"1988-07-20",
86??????????"createTime":?"2019-05-29T09:00:00.000Z"
87????????}
88??????},
89??????{
90????????"_index":?"mydlq-user",
91????????"_type":?"_doc",
92????????"_id":?"CuN0BW8B7BNodGwRFTRj",
93????????"_score":?1,
94????????"_source":?{
95??????????"name":?"趙六",
96??????????"address":?"北京市通州區",
97??????????"remark":?"中層員工",
98??????????"age":?32,
99??????????"salary":?6500,
100??????????"birthDate":?"1987-06-02",
101??????????"createTime":?"2019-12-10T18:00:00.000Z"
102????????}
103??????},
104??????{
105????????"_index":?"mydlq-user",
106????????"_type":?"_doc",
107????????"_id":?"C-N0BW8B7BNodGwRFTRj",
108????????"_score":?1,
109????????"_source":?{
110??????????"name":?"孫七",
111??????????"address":?"北京市朝陽區",
112??????????"remark":?"中層員工",
113??????????"age":?33,
114??????????"salary":?7000,
115??????????"birthDate":?"1986-04-15",
116??????????"createTime":?"2019-06-06T13:00:00.000Z"
117????????}
118??????},
119??????{
120????????"_index":?"mydlq-user",
121????????"_type":?"_doc",
122????????"_id":?"DON0BW8B7BNodGwRFTRj",
123????????"_score":?1,
124????????"_source":?{
125??????????"name":?"周八",
126??????????"address":?"北京市西城區",
127??????????"remark":?"低層員工",
128??????????"age":?32,
129??????????"salary":?5000,
130??????????"birthDate":?"1987-09-26",
131??????????"createTime":?"2019-01-26T14:00:00.000Z"
132????????}
133??????},
134??????{
135????????"_index":?"mydlq-user",
136????????"_type":?"_doc",
137????????"_id":?"DeN0BW8B7BNodGwRFTRj",
138????????"_score":?1,
139????????"_source":?{
140??????????"name":?"吳九",
141??????????"address":?"北京市海淀區",
142??????????"remark":?"高層員工",
143??????????"age":?30,
144??????????"salary":?11000,
145??????????"birthDate":?"1989-11-25",
146??????????"createTime":?"2019-09-07T13:34:00.000Z"
147????????}
148??????},
149??????{
150????????"_index":?"mydlq-user",
151????????"_type":?"_doc",
152????????"_id":?"DuN0BW8B7BNodGwRFTRj",
153????????"_score":?1,
154????????"_source":?{
155??????????"name":?"鄭十",
156??????????"address":?"北京市東城區",
157??????????"remark":?"低層員工",
158??????????"age":?29,
159??????????"salary":?5000,
160??????????"birthDate":?"1990-12-25",
161??????????"createTime":?"2019-03-06T12:08:00.000Z"
162????????}
163??????}
164????]
165??}
166}

七、查詢操作示例

1、精確查詢(term)

(1)、Restful 操作示例

精確查詢

精確查詢,查詢地址為?北京市通州區?的人員信息:

查詢條件不會進行分詞,但是查詢內容可能會分詞,導致查詢不到。之前在創建索引時設置 Mapping 中 address 字段存在 keyword 字段是專門用于不分詞查詢的子字段。

 1GET?mydlq-user/_search
2{
3??"query":?{
4????"term":?{
5??????"address.keyword":?{
6????????"value":?"北京市通州區"
7??????}
8????}
9??}
10}

精確查詢-多內容查詢

精確查詢,查詢地址為?北京市豐臺區北京市昌平區?或?北京市大興區?的人員信息:

 1GET?mydlq-user/_search
2{
3??"query":?{
4????"terms":?{
5??????"address.keyword":?[
6????????"北京市豐臺區",
7????????"北京市昌平區",
8????????"北京市大興區"
9??????]
10????}
11??}
12}

(2)、Java 代碼示例

 1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.index.query.QueryBuilders;
9import?org.elasticsearch.rest.RestStatus;
10import?org.elasticsearch.search.SearchHit;
11import?org.elasticsearch.search.SearchHits;
12import?org.elasticsearch.search.builder.SearchSourceBuilder;
13import?org.springframework.beans.factory.annotation.Autowired;
14import?org.springframework.stereotype.Service;
15import?java.io.IOException;
16
17@Slf4j
18@Service
19public?class?TermQueryService?{
20
21????@Autowired
22????private?RestHighLevelClient?restHighLevelClient;
23
24????/**25?????*?精確查詢(查詢條件不會進行分詞,但是查詢內容可能會分詞,導致查詢不到)26?????*/
27????public?void?termQuery()?{
28????????try?{
29????????????//?構建查詢條件(注意:termQuery 支持多種格式查詢,如 boolean、int、double、string 等,這里使用的是 string 的查詢)
30????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
31????????????searchSourceBuilder.query(QueryBuilders.termQuery("address.keyword",?"北京市通州區"));
32????????????//?創建查詢請求對象,將查詢對象配置到其中
33????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
34????????????searchRequest.source(searchSourceBuilder);
35????????????//?執行查詢,然后處理響應結果
36????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
37????????????//?根據狀態和數據條數驗證是否返回了數據
38????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
39????????????????SearchHits?hits?=?searchResponse.getHits();
40????????????????for?(SearchHit?hit?:?hits)?{
41????????????????????//?將?JSON?轉換成對象
42????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
43????????????????????//?輸出查詢信息
44????????????????????log.info(userInfo.toString());
45????????????????}
46????????????}
47????????}?catch?(IOException?e)?{
48????????????log.error("",?e);
49????????}
50????}
51
52????/**53?????*?多個內容在一個字段中進行查詢54?????*/
55????public?void?termsQuery()?{
56????????try?{
57????????????//?構建查詢條件(注意:termsQuery 支持多種格式查詢,如 boolean、int、double、string 等,這里使用的是 string 的查詢)
58????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
59????????????searchSourceBuilder.query(QueryBuilders.termsQuery("address.keyword",?"北京市豐臺區",?"北京市昌平區",?"北京市大興區"));
60????????????//?創建查詢請求對象,將查詢對象配置到其中
61????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
62????????????searchRequest.source(searchSourceBuilder);
63????????????//?執行查詢,然后處理響應結果
64????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
65????????????//?根據狀態和數據條數驗證是否返回了數據
66????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
67????????????????SearchHits?hits?=?searchResponse.getHits();
68????????????????for?(SearchHit?hit?:?hits)?{
69????????????????????//?將?JSON?轉換成對象
70????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
71????????????????????//?輸出查詢信息
72????????????????????log.info(userInfo.toString());
73????????????????}
74????????????}
75????????}?catch?(IOException?e)?{
76????????????log.error("",?e);
77????????}
78????}
79
80}

2、匹配查詢(match)

(1)、Restful 操作示例

匹配查詢全部數據與分頁

匹配查詢符合條件的所有數據,并且設置以?salary?字段升序排序,并設置分頁:

 1GET?mydlq-user/_search
2{
3??"query":?{
4????"match_all":?{}
5??},
6??"from":?0,
7??"size":?10,
8??"sort":?[
9????{
10??????"salary":?{
11????????"order":?"asc"
12??????}
13????}
14??]
15}

匹配查詢數據

匹配查詢地址為?通州區?的數據:

1GET?mydlq-user/_search
2{
3??"query":?{
4????"match":?{
5??????"address":?"通州區"
6????}
7??}
8}

詞語匹配查詢

詞語匹配進行查詢,匹配?address?中為?北京市通州區?的員工信息:

1GET?mydlq-user/_search
2{
3??"query":?{
4????"match_phrase":?{
5??????"address":?"北京市通州區"
6????}
7??}
8}

內容多字段查詢

查詢在字段?addressremark?中存在?北京?內容的員工信息:

1GET?mydlq-user/_search
2{
3??"query":?{
4????"multi_match":?{
5??????"query":?"北京",
6??????"fields":?["address","remark"]
7????}
8??}
9}

(2)、Java 代碼示例

  1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.index.query.MatchAllQueryBuilder;
9import?org.elasticsearch.index.query.QueryBuilders;
10import?org.elasticsearch.rest.RestStatus;
11import?org.elasticsearch.search.SearchHit;
12import?org.elasticsearch.search.SearchHits;
13import?org.elasticsearch.search.builder.SearchSourceBuilder;
14import?org.elasticsearch.search.sort.SortOrder;
15import?org.springframework.beans.factory.annotation.Autowired;
16import?org.springframework.stereotype.Service;
17import?java.io.IOException;
18
19@Slf4j
20@Service
21public?class?MatchQueryService?{
22
23????@Autowired
24????private?RestHighLevelClient?restHighLevelClient;
25
26????/** 27?????*?匹配查詢符合條件的所有數據,并設置分頁 28?????*/
29????public?Object?matchAllQuery()?{
30????????try?{
31????????????//?構建查詢條件
32????????????MatchAllQueryBuilder?matchAllQueryBuilder?=?QueryBuilders.matchAllQuery();
33????????????//?創建查詢源構造器
34????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
35????????????searchSourceBuilder.query(matchAllQueryBuilder);
36????????????//?設置分頁
37????????????searchSourceBuilder.from(0);
38????????????searchSourceBuilder.size(3);
39????????????//?設置排序
40????????????searchSourceBuilder.sort("salary",?SortOrder.ASC);
41????????????//?創建查詢請求對象,將查詢對象配置到其中
42????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
43????????????searchRequest.source(searchSourceBuilder);
44????????????//?執行查詢,然后處理響應結果
45????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
46????????????//?根據狀態和數據條數驗證是否返回了數據
47????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
48????????????????SearchHits?hits?=?searchResponse.getHits();
49????????????????for?(SearchHit?hit?:?hits)?{
50????????????????????//?將?JSON?轉換成對象
51????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
52????????????????????//?輸出查詢信息
53????????????????????log.info(userInfo.toString());
54????????????????}
55????????????}
56????????}?catch?(IOException?e)?{
57????????????log.error("",?e);
58????????}
59????}
60
61????/** 62?????*?匹配查詢數據 63?????*/
64????public?Object?matchQuery()?{
65????????try?{
66????????????//?構建查詢條件
67????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
68????????????searchSourceBuilder.query(QueryBuilders.matchQuery("address",?"*通州區"));
69????????????//?創建查詢請求對象,將查詢對象配置到其中
70????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
71????????????searchRequest.source(searchSourceBuilder);
72????????????//?執行查詢,然后處理響應結果
73????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
74????????????//?根據狀態和數據條數驗證是否返回了數據
75????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
76????????????????SearchHits?hits?=?searchResponse.getHits();
77????????????????for?(SearchHit?hit?:?hits)?{
78????????????????????//?將?JSON?轉換成對象
79????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
80????????????????????//?輸出查詢信息
81????????????????????log.info(userInfo.toString());
82????????????????}
83????????????}
84????????}?catch?(IOException?e)?{
85????????????log.error("",?e);
86????????}
87????}
88
89????/** 90?????*?詞語匹配查詢 91?????*/
92????public?Object?matchPhraseQuery()?{
93????????try?{
94????????????//?構建查詢條件
95????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
96????????????searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("address",?"北京市通州區"));
97????????????//?創建查詢請求對象,將查詢對象配置到其中
98????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
99????????????searchRequest.source(searchSourceBuilder);
100????????????//?執行查詢,然后處理響應結果
101????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
102????????????//?根據狀態和數據條數驗證是否返回了數據
103????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
104????????????????SearchHits?hits?=?searchResponse.getHits();
105????????????????for?(SearchHit?hit?:?hits)?{
106????????????????????//?將?JSON?轉換成對象
107????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
108????????????????????//?輸出查詢信息
109????????????????????log.info(userInfo.toString());
110????????????????}
111????????????}
112????????}?catch?(IOException?e)?{
113????????????log.error("",?e);
114????????}
115????}
116
117????/**118?????*?內容在多字段中進行查詢119?????*/
120????public?Object?matchMultiQuery()?{
121????????try?{
122????????????//?構建查詢條件
123????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
124????????????searchSourceBuilder.query(QueryBuilders.multiMatchQuery("北京市",?"address",?"remark"));
125????????????//?創建查詢請求對象,將查詢對象配置到其中
126????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
127????????????searchRequest.source(searchSourceBuilder);
128????????????//?執行查詢,然后處理響應結果
129????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
130????????????//?根據狀態和數據條數驗證是否返回了數據
131????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
132????????????????SearchHits?hits?=?searchResponse.getHits();
133????????????????for?(SearchHit?hit?:?hits)?{
134????????????????????//?將?JSON?轉換成對象
135????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
136????????????????????//?輸出查詢信息
137????????????????????log.info(userInfo.toString());
138????????????????}
139????????????}
140????????}?catch?(IOException?e)?{
141????????????log.error("",?e);
142????????}
143????}
144
145}

3、模糊查詢(fuzzy)

(1)、Restful 操作示例

模糊查詢所有以??結尾的姓名

1GET?mydlq-user/_search
2{
3??"query":?{
4????"fuzzy":?{
5??????"name":?"三"
6????}
7??}
8}

(2)、Java 代碼示例

 1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.common.unit.Fuzziness;
9import?org.elasticsearch.index.query.QueryBuilders;
10import?org.elasticsearch.rest.RestStatus;
11import?org.elasticsearch.search.SearchHit;
12import?org.elasticsearch.search.SearchHits;
13import?org.elasticsearch.search.builder.SearchSourceBuilder;
14import?org.springframework.beans.factory.annotation.Autowired;
15import?org.springframework.stereotype.Service;
16import?java.io.IOException;
17
18@Slf4j
19@Service
20public?class?FuzzyQueryService?{
21
22????@Autowired
23????private?RestHighLevelClient?restHighLevelClient;
24
25????/**26?????*?模糊查詢所有以?“三”?結尾的姓名27?????*/
28????public?Object?fuzzyQuery()?{
29????????try?{
30????????????//?構建查詢條件
31????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
32????????????searchSourceBuilder.query(QueryBuilders.fuzzyQuery("name",?"三").fuzziness(Fuzziness.AUTO));
33????????????//?創建查詢請求對象,將查詢對象配置到其中
34????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
35????????????searchRequest.source(searchSourceBuilder);
36????????????//?執行查詢,然后處理響應結果
37????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
38????????????//?根據狀態和數據條數驗證是否返回了數據
39????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
40????????????????SearchHits?hits?=?searchResponse.getHits();
41????????????????for?(SearchHit?hit?:?hits)?{
42????????????????????//?將?JSON?轉換成對象
43????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
44????????????????????//?輸出查詢信息
45????????????????????log.info(userInfo.toString());
46????????????????}
47????????????}
48????????}?catch?(IOException?e)?{
49????????????log.error("",?e);
50????????}
51????}
52
53}

4、范圍查詢(range)

(1)、Restful 操作示例

查詢歲數 ≥ 30 歲的員工數據:

 1GET?/mydlq-user/_search
2{
3??"query":?{
4????"range":?{
5??????"age":?{
6????????"gte":?30
7??????}
8????}
9??}
10}

查詢生日距離現在 30 年間的員工數據:

 1GET?mydlq-user/_search
2{
3??"query":?{
4????"range":?{
5??????"birthDate":?{
6????????"gte":?"now-30y"
7??????}
8????}
9??}
10}

(2)、Java 代碼示例

 1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.index.query.QueryBuilders;
9import?org.elasticsearch.rest.RestStatus;
10import?org.elasticsearch.search.SearchHit;
11import?org.elasticsearch.search.SearchHits;
12import?org.elasticsearch.search.builder.SearchSourceBuilder;
13import?org.springframework.beans.factory.annotation.Autowired;
14import?org.springframework.stereotype.Service;
15import?java.io.IOException;
16
17@Slf4j
18@Service
19public?class?RangeQueryService?{
20
21????@Autowired
22????private?RestHighLevelClient?restHighLevelClient;
23
24????/**25?????*?查詢歲數?≥?30?歲的員工數據26?????*/
27????public?void?rangeQuery()?{
28????????try?{
29????????????//?構建查詢條件
30????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
31????????????searchSourceBuilder.query(QueryBuilders.rangeQuery("age").gte(30));
32????????????//?創建查詢請求對象,將查詢對象配置到其中
33????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
34????????????searchRequest.source(searchSourceBuilder);
35????????????//?執行查詢,然后處理響應結果
36????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
37????????????//?根據狀態和數據條數驗證是否返回了數據
38????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
39????????????????SearchHits?hits?=?searchResponse.getHits();
40????????????????for?(SearchHit?hit?:?hits)?{
41????????????????????//?將?JSON?轉換成對象
42????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
43????????????????????//?輸出查詢信息
44????????????????????log.info(userInfo.toString());
45????????????????}
46????????????}
47????????}?catch?(IOException?e)?{
48????????????log.error("",?e);
49????????}
50????}
51
52????/**53?????*?查詢距離現在?30?年間的員工數據54?????*?[年(y)、月(M)、星期(w)、天(d)、小時(h)、分鐘(m)、秒(s)]55?????*?例如:56?????*?now-1h?查詢一小時內范圍57?????*?now-1d?查詢一天內時間范圍58?????*?now-1y?查詢最近一年內的時間范圍59?????*/
60????public?void?dateRangeQuery()?{
61????????try?{
62????????????//?構建查詢條件
63????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
64????????????//?includeLower(是否包含下邊界)、includeUpper(是否包含上邊界)
65????????????searchSourceBuilder.query(QueryBuilders.rangeQuery("birthDate")
66????????????????????.gte("now-30y").includeLower(true).includeUpper(true));
67????????????//?創建查詢請求對象,將查詢對象配置到其中
68????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
69????????????searchRequest.source(searchSourceBuilder);
70????????????//?執行查詢,然后處理響應結果
71????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
72????????????//?根據狀態和數據條數驗證是否返回了數據
73????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
74????????????????SearchHits?hits?=?searchResponse.getHits();
75????????????????for?(SearchHit?hit?:?hits)?{
76????????????????????//?將?JSON?轉換成對象
77????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
78????????????????????//?輸出查詢信息
79????????????????????log.info(userInfo.toString());
80????????????????}
81????????????}
82????????}?catch?(IOException?e)?{
83????????????log.error("",?e);
84????????}
85????}
86
87}

5、通配符查詢(wildcard)

(1)、Restful 操作示例

查詢所有以 “三” 結尾的姓名:

 1GET?mydlq-user/_search
2{
3??"query":?{
4????"wildcard":?{
5??????"name.keyword":?{
6????????"value":?"*三"
7??????}
8????}
9??}
10}

(2)、Java 代碼示例

 1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.index.query.QueryBuilders;
9import?org.elasticsearch.rest.RestStatus;
10import?org.elasticsearch.search.SearchHit;
11import?org.elasticsearch.search.SearchHits;
12import?org.elasticsearch.search.builder.SearchSourceBuilder;
13import?org.springframework.beans.factory.annotation.Autowired;
14import?org.springframework.stereotype.Service;
15import?java.io.IOException;
16
17@Slf4j
18@Service
19public?class?WildcardQueryService?{
20
21????@Autowired
22????private?RestHighLevelClient?restHighLevelClient;
23
24????/**25?????*?查詢所有以?“三”?結尾的姓名26?????*27?????*?*:表示多個字符(0個或多個字符)28?????*??:表示單個字符29?????*/
30????public?Object?wildcardQuery()?{
31????????try?{
32????????????//?構建查詢條件
33????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
34????????????searchSourceBuilder.query(QueryBuilders.wildcardQuery("name.keyword",?"*三"));
35????????????//?創建查詢請求對象,將查詢對象配置到其中
36????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
37????????????searchRequest.source(searchSourceBuilder);
38????????????//?執行查詢,然后處理響應結果
39????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
40????????????//?根據狀態和數據條數驗證是否返回了數據
41????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
42????????????????SearchHits?hits?=?searchResponse.getHits();
43????????????????for?(SearchHit?hit?:?hits)?{
44????????????????????//?將?JSON?轉換成對象
45????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
46????????????????????//?輸出查詢信息
47????????????????????log.info(userInfo.toString());
48????????????????}
49????????????}
50????????}?catch?(IOException?e)?{
51????????????log.error("",?e);
52????????}
53????}
54
55}

6、布爾查詢(bool)

(1)、Restful 操作示例

查詢出生在 1990-1995 年期間,且地址在?北京市昌平區北京市大興區北京市房山區?的員工信息:

 1GET?/mydlq-user/_search
2{
3??"query":?{
4????"bool":?{
5??????"filter":?{
6????????"range":?{
7??????????"birthDate":?{
8????????????"format":?"yyyy",?
9????????????"gte":?1990,
10????????????"lte":?1995
11??????????}
12????????}
13??????},
14??????"must":?[
15????????{
16??????????"terms":?{
17????????????"address.keyword":?[
18??????????????"北京市昌平區",
19??????????????"北京市大興區",
20??????????????"北京市房山區"
21????????????]
22??????????}
23????????}
24??????]
25????}
26??}
27}

(2)、Java 代碼示例

 1import?club.mydlq.elasticsearch.model.entity.UserInfo;
2import?com.alibaba.fastjson.JSON;
3import?lombok.extern.slf4j.Slf4j;
4import?org.elasticsearch.action.search.SearchRequest;
5import?org.elasticsearch.action.search.SearchResponse;
6import?org.elasticsearch.client.RequestOptions;
7import?org.elasticsearch.client.RestHighLevelClient;
8import?org.elasticsearch.index.query.BoolQueryBuilder;
9import?org.elasticsearch.index.query.QueryBuilders;
10import?org.elasticsearch.rest.RestStatus;
11import?org.elasticsearch.search.SearchHit;
12import?org.elasticsearch.search.SearchHits;
13import?org.elasticsearch.search.builder.SearchSourceBuilder;
14import?org.springframework.beans.factory.annotation.Autowired;
15import?org.springframework.stereotype.Service;
16import?java.io.IOException;
17
18@Slf4j
19@Service
20public?class?BoolQueryService?{
21
22????@Autowired
23????private?RestHighLevelClient?restHighLevelClient;
24
25????public?Object?boolQuery()?{
26????????try?{
27????????????//?創建?Bool?查詢構建器
28????????????BoolQueryBuilder?boolQueryBuilder?=?QueryBuilders.boolQuery();
29????????????//?構建查詢條件
30????????????boolQueryBuilder.must(QueryBuilders.termsQuery("address.keyword",?"北京市昌平區",?"北京市大興區",?"北京市房山區"))
31????????????????????.filter().add(QueryBuilders.rangeQuery("birthDate").format("yyyy").gte("1990").lte("1995"));
32????????????//?構建查詢源構建器
33????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
34????????????searchSourceBuilder.query(boolQueryBuilder);
35????????????//?創建查詢請求對象,將查詢對象配置到其中
36????????????SearchRequest?searchRequest?=?new?SearchRequest("mydlq-user");
37????????????searchRequest.source(searchSourceBuilder);
38????????????//?執行查詢,然后處理響應結果
39????????????SearchResponse?searchResponse?=?restHighLevelClient.search(searchRequest,?RequestOptions.DEFAULT);
40????????????//?根據狀態和數據條數驗證是否返回了數據
41????????????if?(RestStatus.OK.equals(searchResponse.status())?&&?searchResponse.getHits().totalHits?>?0)?{
42????????????????SearchHits?hits?=?searchResponse.getHits();
43????????????????for?(SearchHit?hit?:?hits)?{
44????????????????????//?將?JSON?轉換成對象
45????????????????????UserInfo?userInfo?=?JSON.parseObject(hit.getSourceAsString(),?UserInfo.class);
46????????????????????//?輸出查詢信息
47????????????????????log.info(userInfo.toString());
48????????????????}
49????????????}
50????????}catch?(IOException?e){
51????????????log.error("",e);
52????????}
53????}
54
55}

八、聚合查詢操作示例

1、Metric 聚合分析

(1)、Restful 操作示例

統計員工總數、工資最高值、工資最低值、工資平均工資、工資總和:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_stats":?{
6??????"stats":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工工資最低值:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_min":?{
6??????"min":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工工資最高值:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_max":?{
6??????"max":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工工資平均值:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_avg":?{
6??????"avg":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工工資總值:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_sum":?{
6??????"sum":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工總數:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"employee_count":?{
6??????"value_count":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

統計員工工資百分位:

 1GET?/mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_percentiles":?{
6??????"percentiles":?{
7????????"field":?"salary"
8??????}
9????}
10??}
11}

(2)、Java 代碼示例

  1import?lombok.extern.slf4j.Slf4j;
2import?org.elasticsearch.action.search.SearchRequest;
3import?org.elasticsearch.action.search.SearchResponse;
4import?org.elasticsearch.client.RequestOptions;
5import?org.elasticsearch.client.RestHighLevelClient;
6import?org.elasticsearch.rest.RestStatus;
7import?org.elasticsearch.search.aggregations.AggregationBuilder;
8import?org.elasticsearch.search.aggregations.AggregationBuilders;
9import?org.elasticsearch.search.aggregations.Aggregations;
10import?org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
11import?org.elasticsearch.search.aggregations.metrics.max.ParsedMax;
12import?org.elasticsearch.search.aggregations.metrics.min.ParsedMin;
13import?org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
14import?org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
15import?org.elasticsearch.search.aggregations.metrics.stats.ParsedStats;
16import?org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
17import?org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
18import?org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
19import?org.elasticsearch.search.builder.SearchSourceBuilder;
20import?org.springframework.beans.factory.annotation.Autowired;
21import?org.springframework.stereotype.Service;
22import?java.io.IOException;
23
24@Slf4j
25@Service
26public?class?AggrMetricService?{
27
28????@Autowired
29????private?RestHighLevelClient?restHighLevelClient;
30
31????/** 32?????*?stats?統計員工總數、員工工資最高值、員工工資最低值、員工平均工資、員工工資總和 33?????*/
34????public?Object?aggregationStats()?{
35????????String?responseResult?=?"";
36????????try?{
37????????????//?設置聚合條件
38????????????AggregationBuilder?aggr?=?AggregationBuilders.stats("salary_stats").field("salary");
39????????????//?查詢源構建器
40????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
41????????????searchSourceBuilder.aggregation(aggr);
42????????????//?設置查詢結果不返回,只返回聚合結果
43????????????searchSourceBuilder.size(0);
44????????????//?創建查詢請求對象,將查詢條件配置到其中
45????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
46????????????request.source(searchSourceBuilder);
47????????????//?執行請求
48????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
49????????????//?獲取響應中的聚合信息
50????????????Aggregations?aggregations?=?response.getAggregations();
51????????????//?輸出內容
52????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
53????????????????//?轉換為?Stats?對象
54????????????????ParsedStats?aggregation?=?aggregations.get("salary_stats");
55????????????????log.info("-------------------------------------------");
56????????????????log.info("聚合信息:");
57????????????????log.info("count:{}",?aggregation.getCount());
58????????????????log.info("avg:{}",?aggregation.getAvg());
59????????????????log.info("max:{}",?aggregation.getMax());
60????????????????log.info("min:{}",?aggregation.getMin());
61????????????????log.info("sum:{}",?aggregation.getSum());
62????????????????log.info("-------------------------------------------");
63????????????}
64????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
65????????????responseResult?=?response.toString();
66????????}?catch?(IOException?e)?{
67????????????log.error("",?e);
68????????}
69????????return?responseResult;
70????}
71
72????/** 73?????*?min?統計員工工資最低值 74?????*/
75????public?Object?aggregationMin()?{
76????????String?responseResult?=?"";
77????????try?{
78????????????//?設置聚合條件
79????????????AggregationBuilder?aggr?=?AggregationBuilders.min("salary_min").field("salary");
80????????????//?查詢源構建器
81????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
82????????????searchSourceBuilder.aggregation(aggr);
83????????????searchSourceBuilder.size(0);
84????????????//?創建查詢請求對象,將查詢條件配置到其中
85????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
86????????????request.source(searchSourceBuilder);
87????????????//?執行請求
88????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
89????????????//?獲取響應中的聚合信息
90????????????Aggregations?aggregations?=?response.getAggregations();
91????????????//?輸出內容
92????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
93????????????????//?轉換為?Min?對象
94????????????????ParsedMin?aggregation?=?aggregations.get("salary_min");
95????????????????log.info("-------------------------------------------");
96????????????????log.info("聚合信息:");
97????????????????log.info("min:{}",?aggregation.getValue());
98????????????????log.info("-------------------------------------------");
99????????????}
100????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
101????????????responseResult?=?response.toString();
102????????}?catch?(IOException?e)?{
103????????????log.error("",?e);
104????????}
105????????return?responseResult;
106????}
107
108????/**109?????*?max?統計員工工資最高值110?????*/
111????public?Object?aggregationMax()?{
112????????String?responseResult?=?"";
113????????try?{
114????????????//?設置聚合條件
115????????????AggregationBuilder?aggr?=?AggregationBuilders.max("salary_max").field("salary");
116????????????//?查詢源構建器
117????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
118????????????searchSourceBuilder.aggregation(aggr);
119????????????searchSourceBuilder.size(0);
120????????????//?創建查詢請求對象,將查詢條件配置到其中
121????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
122????????????request.source(searchSourceBuilder);
123????????????//?執行請求
124????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
125????????????//?獲取響應中的聚合信息
126????????????Aggregations?aggregations?=?response.getAggregations();
127????????????//?輸出內容
128????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
129????????????????//?轉換為?Max?對象
130????????????????ParsedMax?aggregation?=?aggregations.get("salary_max");
131????????????????log.info("-------------------------------------------");
132????????????????log.info("聚合信息:");
133????????????????log.info("max:{}",?aggregation.getValue());
134????????????????log.info("-------------------------------------------");
135????????????}
136????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
137????????????responseResult?=?response.toString();
138????????}?catch?(IOException?e)?{
139????????????log.error("",?e);
140????????}
141????????return?responseResult;
142????}
143
144????/**145?????*?avg?統計員工工資平均值146?????*/
147????public?Object?aggregationAvg()?{
148????????String?responseResult?=?"";
149????????try?{
150????????????//?設置聚合條件
151????????????AggregationBuilder?aggr?=?AggregationBuilders.avg("salary_avg").field("salary");
152????????????//?查詢源構建器
153????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
154????????????searchSourceBuilder.aggregation(aggr);
155????????????searchSourceBuilder.size(0);
156????????????//?創建查詢請求對象,將查詢條件配置到其中
157????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
158????????????request.source(searchSourceBuilder);
159????????????//?執行請求
160????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
161????????????//?獲取響應中的聚合信息
162????????????Aggregations?aggregations?=?response.getAggregations();
163????????????//?輸出內容
164????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
165????????????????//?轉換為?Avg?對象
166????????????????ParsedAvg?aggregation?=?aggregations.get("salary_avg");
167????????????????log.info("-------------------------------------------");
168????????????????log.info("聚合信息:");
169????????????????log.info("avg:{}",?aggregation.getValue());
170????????????????log.info("-------------------------------------------");
171????????????}
172????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
173????????????responseResult?=?response.toString();
174????????}?catch?(IOException?e)?{
175????????????log.error("",?e);
176????????}
177????????return?responseResult;
178????}
179
180????/**181?????*?sum?統計員工工資總值182?????*/
183????public?Object?aggregationSum()?{
184????????String?responseResult?=?"";
185????????try?{
186????????????//?設置聚合條件
187????????????SumAggregationBuilder?aggr?=?AggregationBuilders.sum("salary_sum").field("salary");
188????????????//?查詢源構建器
189????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
190????????????searchSourceBuilder.aggregation(aggr);
191????????????searchSourceBuilder.size(0);
192????????????//?創建查詢請求對象,將查詢條件配置到其中
193????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
194????????????request.source(searchSourceBuilder);
195????????????//?執行請求
196????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
197????????????//?獲取響應中的聚合信息
198????????????Aggregations?aggregations?=?response.getAggregations();
199????????????//?輸出內容
200????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
201????????????????//?轉換為?Sum?對象
202????????????????ParsedSum?aggregation?=?aggregations.get("salary_sum");
203????????????????log.info("-------------------------------------------");
204????????????????log.info("聚合信息:");
205????????????????log.info("sum:{}",?String.valueOf((aggregation.getValue())));
206????????????????log.info("-------------------------------------------");
207????????????}
208????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
209????????????responseResult?=?response.toString();
210????????}?catch?(IOException?e)?{
211????????????log.error("",?e);
212????????}
213????????return?responseResult;
214????}
215
216????/**217?????*?count?統計員工總數218?????*/
219????public?Object?aggregationCount()?{
220????????String?responseResult?=?"";
221????????try?{
222????????????//?設置聚合條件
223????????????AggregationBuilder?aggr?=?AggregationBuilders.count("employee_count").field("salary");
224????????????//?查詢源構建器
225????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
226????????????searchSourceBuilder.aggregation(aggr);
227????????????searchSourceBuilder.size(0);
228????????????//?創建查詢請求對象,將查詢條件配置到其中
229????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
230????????????request.source(searchSourceBuilder);
231????????????//?執行請求
232????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
233????????????//?獲取響應中的聚合信息
234????????????Aggregations?aggregations?=?response.getAggregations();
235????????????//?輸出內容
236????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
237????????????????//?轉換為?ValueCount?對象
238????????????????ParsedValueCount?aggregation?=?aggregations.get("employee_count");
239????????????????log.info("-------------------------------------------");
240????????????????log.info("聚合信息:");
241????????????????log.info("count:{}",?aggregation.getValue());
242????????????????log.info("-------------------------------------------");
243????????????}
244????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
245????????????responseResult?=?response.toString();
246????????}?catch?(IOException?e)?{
247????????????log.error("",?e);
248????????}
249????????return?responseResult;
250????}
251
252????/**253?????*?percentiles?統計員工工資百分位254?????*/
255????public?Object?aggregationPercentiles()?{
256????????String?responseResult?=?"";
257????????try?{
258????????????//?設置聚合條件
259????????????AggregationBuilder?aggr?=?AggregationBuilders.percentiles("salary_percentiles").field("salary");
260????????????//?查詢源構建器
261????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
262????????????searchSourceBuilder.aggregation(aggr);
263????????????searchSourceBuilder.size(0);
264????????????//?創建查詢請求對象,將查詢條件配置到其中
265????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
266????????????request.source(searchSourceBuilder);
267????????????//?執行請求
268????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
269????????????//?獲取響應中的聚合信息
270????????????Aggregations?aggregations?=?response.getAggregations();
271????????????//?輸出內容
272????????????if?(RestStatus.OK.equals(response.status())?||?aggregations?!=?null)?{
273????????????????//?轉換為?Percentiles?對象
274????????????????ParsedPercentiles?aggregation?=?aggregations.get("salary_percentiles");
275????????????????log.info("-------------------------------------------");
276????????????????log.info("聚合信息:");
277????????????????for?(Percentile?percentile?:?aggregation)?{
278????????????????????log.info("百分位:{}:{}",?percentile.getPercent(),?percentile.getValue());
279????????????????}
280????????????????log.info("-------------------------------------------");
281????????????}
282????????????//?根據具體業務邏輯返回不同結果,這里為了方便直接將返回響應對象Json串
283????????????responseResult?=?response.toString();
284????????}?catch?(IOException?e)?{
285????????????log.error("",?e);
286????????}
287????????return?responseResult;
288????}
289
290}

2、Bucket 聚合分析

(1)、Restful 操作示例

按歲數進行聚合分桶,統計各個歲數員工的人數:

 1GET?mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"age_bucket":?{
6??????"terms":?{
7????????"field":?"age",
8????????"size":?"10"
9??????}
10????}
11??}
12}

按工資范圍進行聚合分桶,統計工資在 3000-5000、5000-9000 和 9000 以上的員工信息:

 1GET?mydlq-user/_search
2{
3??"aggs":?{
4????"salary_range_bucket":?{
5??????"range":?{
6????????"field":?"salary",
7????????"ranges":?[
8??????????{
9????????????"key":?"低級員工",?
10????????????"to":?3000
11??????????},{
12????????????"key":?"中級員工",
13????????????"from":?5000,
14????????????"to":?9000
15??????????},{
16????????????"key":?"高級員工",
17????????????"from":?9000
18??????????}
19????????]
20??????}
21????}
22??}
23}

按照時間范圍進行分桶,統計 1985-1990 年和 1990-1995 年出生的員工信息:

 1GET?mydlq-user/_search
2{
3??"size":?10,
4??"aggs":?{
5????"date_range_bucket":?{
6??????"date_range":?{
7????????"field":?"birthDate",
8????????"format":?"yyyy",?
9????????"ranges":?[
10??????????{
11????????????"key":?"出生日期1985-1990的員工",?
12????????????"from":?"1985",
13????????????"to":?"1990"
14??????????},{
15????????????"key":?"出生日期1990-1995的員工",?
16????????????"from":?"1990",
17????????????"to":?"1995"
18??????????}
19????????]
20??????}
21????}
22??}
23}

按工資多少進行聚合分桶,設置統計的最小值為 0,最大值為 12000,區段間隔為 3000:

 1GET?mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_histogram":?{
6??????"histogram":?{
7????????"field":?"salary",
8????????"extended_bounds":?{
9??????????"min":?0,
10??????????"max":?12000
11????????},?
12????????"interval":?3000
13??????}
14????}
15??}
16}

按出生日期進行分桶:

 1GET?mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"birthday_histogram":?{
6??????"date_histogram":?{
7????????"format":?"yyyy",?
8????????"field":?"birthDate",
9????????"interval":?"year"
10??????}
11????}
12??}
13}

(2)、Java 代碼示例

  1import?lombok.extern.slf4j.Slf4j;
2import?org.elasticsearch.action.search.SearchRequest;
3import?org.elasticsearch.action.search.SearchResponse;
4import?org.elasticsearch.client.RequestOptions;
5import?org.elasticsearch.client.RestHighLevelClient;
6import?org.elasticsearch.rest.RestStatus;
7import?org.elasticsearch.search.aggregations.AggregationBuilder;
8import?org.elasticsearch.search.aggregations.AggregationBuilders;
9import?org.elasticsearch.search.aggregations.Aggregations;
10import?org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
11import?org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
12import?org.elasticsearch.search.aggregations.bucket.range.Range;
13import?org.elasticsearch.search.aggregations.bucket.terms.Terms;
14import?org.elasticsearch.search.builder.SearchSourceBuilder;
15import?org.springframework.beans.factory.annotation.Autowired;
16import?org.springframework.stereotype.Service;
17import?java.io.IOException;
18import?java.util.List;
19
20@Slf4j
21@Service
22public?class?AggrBucketService?{
23
24????@Autowired
25????private?RestHighLevelClient?restHighLevelClient;
26
27????/** 28?????*?按歲數進行聚合分桶 29?????*/
30????public?Object?aggrBucketTerms()?{
31????????try?{
32????????????AggregationBuilder?aggr?=?AggregationBuilders.terms("age_bucket").field("age");
33????????????//?查詢源構建器
34????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
35????????????searchSourceBuilder.size(10);
36????????????searchSourceBuilder.aggregation(aggr);
37????????????//?創建查詢請求對象,將查詢條件配置到其中
38????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
39????????????request.source(searchSourceBuilder);
40????????????//?執行請求
41????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
42????????????//?獲取響應中的聚合信息
43????????????Aggregations?aggregations?=?response.getAggregations();
44????????????//?輸出內容
45????????????if?(RestStatus.OK.equals(response.status()))?{
46????????????????//?分桶
47????????????????Terms?byCompanyAggregation?=?aggregations.get("age_bucket");
48????????????????List?extends?Terms.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
49????????????????//?輸出各個桶的內容
50????????????????log.info("-------------------------------------------");
51????????????????log.info("聚合信息:");
52????????????????for?(Terms.Bucket?bucket?:?buckets)?{
53????????????????????log.info("桶名:{}?|?總數:{}",?bucket.getKeyAsString(),?bucket.getDocCount());
54????????????????}
55????????????????log.info("-------------------------------------------");
56????????????}
57????????}?catch?(IOException?e)?{
58????????????log.error("",?e);
59????????}
60????}
61
62????/** 63?????*?按工資范圍進行聚合分桶 64?????*/
65????public?Object?aggrBucketRange()?{
66????????try?{
67????????????AggregationBuilder?aggr?=?AggregationBuilders.range("salary_range_bucket")
68????????????????????.field("salary")
69????????????????????.addUnboundedTo("低級員工",?3000)
70????????????????????.addRange("中級員工",?5000,?9000)
71????????????????????.addUnboundedFrom("高級員工",?9000);
72????????????//?查詢源構建器
73????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
74????????????searchSourceBuilder.size(0);
75????????????searchSourceBuilder.aggregation(aggr);
76????????????//?創建查詢請求對象,將查詢條件配置到其中
77????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
78????????????request.source(searchSourceBuilder);
79????????????//?執行請求
80????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
81????????????//?獲取響應中的聚合信息
82????????????Aggregations?aggregations?=?response.getAggregations();
83????????????//?輸出內容
84????????????if?(RestStatus.OK.equals(response.status()))?{
85????????????????//?分桶
86????????????????Range?byCompanyAggregation?=?aggregations.get("salary_range_bucket");
87????????????????List?extends?Range.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
88????????????????//?輸出各個桶的內容
89????????????????log.info("-------------------------------------------");
90????????????????log.info("聚合信息:");
91????????????????for?(Range.Bucket?bucket?:?buckets)?{
92????????????????????log.info("桶名:{}?|?總數:{}",?bucket.getKeyAsString(),?bucket.getDocCount());
93????????????????}
94????????????????log.info("-------------------------------------------");
95????????????}
96????????}?catch?(IOException?e)?{
97????????????log.error("",?e);
98????????}
99????}
100
101????/**102?????*?按照時間范圍進行分桶103?????*/
104????public?Object?aggrBucketDateRange()?{
105????????try?{
106????????????AggregationBuilder?aggr?=?AggregationBuilders.dateRange("date_range_bucket")
107????????????????????.field("birthDate")
108????????????????????.format("yyyy")
109????????????????????.addRange("1985-1990",?"1985",?"1990")
110????????????????????.addRange("1990-1995",?"1990",?"1995");
111????????????//?查詢源構建器
112????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
113????????????searchSourceBuilder.size(0);
114????????????searchSourceBuilder.aggregation(aggr);
115????????????//?創建查詢請求對象,將查詢條件配置到其中
116????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
117????????????request.source(searchSourceBuilder);
118????????????//?執行請求
119????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
120????????????//?獲取響應中的聚合信息
121????????????Aggregations?aggregations?=?response.getAggregations();
122????????????//?輸出內容
123????????????if?(RestStatus.OK.equals(response.status()))?{
124????????????????//?分桶
125????????????????Range?byCompanyAggregation?=?aggregations.get("date_range_bucket");
126????????????????List?extends?Range.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
127????????????????//?輸出各個桶的內容
128????????????????log.info("-------------------------------------------");
129????????????????log.info("聚合信息:");
130????????????????for?(Range.Bucket?bucket?:?buckets)?{
131????????????????????log.info("桶名:{}?|?總數:{}",?bucket.getKeyAsString(),?bucket.getDocCount());
132????????????????}
133????????????????log.info("-------------------------------------------");
134????????????}
135????????}?catch?(IOException?e)?{
136????????????log.error("",?e);
137????????}
138????}
139
140????/**141?????*?按工資多少進行聚合分桶142?????*/
143????public?Object?aggrBucketHistogram()?{
144????????try?{
145????????????AggregationBuilder?aggr?=?AggregationBuilders.histogram("salary_histogram")
146????????????????????.field("salary")
147????????????????????.extendedBounds(0,?12000)
148????????????????????.interval(3000);
149????????????//?查詢源構建器
150????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
151????????????searchSourceBuilder.size(0);
152????????????searchSourceBuilder.aggregation(aggr);
153????????????//?創建查詢請求對象,將查詢條件配置到其中
154????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
155????????????request.source(searchSourceBuilder);
156????????????//?執行請求
157????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
158????????????//?獲取響應中的聚合信息
159????????????Aggregations?aggregations?=?response.getAggregations();
160????????????//?輸出內容
161????????????if?(RestStatus.OK.equals(response.status()))?{
162????????????????//?分桶
163????????????????Histogram?byCompanyAggregation?=?aggregations.get("salary_histogram");
164????????????????List?extends?Histogram.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
165????????????????//?輸出各個桶的內容
166????????????????log.info("-------------------------------------------");
167????????????????log.info("聚合信息:");
168????????????????for?(Histogram.Bucket?bucket?:?buckets)?{
169????????????????????log.info("桶名:{}?|?總數:{}",?bucket.getKeyAsString(),?bucket.getDocCount());
170????????????????}
171????????????????log.info("-------------------------------------------");
172????????????}
173????????}?catch?(IOException?e)?{
174????????????log.error("",?e);
175????????}
176????}
177
178????/**179?????*?按出生日期進行分桶180?????*/
181????public?Object?aggrBucketDateHistogram()?{
182????????try?{
183????????????AggregationBuilder?aggr?=?AggregationBuilders.dateHistogram("birthday_histogram")
184????????????????????.field("birthDate")
185????????????????????.interval(1)
186????????????????????.dateHistogramInterval(DateHistogramInterval.YEAR)
187????????????????????.format("yyyy");
188????????????//?查詢源構建器
189????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
190????????????searchSourceBuilder.size(0);
191????????????searchSourceBuilder.aggregation(aggr);
192????????????//?創建查詢請求對象,將查詢條件配置到其中
193????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
194????????????request.source(searchSourceBuilder);
195????????????//?執行請求
196????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
197????????????//?獲取響應中的聚合信息
198????????????Aggregations?aggregations?=?response.getAggregations();
199????????????//?輸出內容
200????????????if?(RestStatus.OK.equals(response.status()))?{
201????????????????//?分桶
202????????????????Histogram?byCompanyAggregation?=?aggregations.get("birthday_histogram");
203
204????????????????List?extends?Histogram.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
205????????????????//?輸出各個桶的內容
206????????????????log.info("-------------------------------------------");
207????????????????log.info("聚合信息:");
208????????????????for?(Histogram.Bucket?bucket?:?buckets)?{
209????????????????????log.info("桶名:{}?|?總數:{}",?bucket.getKeyAsString(),?bucket.getDocCount());
210????????????????}
211????????????????log.info("-------------------------------------------");
212????????????}
213????????}?catch?(IOException?e)?{
214????????????log.error("",?e);
215????????}
216????}
217
218}

3、Metric 與 Bucket 聚合分析

(1)、Restful 操作示例

按照員工歲數分桶、然后統計每個歲數員工工資最高值:

 1GET?mydlq-user/_search
2{
3??"size":?0,
4??"aggs":?{
5????"salary_bucket":?{
6??????"terms":?{
7????????"field":?"age",
8????????"size":?"10"
9??????},
10??????"aggs":?{
11????????"salary_max_user":?{
12??????????"top_hits":?{
13????????????"size":?1,
14????????????"sort":?[
15??????????????{
16????????????????"salary":?{
17??????????????????"order":?"desc"
18????????????????}
19??????????????}
20????????????]
21??????????}
22????????}
23??????}
24????}
25??}
26}

(2)、Java 代碼示例

 1import?lombok.extern.slf4j.Slf4j;
2import?org.elasticsearch.action.search.SearchRequest;
3import?org.elasticsearch.action.search.SearchResponse;
4import?org.elasticsearch.client.RequestOptions;
5import?org.elasticsearch.client.RestHighLevelClient;
6import?org.elasticsearch.rest.RestStatus;
7import?org.elasticsearch.search.SearchHit;
8import?org.elasticsearch.search.aggregations.AggregationBuilder;
9import?org.elasticsearch.search.aggregations.AggregationBuilders;
10import?org.elasticsearch.search.aggregations.Aggregations;
11import?org.elasticsearch.search.aggregations.bucket.terms.Terms;
12import?org.elasticsearch.search.aggregations.metrics.tophits.ParsedTopHits;
13import?org.elasticsearch.search.builder.SearchSourceBuilder;
14import?org.elasticsearch.search.sort.SortOrder;
15import?org.springframework.beans.factory.annotation.Autowired;
16import?org.springframework.stereotype.Service;
17import?java.io.IOException;
18import?java.util.List;
19
20@Slf4j
21@Service
22public?class?AggrBucketMetricService?{
23
24????@Autowired
25????private?RestHighLevelClient?restHighLevelClient;
26
27????/**28?????*?topHits?按歲數分桶、然后統計每個員工工資最高值29?????*/
30????public?Object?aggregationTopHits()?{
31????????try?{
32????????????AggregationBuilder?testTop?=?AggregationBuilders.topHits("salary_max_user")
33????????????????????.size(1)
34????????????????????.sort("salary",?SortOrder.DESC);
35????????????AggregationBuilder?salaryBucket?=?AggregationBuilders.terms("salary_bucket")
36????????????????????.field("age")
37????????????????????.size(10);
38????????????salaryBucket.subAggregation(testTop);
39????????????//?查詢源構建器
40????????????SearchSourceBuilder?searchSourceBuilder?=?new?SearchSourceBuilder();
41????????????searchSourceBuilder.size(0);
42????????????searchSourceBuilder.aggregation(salaryBucket);
43????????????//?創建查詢請求對象,將查詢條件配置到其中
44????????????SearchRequest?request?=?new?SearchRequest("mydlq-user");
45????????????request.source(searchSourceBuilder);
46????????????//?執行請求
47????????????SearchResponse?response?=?restHighLevelClient.search(request,?RequestOptions.DEFAULT);
48????????????//?獲取響應中的聚合信息
49????????????Aggregations?aggregations?=?response.getAggregations();
50????????????//?輸出內容
51????????????if?(RestStatus.OK.equals(response.status()))?{
52????????????????//?分桶
53????????????????Terms?byCompanyAggregation?=?aggregations.get("salary_bucket");
54????????????????List?extends?Terms.Bucket>?buckets?=?byCompanyAggregation.getBuckets();
55????????????????//?輸出各個桶的內容
56????????????????log.info("-------------------------------------------");
57????????????????log.info("聚合信息:");
58????????????????for?(Terms.Bucket?bucket?:?buckets)?{
59????????????????????log.info("桶名:{}",?bucket.getKeyAsString());
60????????????????????ParsedTopHits?topHits?=?bucket.getAggregations().get("salary_max_user");
61????????????????????for?(SearchHit?hit:topHits.getHits()){
62????????????????????????log.info(hit.getSourceAsString());
63????????????????????}
64????????????????}
65????????????????log.info("-------------------------------------------");
66????????????}
67????????}?catch?(IOException?e)?{
68????????????log.error("",?e);
69????????}
70????}
71
72}

a84743e4b71a2aa19aafda1f6d008a2e.gif

●?SpringBoot 使用 Caffeine 本地緩存

●?Github推出了GitHub CLI

●?(很全面)SpringBoot 集成 Apollo 配置中心

●?你知道如何成為一名靠譜的架構師不?

●?Tomcat 在 SpringBoot 中是如何啟動的?

●?SpringBoot 深度調優,讓你的項目飛起來!

●?8種經常被忽視的SQL錯誤用法,你有沒有踩過坑?

●?Java面試應該知道之深入理解Java的接口和抽象類

●?Spring系列之beanFactory與ApplicationContext

●?多線程同步的五種方法

●?redis應用場景

●?手把手帶你剖析 Springboot 啟動原理!

●?Java多線程:synchronized關鍵字和Lock

●?Java多線程:多線程基礎知識

●?Kafka基本架構及原理

8e51bd1eea0ef6630ee3f1054df9099b.png

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/135972.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息