以下操作均使用 Postman 执行。Elasticsearch版本为7.4.2
一、索引
1. 创建索引(索引名为 index_demo)
请求方式:PUT
接口: http://localhost:9200/index_demo
入参(以json形式入参):
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "0"
}
}
}
2. 查看单个索引
请求方式:GET
接口: http://localhost:9200/index_demo
3. 查看全部索引
请求方式:GET
接口: http://localhost:9200/_cat/indices?v
4. 删除索引
请求方式:DELETE
接口: http://localhost:9200/index_demo
二、索引的mappings映射(相当于数据结构)
1. 创建索引的同时创建mappings
请求方式:PUT
接口:http://localhost:9200/index_demo
入参(json形式):
{
"mappings": {
"properties": {
"realname": {
"type": "text",
"index": true
},
"username": {
"type": "keyword",
"index": false
}
}
}
}
- index:默认true,设置为false的话,那么这个字段就不会被索引
2. 为已存在的索引创建mappings
请求方式:PUT
接口:http://localhost:9200/index_demo/_mapping
入参(json形式):
{
"properties": {
"id": {
"type": "long"
},
"age": {
"type": "integer"
},
"nickname": {
"type": "keyword"
},
"money1": {
"type": "float"
},
"money2": {
"type": "double"
},
"sex": {
"type": "byte"
},
"score": {
"type": "short"
},
"is_teenager": {
"type": "boolean"
},
"birthday": {
"type": "date"
},
"relationship": {
"type": "object"
}
}
}
- 注:某个属性一旦被建立,就不能修改了,但是可以新增额外属性
3. 测试分词效果
请求方式:GET(有些 ElasticSearch 版本 可能需要使用 POST)
接口:http://localhost:9200/index_demo/_analyze
入参(json形式):
{
"field": "realname",
"text": "didiok is good"
}
4. 主要数据类型
- text, keyword
- long, integer, short, byte
- double, float
- boolean
- date
- object
- 数组中的类型要一致,不能混合
字符串类型中 text 与 keyword 的区别
- text:文字类需要被分词被倒排索引的内容,比如商品名称,商品详情,商品介绍,使用text。
- keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态,用户qq,微信号,手机号等,这些精确匹配,无需分词。
三、文档document的基本操作
1. 新增
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/1
{索引名}/_doc/{索引ID}(索引ID是指索引在es中的ID,如果不写的话,会自动生成一个ID)
入参(json形式):
{
"id": 1001,
"name": "didiok-1",
"desc": "didiok is very good, 我喜欢学习!",
"create_date": "2022-12-24"
}
- 注:如果索引没有手动建立mappings,那么当插入文档数据的时候,会根据文档类型自动设置属性类型。这个就是es的动态映射,帮我们在index索引库中去建立数据结构的相关配置信息。
2. 修改
(1)只修改文档中的某个字段值
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/1/_update
入参(json形式):
{
"doc": {
"name": "didiok-2"
}
}
(2)修改整个文档document
请求方式:PUT
接口:http://localhost:9200/index_demo/_doc/1
入参(json形式):
{
"id": 123,
"name": "didiok-3",
"desc": "这是一个全量替换!",
"create_date": "2022-12-24"
}
3. 删除
请求方式:DELETE
接口:http://localhost:9200/index_demo/_doc/1
- 文档删除不是立即删除,文档还是保存在磁盘上,索引增长越来越多,才会把那些曾经标识过删除的,进行清理,从磁盘上移出去。
4. 查询
(1)普通查询
请求方式:GET
查询某一篇文档:http://localhost:9200/index_demo/_doc/1
查询全部文档:http://localhost:9200/index_demo/_doc/_search
查询结果:
{
"_index": "my_doc",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_version": 9,
"_source": {
"id": 123,
"name": "didiok-3",
"desc": "这是一个全量替换!",
"create_date": "2022-12-24"
}
}
- _index:文档数据所属那个索引,理解为数据库的某张表即可。
- _type:类型,新版本使用_doc。
- _id:文档的唯一标识,表的主键。可以自动生成或手动指定。
- _score:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高。
- _version:版本号。
- _source:文档数据,json格式。
(2)查询文档中的指定字段:
请求方式:GET
接口:http://localhost:9200/index_demo/_doc/1?_source=id,name
http://localhost:9200/index_demo/_doc/_search?_source=id,name
(3) 根据关键字查询
请求方式:GET
接口:http://localhost:9200/index_demo/_doc/_search?q=desc:这是关键字&q=name:第二个关键字
(4)判断某篇文档是否存在
请求方式:HEAD
接口:http://localhost:9200/index_demo/_doc/1
四、文档乐观锁控制 if_seq_no与if_primary_term
1. 插入新数据
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/3
{
"id": 1010,
"name": "didiok-1010",
"desc": "are you ok!",
"create_date": "2023-12-24"
}
# 此时 _version 为 1
2. 修改数据
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/3/_update
{
"doc": {
"name": "嘀咕文学奖"
}
}
# 此时 _version 为 2
3. 模拟两个客户端操作同一个文档数据
_version版本号(现在使用_seq_no和_primary_term共同来作为版本号,替代旧版的_version)都携带为一样的数值,假如上面第2步之后,_seq_no=30,_primary_term=1
(1)操作1
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/3/_update?if_seq_no={数值}&if_primary_term={数值}
{
"doc": {
"name": "hello123"
}
}
(2)操作2
请求方式:POST
接口:http://localhost:9200/index_demo/_doc/3/_update?if_seq_no={数值}&if_primary_term={数值}
{
"doc": {
"name": "hello12345"
}
}
当以上两个操作中的 if_seq_no=30 和 if_primary_term=1 的值都使用当前的_seq_no=30,_primary_term=1时,操作2就会修改失败
"_version":引入了乐观锁机制,每次变更都会对version进行增加 "_seq_no"功能与version类似 "_primary_term"数据所在位置