ElasticSearch8.5

Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,Elasticsearch 会集中存储您的数据,让您飞快完成搜索,微调相关性,进行强大的分析,并轻松缩放规模

官方地址

https://www.elastic.co/cn/elasticsearch/

全文检索

全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置。当用户查询时根据建立的索引查找,类似于通过字典的检索字表查字的过程。
全文检索(Full-Text Retrieval)以文本作为检索对象,找出含有指定词汇的文本。全面、准确和快速是衡量全文检索系统的关键指标。

  1. 只处理文本、不处理语义
  2. 搜索时英文不区分大小写
  3. 结果列表有相关度排序

倒排索引

我们如Mysql使用的是正排索引,简单的理解倒排索引就是通过内容获取到索引值,例如查询hello显示在文章的第一行第十个单词

倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inverted index)。带有倒排索引的文件我们称为倒排索引文件,简称倒排文件(inverted file)。

ES安装

windows

https://www.elastic.co/cn/downloads/elasticsearch
在这里插入图片描述
下载后直接解压放置到你想放发地方就行

启动

双击elasticsearch.bat或使用cmd命令启动./elasticsearch.bat
在这里插入图片描述

如果不出以外你应该会报错并且访问127.0.0.1:9200出现连接已重置的问题
在这里插入图片描述
这时我们直接去config目录下修改配置文件

在这里插入图片描述
把所有的enabled:true改成false保存后重启bat
在这里插入图片描述
虽然你还是能看到报错但是这里正常启动了,主要是因为有写默认的数据库说明的那些配置你压根没有所以起不起来

在这里插入图片描述

ES使用

我们采用http方式的Restful风格对ES进行使用,使用的工具是APIFox

创建索引

请求方式PUT
请求路径127.0.0.1:9200
请求参数indexName
参数类型string
参数规范使用全小写命名
示例值testing1

返回值:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "testing1"
}

在这里插入图片描述

查询索引

请求方式GET
请求路径127.0.0.1:9200
请求参数indexName
参数类型string
参数规范使用全小写命名
示例值testing1

返回值:

{
    "testing1": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "testing1",
                "creation_date": "1667768312802",
                "number_of_replicas": "1",
                "uuid": "JLIaQLWLQJyzTHHG3_rVkg",
                "version": {
                    "created": "8050099"
                }
            }
        }
    }
}

查询所有索引

请求方式GET
请求路径127.0.0.1:9200/_cat/indices
请求参数v
参数类型string
参数规范无需填写
示例值

返回值:

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   testing2 Ib9-iykmQ6WTOOU0ExAzdQ   1   1          0            0       225b           225b
yellow open   testing1 JLIaQLWLQJyzTHHG3_rVkg   1   1          0            0       225b           225b

在这里插入图片描述

删除索引

请求方式DELETE
请求路径127.0.0.1:9200
请求参数indexName
参数类型string
参数规范使用全小写命名
示例值testing1

返回值:

{
    "acknowledged": true
}

在这里插入图片描述

创建文档

请求方式POST
请求路径127.0.0.1:9200/{indexName}/_doc
参数indexName
参数类型string
参数规范使用全小写命名
示例值testing2

请求体:

{
    "articleName":"testing2测试",
    "catgory":"原创",
    "content":"this is a test"
}

返回值:

{
    "_index": "testing2",
    "_id": "ETrSToQBZKxpJDuDc2gL",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

在这里插入图片描述

创建文档(自定义文档ID)

请求方式POST
请求路径127.0.0.1:9200/{indexName}/_doc/{docId}
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2docId
参数2类型string
参数2规范
参数2示例值116688

请求体:

{
    "articleName":"testing2测试",
    "catgory":"原创",
    "content":"this is a test"
}

返回值:

{
    "_index": "testing2",
    "_id": "116688",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

在这里插入图片描述

文档查询

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_doc/{docId}
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2docId
参数2类型string
参数2规范
参数2示例值116688

返回值:

{
    "_index": "testing2",
    "_id": "116688",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "articleName": "testing2测试",
        "catgory": "原创",
        "content": "this is a test"
    }
}

在这里插入图片描述

文档查询(所有内容文档)

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_search
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2

返回值:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "testing2",
                "_id": "ETrSToQBZKxpJDuDc2gL",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "116688",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            }
        ]
    }
}

文档修改(覆盖)

请求方式PUT
请求路径127.0.0.1:9200/{indexName}/_doc/{docId}
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2docId
参数2类型string
参数2规范
参数2示例值116688

请求体:

{
    "articleName":"testing2测试覆盖",
    "catgory":"原创",
    "content":"this is a test-覆盖"
}

返回值:

{
    "_index": "testing2",
    "_id": "116688",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

在这里插入图片描述

文档修改(局部)

请求方式POST
请求路径127.0.0.1:9200/{indexName}/_update/{docId}
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2docId
参数2类型string
参数2规范
参数2示例值116688

请求体:

{
    "doc":{
        "content":"局部修改"
    }
}

返回值:

{
    "_index": "testing2",
    "_id": "116688",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

在这里插入图片描述

文档删除

请求方式DELETE
请求路径127.0.0.1:9200/{indexName}/_doc/{docId}
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2docId
参数2类型string
参数2规范
参数2示例值116688

返回值:

{
    "_index": "testing2",
    "_id": "116688",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

在这里插入图片描述

字段条件查询

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_search
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2
参数2q
参数2类型string
参数2规范说明:查询条件
参数2示例值catgory:原创

返回值:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 0.17402273,
    "hits": [
      {
        "_index": "testing2",
        "_id": "ETrSToQBZKxpJDuDc2gL",
        "_score": 0.17402273,
        "_source": {
          "articleName": "testing2测试",
          "catgory": "原创",
          "content": "this is a test"
        }
      },
      {
        "_index": "testing2",
        "_id": "Ezr1ToQBZKxpJDuDemhS",
        "_score": 0.17402273,
        "_source": {
          "articleName": "testing2测试2",
          "catgory": "原创",
          "content": "this is a test"
        }
      },
      {
        "_index": "testing2",
        "_id": "116688",
        "_score": 0.17402273,
        "_source": {
          "articleName": "testing2测试",
          "catgory": "原创",
          "content": "this is a test"
        }
      },
      {
        "_index": "testing2",
        "_id": "116699",
        "_score": 0.17402273,
        "_source": {
          "articleName": "testing2测试4",
          "catgory": "原创",
          "content": "this is a test"
        }
      }
    ]
  }
}

在这里插入图片描述

字段条件查询(请求体方式)

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_search
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2

请求体:

{
    "query":{
        "match":{
            "catgory":"原创"
        }
    }
}

返回值:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 0.17402273,
        "hits": [
            {
                "_index": "testing2",
                "_id": "ETrSToQBZKxpJDuDc2gL",
                "_score": 0.17402273,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "Ezr1ToQBZKxpJDuDemhS",
                "_score": 0.17402273,
                "_source": {
                    "articleName": "testing2测试2",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "116688",
                "_score": 0.17402273,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "116699",
                "_score": 0.17402273,
                "_source": {
                    "articleName": "testing2测试4",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            }
        ]
    }
}

在这里插入图片描述

分页查询

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_search
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2

请求体:

{
    "query": {
        "match_all": {}
    },
    "from": 0, //开始的页数,公式:(页数-1)*size
    "size": 3 //每页的大小
}

返回值:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "testing2",
                "_id": "ETrSToQBZKxpJDuDc2gL",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "Ezr1ToQBZKxpJDuDemhS",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试2",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            },
            {
                "_index": "testing2",
                "_id": "116688",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试",
                    "catgory": "原创",
                    "content": "this is a test"
                }
            }
        ]
    }
}

分页查询(指定查询字段)

请求方式GET
请求路径127.0.0.1:9200/{indexName}/_search
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2

请求体:

{
    "query": {
        "match_all": {}
    },
    "from": 0, //开始的页数,公式:(页数-1)*size
    "size": 3, //每页的大小
    "_source": ["articleName"] //设置查询的字段名称,放入list中
}

返回值:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "testing2",
                "_id": "ETrSToQBZKxpJDuDc2gL",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试"
                }
            },
            {
                "_index": "testing2",
                "_id": "Ezr1ToQBZKxpJDuDemhS",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试2"
                }
            },
            {
                "_index": "testing2",
                "_id": "116688",
                "_score": 1,
                "_source": {
                    "articleName": "testing2测试"
                }
            }
        ]
    }
}

在这里插入图片描述

分页查询(排序)

修改请求体添加字段,示例如下:

{
    "query": {
        "match_all": {}
    },
    "from": 0, //开始的页数,公式:(页数-1)*size
    "size": 3, //每页的大小
    "_source": ["articleName"], //设置查询的字段名称,放入list中
    "sort":{
        "level":{   //设置需要排序的字段名称
            "order": "desc" //设置排序方式:1.desc降序 2.asc升序
        }
    }
}

多条件查询

条件必须同时成立

请求体:

{
    "query": {
        "bool": { //标识进行多条件查询
            "must": [ //标识必须同时成立的查询条件
                {
                    "match": { //匹配条件
                        "catgory": "原创"
                    }
                },
                {
                    "match": { //匹配条件
                        "articleName": "testing2测试"
                    }
                }
            ]
        }
    }
}
条件成立一个即可
{{
    "query": {
        "bool": { //标识进行多条件查询
            "should": [ //标识任一条件成立
                {
                    "match": { //匹配条件
                        "catgory": "原创"
                    }
                },
                {
                    "match": { //匹配条件
                        "articleName": "testing2测试"
                    }
                }
            ]
        }
    }
}

范围查询

请求体:

{
    "query": {
        "bool": { //标识进行多条件查询
            "should": [ //标识任一条件成立
                {
                    "match": { //匹配条件
                        "catgory": "原创"
                    }
                },
                {
                    "match": { //匹配条件
                        "articleName": "testing2测试"
                    }
                }
            ],
            "filter":{ //标识过滤器
                "range":{ //标识进行范围查询
                    "level":{ //范围查询的条件
                        "gt": 4 // GT大于,GE大于或等于,NE是不等于,EQ是等于,LT小于,LE小于或等于
                    }
                }
            }
        }
    }
}

全文检索(完全匹配)

请求体:

{
    "query":{
        "match_phrase":{ //表示进行完全匹配
            "articleName":"testing2测试"
        }
    }
}

高亮显示

请求体:

{
    "query":{
        "match_phrase":{ //表示进行完全匹配
            "articleName":"testing2测试4"
        }
    },
    "highlight":{
        "fields":{
            "articleName":{}
        }
    }
}

聚合操作

请求体:

{
    "aggs": { //聚合操作
        "levelGroup": { //分组名称,任意
            "terms": { //分组:terms,平均值:avg
                "field": "level" //分组字段
            }
        }
    },
    "size" : 0   //添加"size":0可去除原始数据
}

设置映射

请求方式PUT
请求路径127.0.0.1:9200/{indexName}/_mapping
参数1indexName
参数1类型string
参数1规范使用全小写命名
参数1示例值testing2

请求体:

{
    "properties": { //设置映射关系
        "name": {
            "type": "text", //表示可以分词
            "index": true //表示可以被索引
        },
        "email": {
            "type": "keyword", //表示不可以分词
            "index": true //表示可以被索引
        },
        "hidden": {
            "type": "keyword", //表示不可以分词
            "index": false //表示不可以被索引
        }
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐