@@注意:本篇笔记使用的ElasticSearch7.5

1.添加

1.1 单条添加

1.不指定ID添加
curl -XPOST "http://elasticsearch:9200/test_db/_doc" -H 'Content-Type: application/json' -d'{
"name":"张三",
"age":18,
"score":81.5,
"birth":"1992-04-12",
"isBoy":"true",
"home":"北京天安门鼓楼大街103号",
"likes":[
"旅游",
"玩游戏"
]
}'
2.指定ID添加
# 指定id=2
curl -XPOST "http://elasticsearch:9200/test_db/_doc/2" -H 'Content-Type: application/json' -d'{
"name":"李四",
"age":28,
"score":75,
"birth":"1991-03-18",
"isBoy":"true",
"home":"安徽省合肥市瑶海区中兴街143号",
"likes":[
"吃饭",
"喝酒"
]
}'

1.2 批量添加

ElasticSearch中批量操作,是通过_bulk操作。具体语法整理如下:

# 批量添加语法
{"create":{"_type":"_doc"}} # 不指定ID
{...}
{"create":{"_type":"_doc","_id":1}} #指定ID
{...}

官网文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html

1.不指定ID
image-20210318143733115
2.指定ID
image-20210318144156811
curl -XPOST "http://elasticsearch:9200/test_db/_bulk" -H 'Content-Type: application/json' -d'{"create":{"_type":"_doc","_id":12}}{"name":"王大夫","age":33,"score":30,"birth":"1980-12-13","isBoy":"true","home":"河南省莆田系中山医院正骨科10层","likes":["吸烟","烫头"]}{"create":{"_type":"_doc","_id":13}}{"name":"将太白","age":47,"score":88,"birth":"1979-09-22","isBoy":"true","home":"湖南省东佃风场找不到大街808号","likes":["喝酒","吃肉"]}'

2.修改

2.1 单条修改

1.根据ID修改全部字段

image-20210318144914767

# 覆盖id=2的所有字段内容
curl -XPOST "http://elasticsearch:9200/test_db/_doc/2" -H 'Content-Type: application/json' -d'{
"name":"李四2",
"age":38,
"score":70,
"birth":"1991-03-14",
"isBoy": "true",
"home":"安徽省合肥市瑶海区中兴街143090号",
"likes":["吃肉","喝酒"]
}'
2.根据ID修改部分字段

image-20210318145331969

curl -XPOST "http://elasticsearch:9200/test_db/_doc/12/_update" -H 'Content-Type: application/json' -d'{
"doc":{
"name":"王大夫-修改"
}
}'
3.根据其他条件修改

image-20210318150744149

# 修改name=西施记录的 home字段信息
curl -XPOST "http://elasticsearch:9200/test_db/_update_by_query" -H 'Content-Type: application/json' -d'{
"query":{
"match":{
"name":"西施"
}
},
"script":{
"source":"ctx._source.home = \"春秋末期越国美人大街2号\""
}
}'

2.2 批量修改

# 批量修改语法
{"update": {"_id": "1"} }
{"doc":{"key":"value"}}
{"update":{"_id": "2"}}
{"doc":{"key":"value"}}
image-20210318152533259

3.删除

3.1 单条删除

1. 删除指定ID的记录
# 删除ID=1的记录
curl -XDELETE "http://elasticsearch:9200/test_db/_doc/1"
2. 删除满足条件的记录

image-20210318160122802

#  删除 name=赵东 的所有记录
curl -XPOST "http://elasticsearch:9200/test_db/_delete_by_query" -H 'Content-Type: application/json' -d'{
"query":{
"match":{
"name":"赵东"
}
}
}'

3.2 批量删除

# 批量修改语法
{"delete":{"_id":"12"}}
{"delete":{"_id":"13"}}

image-20210318160422042