_id =1 新增一条.
此命令重复执行,就是更新id=1的数据
POST employee_zcy/_doc/1
{
"uid" : "1234",
"phone":"12345678909",
"message" : "qq",
"msgcode" : "1",
"sendtime":"2019-03-14 01:57:04",
"saymessage":"zcy11111111111"
}
_id是随机的字符串
此命令重复执行,就是不断新增
POST projecy_zcy/_doc
{
"projectCode":"31000012",
"projectName":"xxxx12"
}
下面的示例不指定id,则返回的数据中id是随机的字符串;
更新整个文档, 和新增一样
POST employee_zcy/_doc/1
{
"uid" : "1234",
"phone":"12345678909",
"message" : "qq",
"msgcode" : "1",
"sendtime":"2019-03-14 01:57:04",
"saymessage":"zcy11111111111"
}
POST employee_zcy/_doc/1
{
"uid" : "1234567"
}
那么文档1的内容就:
{
"uid" : "1234567"
}
POST employee_zcy/_update/3
{
"doc" : {
"message":"qq456222",
"age":13
}
}
修改了id是3的 message,
结果:
修改了主键=3的中message 为 qq456111, 新增了age字段
POST projecy_zcy/_update/10
{
"script": {
"source": "ctx._source.projectCode=1234567;ctx._source.projectPerson+=params.personName ",
"params": {
"personName":"zcy"
}
}
}
修改_id=10的, projectCode的值, 新增projectPerson的,可以用参数变量进行字符处理
POST projecy_zcy/_update/4
{
"script": {
"source": "ctx._source['message'] = '123'; ctx._source.age=14"
}
}
POST projecy_zcy/_update/10
{
"script": {
"source": "ctx._source.remove('projectPerson')"
}
}
POST employee_zcy/_update_by_query
{
"query": {
"term": {
"phone": "12345678909"
}
} ,
"script": {
"source": "ctx._source['message'] = '1234566666'"
}
}
-- 搜索projectName like 管理域 , 增加flag值
POST projecy_zcy/_update_by_query
{
"query": {
"match": {
"projectName": "管理域"
}
},
"script":{
"source":"ctx._source.flag='abc'"
}
}
此id,不存在,会报错
POST projecy_zcy/_update/16
{
"script": {
"source": "ctx._source.projectCode=1234567 ",
"params": {
"personName":"zcy"
}
}
}
报错:
{
"type" : "document_missing_exception",
"reason" : "[_doc][16]: document missing",
"index_uuid" : "P--CS2RRStmmvjoicjFnmw",
"shard" : "0",
"index" : "projecy_zcy"
如何不报错? 我们可以使用 upsert
参数,指定如果文档不存在就应该先创建它
POST projecy_zcy/_update/15
{
"script": {
"source": "ctx._source.projectCode=1234567;ctx._source.projectPerson+=params.personName ",
"params": {
"personName":"zcy"
}
},
"upsert": {"projectCode":234,"projectPerson":"345"}
}
id=15 不存在, 先初始化projectCode, projectPerson
第一次执行:
{
"_index" : "projecy_zcy",
"_type" : "_doc",
"_id" : "15",
"_score" : 1.0,
"_source" : {
"projectCode" : 234,
"projectPerson" : "345"
}
}
第二次执行:
{
"_index" : "projecy_zcy",
"_type" : "_doc",
"_id" : "15",
"_score" : 1.0,
"_source" : {
"projectCode" : 1234567,
"projectPerson" : "345zcy"
}
}
不好报错, 没有任何数据更新, 和关系数据库一样
POST projecy_zcy/_update/15
{
"script": {
"source": "ctx._source.projectCode=1234567;ctx._source.projectPerson+=params.personName ",
"params": {
"personName":"zcy"
}
},
"upsert": {"projectCode":234,"projectPerson":"345"}
}
优化:
1.需要先将脚本post到es的库中,新建zcy_message,脚本名称
POST _scripts/zcy_script1
{
"script":{
"lang":"painless",
"source": "ctx._source.projectCode=1234567;ctx._source.projectPerson+=params.personName "
}
}
lang: 必须有,否则报错
params: 不需要放, 放了也不生效
2.根据id来调用
POST projecy_zcy/_update/15
{
"script": {
"id": "zcy_script1",
"params": {
"personName":"aaa"
}
},
"upsert": {"projectCode":234,"projectPerson":"345"}
}
DELETE employee_zcy/_doc/1
注意_doc 就是type的值, 插数据时候指定的
POST employee_zcy/_doc/1
2>根据条件删除数据
POST employee_zcy/_delete_by_query
{
"query":{
"term":{
"phone":"12345678909"
}
}
}
其实可以看得出来index是比较常用的。还有bulk的操作,某一个操作失败,是不会影响其他文档的操作的,它会在返回结果中告诉你失败的详细的原因。
POST projecy_zcy/_bulk
{"index":{"_id":1}}
{"id":1,"name":"admin","counter":"10","tags":["red","black"]}
{"index":{"_id":2}}
{"id":2,"name":"zcy"}
POST projecy_zcy/_bulk
{"update":{"_id":1}}
{"script":{"source":"ctx._source.name='www'"}}
{"update":{"_id":1}}
{"script":{"source":"ctx._source.name='zzzz'"}}
POST projecy_zcy/_bulk
{"delete":{"_id":2}}
{"delete":{"_id":1}}