一.ElasticSearch 简介:
 1.介绍:
     1)全文搜索,属于最常见的需求,开源的 Elasticsearch 是目前,全文搜索引擎的首选。
     2)它可以快速的存储、搜索、和分析,海量的数据。
     3)Elastic 是对 Lucene 的封装,提供了 RESTful 风格API 的操作接口。开箱即用
     4)官网:
     -1:官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
     -2:官网中文:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
 
 
 2.用途:
 
 
 3.基本概念:
     1)Index(索引)
 动词:相当于 Mysql 中的 insert。
 名次:相当于 Mysql 中的 DataBase(数据库)。
     2)Type(类型)
 在 Index(索引) 中,可以定义 一个/多个 Type(类型)。
 类似于 Mysql 中的 Table,每一种类型的数据库,放在一起。
     3)文档 (Document)
 保存在 某个索引(index)下,某种类型(Type)下的一个数据(Document)。
 文档时 JSON 格式的。
 文档(Document)就像是,Mysql 中的某个 Table 里面的数据。
     4)图示
 
 
 4.检索原理:(倒排索引机制)
 
 
 5.工作原理(官网)
 
 
 6.索引是什么(官网)
 
 7.LogStach 用途是什么(官网)
 
 
二.ElasticSearch 安装:
 1.Docker 安装:
// 安装 Elasticsearch(存储和检索数据)
docker run -d -p 9200:9200 -p 9300:9300 
--name es -e "discovery.type=single-node" 
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" elasticsearch:7.4.2
// 安装 Kibana(可视化检索数据)
// 安装教程:
https://www.jianshu.com/p/ac3cf42dc95e
 2.
 
 3.
 
 4.
 
 5.
三.ElasticSearch 入门:
         4)Docker 安装: 
                  a:安装 ElasticSearch:7.4.2 :存储和检索数据
                  b:安装  Kibana:7.4.2        :可视化,存储和检索数据
                  c:安装教程:https://www.jianshu.com/p/ac3cf42dc95e
docker run -p 9200:9200 -p 9300:9300 
 -e “discovery.type=single-node” 
 -e ES_JAVA_OPTS="-Xms128m -Xmx256m" 
 -v /root/mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml 
 -v /root/mydata/elasticsearch/data:/usr/share/elasticsearch/data 
 -v /root/mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins 
 -d --name elasticsearch elasticsearch:7.4.2
docker run -e “discovery.type=single-node” -v /root/mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -e ES_JAVA_OPTS="-Xms64m -Xmx128m" -d -p 9200:9200 -p 9300:9300 --name es elasticsearch:7.4.2
–最终
 docker run -d -p 9200:9200 -p 9300:9300 --name es -e “discovery.type=single-node” -e ES_JAVA_OPTS="-Xms256m -Xmx256m" elasticsearch:7.4.2
 d:网址访问:Elasticsearch:http://192.168.43.189:9200/
 Kibana:http://192.168.43.189:5601/
二.ElasticSearch 初步检索(RestAPI 风格):
         1)_cat:
                  a:_cat/nodes:查询 所有节点信息:
                  b:_cat/health:查询 节点 健康状况
                  c:_cat/master:查询 主节点
                  d:_cat/indices:查询 所有 索引
         2)索引一个文档(保存)(PUT/POST):
                  a:/customer/external/1
         3)查询 文档
                  a:请求:http://114.215.173.88:9200/customer/external/1
                  b:报文:
{
 “_index”: “customer”, //在哪个索引
 “_type”: “external”, //在哪个类型
 “_id”: “1”, //记录 id
 “_version”: 1, //版本号
 “_seq_no”: 0, //并发控制字段,每次更新就会 +1
 “_primary_term”: 1, //同上 ,主分片重新分配,如重启,就会变化
 “found”: true,
 “_source”: { //找到的 数据
 “name”: “123”
 }
 }
         4)模拟并发修改(乐观锁修改):如果 if_seq_no=9&if_primary_term=1 ,则进行修改。
         5)更新文档(POST / PUT)
         6)删除文档
         7)删除索引
         8)批量 保存数据:(GET bank/account/_bulk,保存 测试数据)
三.ElasticSearch 进阶检索(Query DSL)
三.ElasticSearch 映射
四.ElasticSearch 分词 & 安装 ik 分词
五.ElasticSearch 整合 SpringBoot
Start searching | Elasticsearch Guide [7.16] | Elastic
         1)查询所有(match_all)并排序(进行分页操作,并返回部分字段):
                  a:请求信息
GET /bank/_search
 {
 “query”: {
 “match_all”: {}
 },
 “sort”: [
 {
 “account_number”: {
 “order”: “asc”
 }
 }
 ],
 “from”: 0,
 “size”: 2,
 “_source”: [“balance”,“firstname”]
 }
 b:返回报文:
{
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1000,
 “relation” : “eq”
 },
 “max_score” : null,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “0”,
 “_score” : null,
 “_source” : {
 “account_number” : 0,
 “balance” : 16623,
 “firstname” : “Bradshaw”,
 “lastname” : “Mckenzie”,
 “age” : 29,
 “gender” : “F”,
 “address” : “244 Columbus Place”,
 “employer” : “Euron”,
 “email” : “bradshawmckenzie@euron.com”,
 “city” : “Hobucken”,
 “state” : “CO”
 },
 “sort” : [
 0
 ]
 },
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “1”,
 “_score” : null,
 “_source” : {
 “account_number” : 1,
 “balance” : 39225,
 “firstname” : “Amber”,
 “lastname” : “Duke”,
 “age” : 32,
 “gender” : “M”,
 “address” : “880 Holmes Lane”,
 “employer” : “Pyrami”,
 “email” : “amberduke@pyrami.com”,
 “city” : “Brogan”,
 “state” : “IL”
 },
 “sort” : [
 1
 ]
 }
 ]
 }
 }
 c:报文解释:
took – Elasticsearch运行查询多长时间(以毫秒为单位)
 timed_out –搜索请求是否超时
 _shards –搜索了多少个分片,以及成功,失败或跳过了多少个分片。
 max_score –找到的最相关文件的分数
 hits.total.value -找到了多少个匹配的文档
 hits.sort -文档的排序位置(不按相关性得分排序时)
 hits._score-文档的相关性得分(使用时不适用match_all)
         2)条件查询(match)
                  a:match :解析:
                              a:match 匹配 ,非字符串类型,做精确查询。
                              b:match 匹配,字符串类型,做 模糊查询。
                              c:匹配字符为“mill  lane”,则有 mill 会查出来,有 lane 也会查出来,进行分词操作,只是得分 低。
                  b:"address.keyword": "990 Mill Road" : 按照 精确信息查找。
                  c:请求信息:
GET bank/_search
 {
 “query”: {
 “match”: {
 “address.keyword”: “990 Mill Road”
 }
 }
 }
 GET bank/_search
 {
 “query”: {
 “match”: {
 “address”: “Mill”
 },
 }
 }
 d:返回报文:
{
 “took” : 2,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 19,
 “relation” : “eq”
 },
 “max_score” : 9.507477,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “136”,
 “_score” : 9.507477,
 “_source” : {
 “account_number” : 136,
 “balance” : 45801,
 “firstname” : “Winnie”,
 “lastname” : “Holland”,
 “age” : 38,
 “gender” : “M”,
 “address” : “198 Mill Lane”,
 “employer” : “Neteria”,
 “email” : “winnieholland@neteria.com”,
 “city” : “Urie”,
 “state” : “IL”
 }
 },
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “970”,
 “_score” : 5.4032025,
 “_source” : {
 “account_number” : 970,
 “balance” : 19648,
 “firstname” : “Forbes”,
 “lastname” : “Wallace”,
 “age” : 28,
 “gender” : “M”,
 “address” : “990 Mill Road”,
 “employer” : “Pheast”,
 “email” : “forbeswallace@pheast.com”,
 “city” : “Lopezo”,
 “state” : “AK”
 }
 }
 ]
 }
 }
 3)短语 匹配(match_phrase):把要 匹配的词,当成一个整体(不分词),进行检索
 a:请求信息:
GET bank/_search
 {
 “query”: {
 “match_phrase”: {
 “address”: “mill lane”
 }
 }
 }
 b:返回报文:
{
 “took” : 5,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1,
 “relation” : “eq”
 },
 “max_score” : 9.507477,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “136”,
 “_score” : 9.507477,
 “_source” : {
 “account_number” : 136,
 “balance” : 45801,
 “firstname” : “Winnie”,
 “lastname” : “Holland”,
 “age” : 38,
 “gender” : “M”,
 “address” : “198 Mill Lane”,
 “employer” : “Neteria”,
 “email” : “winnieholland@neteria.com”,
 “city” : “Urie”,
 “state” : “IL”
 }
 }
 ]
 }
 }
 4)多字段匹配(multi_match)
 a:multi_match 解析:address / email 里面有 mill ,就会被查出来。会对查询条件,进行分词。
"multi_match": {
  "query": "mill",
  "fields": ["address","email"]
}
 b:请求信息:
GET bank/_search
 {
 “query”: {
 “multi_match”: {
 “query”: “Mill Street”,
 “fields”: [“address”,“email”]
 }
 }
 }
  c:返回报文:
{
 “took” : 4,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 388,
 “relation” : “eq”
 },
 “max_score” : 6.357156,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “472”,
 “_score” : 6.357156,
 “_source” : {
 “account_number” : 472,
 “balance” : 25571,
 “firstname” : “Lee”,
 “lastname” : “Long”,
 “age” : 32,
 “gender” : “F”,
 “address” : “288 Mill Street”,
 “employer” : “Comverges”,
 “email” : “leelong@comverges.com”,
 “city” : “Movico”,
 “state” : “MT”
 }
 },
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “970”,
 “_score” : 5.4032025,
 “_source” : {
 “account_number” : 970,
 “balance” : 19648,
 “firstname” : “Forbes”,
 “lastname” : “Wallace”,
 “age” : 28,
 “gender” : “M”,
 “address” : “990 Mill Road”,
 “employer” : “Pheast”,
 “email” : “forbeswallace@pheast.com”,
 “city” : “Lopezo”,
 “state” : “AK”
 }
 }
 ]
 }
 }
         5)复合查询(bool)
 a:bool 解析:
 bool 可以用来做 复合查询,复合语句,可以合并其他任何语句,包括 复合语句。
 should:应该匹配,匹配上评分靠前,不匹配上也可以。
  b:请求信息:
GET bank/_search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “match”: {
 “gender”: “M”
 }
 },{
 “match”: {
 “address”: “mill”
 }
 }
 ],
 “must_not”:
 [
 {
 “match”: {
 “age”: “38”
 }
 }
 ],
 “should”: [
 {
 “match”: {
 “lastname”: “Wallace”
 }
 }
 ]
 }
 }
 }
  c:返回报文:
{
 “took” : 0,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1,
 “relation” : “eq”
 },
 “max_score” : 6.0824604,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “970”,
 “_score” : 6.0824604,
 “_source” : {
 “account_number” : 970,
 “balance” : 19648,
 “firstname” : “Forbes”,
 “lastname” : “Wallace”,
 “age” : 28,
 “gender” : “M”,
 “address” : “990 Mill Road”,
 “employer” : “Pheast”,
 “email” : “forbeswallace@pheast.com”,
 “city” : “Lopezo”,
 “state” : “AK”
 }
 }
 ]
 }
 }
         6)结果过滤(filter)
 a:filter 解析:对结果的过滤,filter ,must_not 不会贡献 相关性得分。
  b:不使用 filter 过滤:
GET bank/_search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “range”: {
 “age”: {
 “gte”: 10,
 “lte”: 20
 }
 }
 }
 ]
 }
 }
 }
  c:使用 filter 过滤:
GET bank/_search
 {
 “query”: {
 “bool”: {
 “filter”: {
 “range”: {
 “age”: {
 “gte”: 10,
 “lte”: 20
 }
 }
 }
 }
 }
 }
  d:报文分析:
 使用 filter 过滤,结果相关性得分 为 0 。
         7)查询 字段中包含确切术语的文档(term)
 a:term 解析:
 a:您可以使用term查询根据精确的值(例如价格,产品ID或用户名)查找文档,
 避免term对text字段使用查询。要搜索text字段值,请改用match查询。因为 分词 的信息查找不到。
 b:和 match一样,匹配某个属性的值。
 全文检索 用 match,其他 非 全文检索,用 term。
  b:请求信息:
GET bank/_search
 {
 “query”: {
 “term”: {
 “address”: {
 “value”: “990 Mill Road”
 }
 }
 }
 }
  c:返回报文:
{
 “took” : 0,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 0,
 “relation” : “eq”
 },
 “max_score” : null,
 “hits” : [ ]
 }
 }
四.ElasticSearch 进阶检索(aggregation 执行聚合)
 1)聚合 介绍
  a:聚合:提供了 从数据中,分组 和 提取数值 的能力。大致等于 SQL 中,GROUP BY / 和 SQL聚合函数。
         2)搜索 address 中 ,包含 mill 的,所有人,年龄分布和平均年龄,不显示这些人详情。
 a:请求
GET bank/_search
 {
 “query”: {
 “match”: {
 “address”: “mill”
 }
 },
 “aggs”: {
 “ageAggs”: {
 “terms”: {
 “field”: “age”,
 “size”: 10
 }
 },
 “aggAVG”:{
 “avg”: {
 “field”: “age”
 }
 }
 }
 }
  b:返回报文
{
 “took” : 2,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 5.4032025,
 “hits” : [
 …
 ]
 },
 “aggregations” : {
 “aggAVG” : {
 “value” : 34.0
 },
 “ageAggs” : {
 “doc_count_error_upper_bound” : 0,
 “sum_other_doc_count” : 0,
 “buckets” : [
 {
 “key” : 38,
 “doc_count” : 2
 },
 {
 “key” : 28,
 “doc_count” : 1
 },
 {
 “key” : 32,
 “doc_count” : 1
 }
 ]
 }
 }
 }
 3)按照 年龄聚合,并且 求这些 年龄段的 这些人的 平均薪资。
  a:请求方式:
GET bank/_search
 {
 “query”: {
 “match_all”: {}
 },
 “aggs”: {
 “ageAggs”: {
 “terms”: {
 “field”: “age”,
 “size”: 2
 },
 “aggs”: {
 “balanceAVG”: {
 “avg”: {
 “field”: “balance”
 }
 }
 }
 }
 }
 }
  b:返回报文
{
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 1,
 “successful” : 1,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1000,
 “relation” : “eq”
 },
 “max_score” : 1.0,
 “hits” : [
 …
 ]
 },
 “aggregations” : {
 “ageAggs” : {
 “doc_count_error_upper_bound” : 0,
 “sum_other_doc_count” : 820,
 “buckets” : [
 {
 “key” : 31,
 “doc_count” : 61,
 “balanceAVG” : {
 “value” : 28312.918032786885
 }
 },
 {
 “key” : 39,
 “doc_count” : 60,
 “balanceAVG” : {
 “value” : 25269.583333333332
 }
 }
 ]
 }
 }
 }
四.ElasticSearch 进阶检索(mapping 映射)
 1)映射介绍:
  a:映射是定义 文档及其包含的字段 被 存储和索引方式的过程。(是如何被处理的)
         2)映射查询:
 a:查询 所有映射:
 GET bank/_mapping
  b:查询结果:
{
 “bank” : {
 “mappings” : {
 “properties” : {
 “account_number” : {
 “type” : “long”
 },
 “address” : {
 “type” : “text”,
 “fields” : {
 “keyword” : {
 “type” : “keyword”,
 “ignore_above” : 256
 }
 }
 },
 “age” : {
 “type” : “long”
 }
 }
 }
 }
 }
 3)映射修改:
  a:添加 :
  b:修改 :创建新的索引,再将数据迁移。










