赞
踩
PUT user
{
"mappings": {
"properties": {
"age": {"type": "integer"},
"email": {"type": "keyword"},
"name": {"type": "keyword"},
"address": {"type": "text", "analyzer": "ik_smart"}
}
}
}
POST user/_bulk
{"index": {"_id": 1}}
{"name": "张三", "age": 10, "email": "1.qq.com", "address": "北京朝阳"}
{"index": {"_id": 2}}
{"name": "李四", "age": 20, "email": "2.qq.com", "address": "北京西城"}
{"index": {"_id": 3}}
{"name": "王五", "age": 30, "email": "3.qq.com", "address": "北京东城"}
{"index": {"_id": 4}}
{"name": "赵六", "age": 40, "email": "4.qq.com", "address": "北京海淀"}
指定id
// 无则插入,有则覆盖(覆盖的逻辑是先删除,再插入)
PUT /<target>/_doc/<_id>
// 无则插入,有则覆盖
POST /<target>/_doc/<_id>
// 无则插入,有则报错
PUT /<target>/_create/<_id>
// 无则插入,有则报错
POST /<target>/_create/<_id>
不指定id
// 正常插入
POST /<target>/_doc
// 报错
POST /<target>/_create
// 报错
PUT /<target>/_create
// 报错
PUT /<target>/_doc
PUT user/_doc/5
{
"name": "张三(5)",
"age": 10,
"email": "1.qq.com",
"address": "北京朝阳"
}
// 根据 id 删除
DELETE /<index>/_doc/<_id>
// 根据查询删除
POST /<target>/_delete_by_query
删除 id 为1的数据
DELETE user/_doc/1
删除全部数据
POST user/_delete_by_query
{
"query": {
"match_all": {}
}
}
// 根据 id 更新
POST /<index>/_update/<_id>
// 根据查询更新
POST /<target>/_update_by_query
将 id 为1的数据的 name 修改为张三(修改后)
POST user/_update/1
{
"doc": {
"name": "张三(修改后)"
}
}
将 id 为1,2的数据 age 修改为70
POST user/_update_by_query
{
"query": {
"ids": {
"values": [1, 2]
}
},
"script": {
"source": "ctx._source.age = 70"
}
}
索引在使用一段时间后,如果想修改索引的静态设置,比如主分片的数目,分词器等(这些设置无法直接修改),此时就可以使用索引重建
POST _reindex
{
"source": {
"index": "my-index-000001"
},
"dest": {
"index": "my-new-index-000001"
}
}
当进行并发控制时通常有乐观锁和悲观锁两种方式:
乐观锁:适用于读多写少的情况,冲突比较少,可以提高系统的吞吐量
悲观锁:适用于读少写多的情况,经常会产生冲突,如果使用乐观锁,应用会不断的重试,会降低性能
ElasticSearch 使用乐观锁的形式来进行并发控制,即 if_primary_term 参数和 if_seq_no 参数
参数 | 作用 |
---|---|
if_primary_term | 数据在哪个分片 |
if_seq_no | 版本号,每次修改都会增加 |
插入一条数据
POST user/_doc/5
{
"name": "张三(5)",
"age": 10,
"email": "1.qq.com",
"address": "北京朝阳"
}
{
"_index" : "user",
"_id" : "5",
"_version" : 12,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 38,
"_primary_term" : 2
}
查询插入的数据
GET user/_doc/5
{
"_index" : "user",
"_id" : "5",
"_version" : 12,
"_seq_no" : 38,
"_primary_term" : 2,
"found" : true,
"_source" : {
"name" : "张三(5)",
"age" : 10,
"email" : "1.qq.com",
"address" : "北京朝阳"
}
}
当 _seq_no=38 时,执行如下请求报错,加if_seq_no改为38时正常执行
POST user/_doc/5?if_primary_term=2&if_seq_no=30
{
"name": "张三(5)",
"age": 10,
"email": "1.qq.com",
"address": "北京朝阳"
}
创建,更新,删除文档等操作的 api 都可以使用这2个参数
批量操作对json有严格的要求,每个json串不能换行,只能放在同一行,相邻的json串之间必须要有换行。每个操作必须是一对json串(delete语法除外)
{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body }
操作类型 | 介绍 |
---|---|
create | 文档id不存在则创建,不存在则报错 |
index | 文档id不存在则创建,存在则更新文档 |
update | 根据文档id更新文档,不存在则返回错误 |
delete | 根据文档id删除文档,不存在则返回错误 |
批量新增
POST user/_bulk
{"index": {"_id": 1}}
{"name": "张三", "age": 10, "email": "1.qq.com", "address": "北京朝阳"}
{"index": {"_id": 2}}
{"name": "李四", "age": 20, "email": "2.qq.com", "address": "北京西城"}
{"index": {"_id": 3}}
{"name": "王五", "age": 30, "email": "3.qq.com", "address": "北京东城"}
{"index": {"_id": 4}}
{"name": "赵六", "age": 40, "email": "4.qq.com", "address": "北京海淀"}
官方文档
[0]https://www.elastic.co/guide/en/elasticsearch/reference/8.11/docs.html
[1]https://blog.csdn.net/weixin_39723544/article/details/109237175
[2]https://cloud.tencent.com/developer/article/2133017
大佬博客
[3]https://elasticstack.blog.csdn.net/article/details/128835177
插入方式的区别
[4]https://www.modb.pro/db/1717735427791724544
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。