ElasticSearch 在 Kibana中的操作

Kibana 命令使用

PUT 创建

创建 一个 test 名字的索引

1
PUT /test

查询所有的索引列表

1
GET /_cat/indices

GET 查看 test 索引信息

1
GET /test

PUT 带参数

1、Text 会分词、然后进行索引,字段能被全文搜索
2、keyword 不分词,直接搜索,适合索引结构化的字段 如商品分类 文字标签【类似于 mysql nickname=’张三’,精准匹配】
3、byte 适用于 年龄
4、integer 类似于 mysql int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// PUT /test1
{
"settings": {
"number_of_shards": 1, //默认1
"number_of_replicas": 1 //副本
},
"mappings": {
//属性
"properties": {
"nickname": { "type": "text" }, //设置名字
"age": { "type": "byte" } //设置年龄
}
}
}

DELETE 删除

1
DELETE /test

判断索引是否存在

1
HEAD /test

POST 添加数据

test 索引名称
_doc 类型
1 id 唯一表示类似于 mysql id 自增

1
2
3
4
5
POST /test/_doc/1
{
"nickname": "张三",
"age": 18
}

查询 id = 1 的数据

根据 ID 查询

1
GET /test/_doc/1

查询 test 索引的所有数据

1
GET /test/_search

根据 ID 修改数据

1
2
3
4
5
6
7
POST /test/_update/1
{
"doc": {
"nickname": "李四",
"age": 22
}
}

_bulk 批量操作

create 没有就是新增,有的话报错
index 没有就是新增,有的话修改
update 更新
delete 删除

1
2
3
4
5
6
POST /_bulk{
{"index":{"_index":"two","_id":1}}
{"nickname":"李四"}
{"index":{"_index":"two","_id":2}}
{"nickname":"王武"}

create

1
2
3
4
POST /_bulk{
{"create":{"_index":"two","id":1}}
{"nickname":"李四"}

update

1
2
3
4
POST /_bulk{
{"create":{"_index":"two","id":1}}
{"doc":"{nickname":"李四"}}

deltete

1
2
3
4
POST /_bulk{
{"deltete":{"_index":"two","_id":1}}
{"deltete":{"_index":"two","_id":2}}
}

多个 ID 搜索

1
2
3
4
GET /test/_mget
{
"ids":[1,2]
}

URL 条件查询

1
2
3
4
5
6
7
8
9
10
11
12
// id 查询
GET /test/_doc/1

// 限制字段显示 类似于 select nickname,age from table where id=1
GETT /test/_doc/1?_source=nickname,age

// 查询nickname
GET /test/_search?q=nickname:李四

// 分页 size=每页数量 from=从哪里开始
GET /test/_search?size=10&from=1

DSL 查询

普通查询

查询年龄字段 >40 & <50 的 ,每页 10 条,从第三条开始展示,年龄从大到小(先排序在分页),只要 nickname 字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
POST /test/_search
{
"query": {
"range": {
"age": {
"gte": 40,
"lte": 50
}
}
},
"_source": ["nickname"],
"from": 2,
"size": 10,
"sort": [
{
"age": {
"order": "desc"
}
}
]
}

match

match 会根据查询的字短进行分词,在进行分词查询 模糊匹配 (select * from table where nickname like ‘%张%’ or nickname like ‘%山%’)

1
2
3
4
5
6
7
8
POST /test/_search
{
"query": {
"match": {
"nickname": "张三"
}
}
}

multi_match

multi_match 根据哪个字段进行搜索,比如搜索用户和描述 两个字段中有没有张三

1
2
3
4
5
6
7
8
{
"query": {
"multi_match": {
"query": "张三",
"fields": ["nickname", "desc"]
}
}
}

trem

trem 不会进行分词,会采用精确匹配,也是类似于模糊查询(select * from table where nickname like ‘%张三%’)

1
2
3
4
5
6
7
8
POST /test/_search
{
"query": {
"trem": {
"nickname": "张三"
}
}
}

高亮展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"query": {
"trem": {
"nickname": "张三"
}
},
"highlight": {
"fields": {
"nickname": {}
},
// "tags_schema": "styled", //默认标签包裹 和下面两个字段不能一起
// 下面的是自定义标签包括
"pre_tags": ["<font class='style:red'>"],
"post_tags": ["</font>"]
}
}

分词

默认是以空格为分词的,对英文友好.中文分词是每个字进行匹配

查询分词效果

1
2
3
4
5
GET /\_analyze
{
"tokenizer": "standard",
"text": "张三"
}

安装中文分词器

ik_smart 分少个词

1
2
3
4
5
GET /\_analyze
{
"tokenizer": "ik_smart",
"text": "北京市海淀区"
}

ik_max_word 分多个词

1
2
3
4
5
GET /\_analyze
{
"tokenizer": "ik_max_word",
"text": "北京市海淀区"
}

分词要在创建索引的时候,指定分词器

创建索引 里面有 nickname age addr 三个字段,add 设置了分词器和搜索分词器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PUT /ik
{
"mappings":{
"properties":{
"nickname":{
"type":"text"
},
"age":{
"type":"byte"
},
"addr":{
"type":"text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
}
}
}
}

插入测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
POST /_bulk
{"index":{"_index":"ik","_id":1}}
{"nickname":"李四","age":18,"addr":"山西省忻州市忻府区"}
{"index":{"_index":"ik","_id":2}}
{"nickname":"王武","age":25,"addr":"山西省忻州市忻府区"}
{"index":{"_index":"ik","_id":3}}
{"nickname":"赵四","age":30,"addr":"河南省郑州市"}
{"index":{"_index":"ik","_id":4}}
{"nickname":"赵五","age":30,"addr":"河南省信阳市"}
{"index":{"_index":"ik","_id":5}}
{"nickname":"王老五","age":30,"addr":"河南省漯河市"}


match_phrase

1
2
3
4
5
6
7
8
9
10
{
"query": {
"match_phrase": {
"addr": {
"query": "山西省",
"slop": 20
}
}
}
}

悲观锁

单线程 和 redis 一样

乐观锁

根据版本号 进行并发控制,进行版本对比,不一致重新生成新的数据和版本号


ElasticSearch 在 Kibana中的操作
https://code-lives.github.io/2023/06/15/ElasticSearch/
作者
Li Jie
发布于
2023年6月15日
许可协议