【ElasticSearch】01-ElasticSearch使用

cover-01

安装

Docker

IK分词器

分词器,就是将中文、英文拆分成多个词汇,包括单个汉字。

如:我爱玩手机,可以被分为:我、爱、玩、手机、玩手机五个词汇。

算法

IK分词器提供了两种算法:

1. ik_smart:最少切分。
2. ik_max_word:最细粒度划分。

安装

  1. 下载相应版本的ik分词器https://github.com/medcl/elasticsearch-analysis-ik/tags

  2. 下载完毕之后,解压并放入到elasticsearch中plugins下的ik文件夹中。

    img-02

  3. 重启elasticsearch。

    img-01

使用Kibana测试

ik_smart、ik_max_word使用效果

使用

最粗粒度划分

1
2
3
4
5
6
// 最粗粒度划分(zn-Ch)
GET _analyze
{
"analyzer": "ik_smart",
"text": "中国人民共和国"
}

被划分为:’中国人民’, ‘共和国’ 两个词语

img-14

最细粒度划分

1
2
3
4
5
6
// 最细粒度划分(zn-Ch)
GET _analyze
{
"analyzer": "ik_max_word",
"text": "中国人民共和国"
}

被划分为:’中国人民’, ‘中国人’, ‘中国’, ‘国人’, ‘人民共和国’, ‘人民’, ‘共和国’ 七个词语

img-15

扩展分词器

想要在分词其中添加没有的词汇。如:’举头望明月’。

未添加自定义分词器

1
2
3
4
5
GET _analyze
{
"analyzer": "ik_smart",
"text": "举头望明月"
}

‘举头望明月’被分解为三个词:’举头’, ‘望’, ‘明月’

img-16

添加自定义分词器

  1. 打开C/development/elasticsearch/elasticsearch-7.11.1/plugin/ik/config/文件夹。

    img-05

  2. 在该文件夹下创建自己的dic文件。

    注意:最好复制已有的dic文件,将其修改。自己创建的dic文件可能因为格式问题无法被识别。

    img-04

  3. 修改IKAnalyzer.cfg.xml文件

    img-06

  4. 重启ElasticSearch,查看日志

    创建的字典已经被加载到分词器中

    img-07

  5. 再次解析“举头望明月”

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    GET _analyze
    {
    "analyzer": "ik_smart",
    "text": "举头望明月"
    }

    {
    "tokens": [
    {
    "token": "举头望明月",
    "start_offset": 0,
    "end_offset": 5,
    "type": "CN_WORD",
    "position": 0
    }
    ]
    }

    img-17

ElasticSearch基本操作

ElasticSearch,其增删改查操作完全遵循Restful风格。查询:GET;添加:POST;修改:PUT;删除:DELETE;

操作索引

查寻索引

查询索引信息GET /_cat/indices?v

img-18

添加索引

创建一个名为test的索引PUT /test/

img-19

删除索引

删除一个名为test的索引DELETE /test
删除所有索引DELETE /*

img-20

操作文档

文档类型几乎已被弃用,全都被_doc所代替。

method url地址 描述
PUT localhost:9200/索引名称/类型名称/文档id 创建文档(指定文档id)
POST localhost:9200/索引名称/类型名称 创建文档(随即文档id)
POST localhost:9200/索引名称/_update/文档id 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 查询文档通过文档id
POST localhost:9200/索引名称/类型名称/_search 查询所有数据

创建文档

通过PUT方式
该方式创建必须指定id。

1
2
3
4
5
6
7
//请求
PUT /first_index/_doc/1
{
"name": "张三",
"age": 33,
"gender": 1
}

img-21

通过POST方式
该方式创建无需指定id,elasticsearch会为其自动创建一个字符串作为id;也可以向PUT请求一样指定id。

1
2
3
4
5
6
POST /student/_doc
{
"name": "李四",
"gender": 2,
"age": 20
}

img-22

修改文档

修改所有字段
该方法需要指定id,并覆盖指定id的所有数据

1
2
3
4
5
6
PUT /student/_doc/1
{
"name": "张三",
"gender": 1,
"age": 99
}

img-23

修改某些字段
对某一个文档进修改某些字段时使用。

1
2
3
4
5
6
POST /student/_update/1
{
"doc": {
"name": "张三三"
}
}

img-24

删除文档

删除文档

指定索引名称,id删除某个文档

1
DELETE /first_index/_doc/1

img-25

返回结果说明

字段 说明
version 更新次数
result 操作类型(update:更新,created:创建)
score 匹配度。如果分值越高,查询出的结果优先级越高

查询文档

使用first_index索引。先给这个索引插入一些数据,供后面查询使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//1
PUT /first_index/_doc/1
{
"name": "张三", "age": 33, "gender": 1
}
//2
PUT /first_index/_doc/2
{
"name": "李四", "age": 4, "gender": 2
}
//3
PUT /first_index/_doc/3
{
"name": "王五", "age": 55, "gender": 1
}
//4
PUT /first_index/_doc/4
{
"name": "赵六", "age": 66, "gender": 1
}
//5
PUT /first_index/_doc/5
{
"name": "张三四", "age": 34, "gender": 1
}
//6
PUT /first_index/_doc/6
{
"name": "张三六", "age": 36, "gender": 2
}
//7
POST /first_index/_doc
{
"name": "李四", "gender": 2, "age": 20
}

指定id查询

1
GET /first_index/_doc/1

img-26

查询全部

1
GET /first_index/_search

img-27

分页查询
from(偏移量), size 相当于MySQL的offset和limit

1
2
3
4
5
GET /first_index/_search
{
"from": 3,
"size": 2
}

img-28

排序查询
sort键的value是一个数据,可以对多个字段进行排序

1
2
3
4
5
6
7
8
9
10
GET /first_index/_search
{
"sort": [
{
"age": {
"order": "desc"
}
}
]
}

img-29

批量查询

1
2
3
4
GET /first_index/_mget
{
"ids": [1, "j8UCeX0BwclvZt6-7z2q"]
}

img-30

泛查询

  1. 查询包含的数据

    这种方式查询不限制字段,可以是name,gender等其他字段,只要包含这个字,都可以被查询到

    1
    GET /first_index/_search?q=四

    img-31

  2. 指定字段,查询包含的数据

    1
    GET /first_index/_search?q=name:"四"

    img-32

复杂查询

match

  1. 单条件查询

    1
    2
    3
    4
    5
    6
    7
    8
    GET /first_index/_search
    {
    "query": {
    "match": {
    "name": "四"
    }
    }
    }

    img-33

  2. 单条件查询、匹配字段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    GET /first_index/_search
    {
    "query": {
    "match": {
    "name": "四"
    }
    },
    "_source": [
    "name",
    "age"
    ]
    }

    img-34

  3. 单条件查询、匹配字段、排序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    GET /first_index/_search
    {
    "query": {
    "match": {
    "name": "四"
    }
    },
    "_source": [
    "name",
    "age"
    ],
    "sort": [
    {
    "age": {
    "order": "desc"
    }
    }
    ]
    }

    img-35

  4. 单条件查询、匹配字段、排序、分页

    1
    2
    3
    4
    5
    6
    7
    GET /first_index/_search
    {
    "query": { "match": { "name": "四" } },
    "_source": [ "name", "age" ],
    "sort": [ { "age": { "order": "desc" } ],
    "from": 0, "size": 5
    }

    img-37

must

  • name中包含,
  • age在[30, 40]范围内
1
2
3
4
5
6
7
8
9
10
11
GET /first_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "三和四" } },
{ "range": { "age": { "gte": 30, "lte": 40 } } }
]
}
}
}

img-38

should

  • name中包含,

  • age在[30, 35]范围之间

1
2
3
4
5
6
7
8
9
10
11
GET /first_index/_search
{
"query": {
"bool": {
"should": [
{ "match": { "name": "三和四" } },
{ "range": { "age": { "gte": 30, "lte": 35 } } }
]
}
}
}

img-39

must_not

  • name中不包含,
  • age的范围不在[30, 35]之间
  • !(name.contails("三") || name.contails("四") || (age >= 30 && age <= 35))
1
2
3
4
5
6
7
8
9
10
11
GET /first_index/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "name": "三和四" } },
{ "range": { "age": { "gte": 30, "lte": 35 } } }
]
}
}
}

img-40

filter

  • name中包含,
  • age在[30, 40]范围内
  • 过滤掉age在[30, 33]范围内的数据
1
2
3
4
5
6
7
8
9
10
11
12
GET /first_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "三和四" } },
{ "range": { "age": { "gte": 30, "lte": 35 } } }
],
"filter": [ { "range": { "age": { "gte": 30, "lte": 33 } } }]
}
}
}

img-41

term

高亮

  • 给name字段中的,添加高亮
1
2
3
4
5
6
7
8
9
10
11
12
13
GET /first_index/_search
{
"query": {
"match": {
"name": "三"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}

img-42

自定义标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GET /first_index/_search
{
"query": {
"match": {
"name": "三"
}
},
"highlight": {
"pre_tags": "<p style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}

img-43

term:直接去查询,且该字段类型需为keyword。

match:会使用分词器进行查询。

两个类型

  1. text:会被分词器解析。
  2. keyword:不会被分词器解析。

返回结果说明

字段 说明
version 更新次数
result 操作类型(update:更新,created:创建)
score 匹配度。如果分值越高,查询出的结果优先级越高
hits 索引和文档信息(结果总数、数据结果)
-------------本文结束感谢您的阅读-------------