es-DSL-document操作相关
1、索引一个文档
PUT /<index>/_doc/<_id> 指定_id值
POST /<index>/_doc/ 自动生成一个_id值
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>
id可以指定,也可以不提供,不提供时,es自动生成一个随机字符串作为id,如果是调试的话建议指定一个方便分析
{ "user_id": 1001, "name": "张三", "content": "张三 is very 666!", "create_date": "2022-01-01" }
批量添加:
POST /test001/_doc/_bulk {"index":{"_id":1}} \\行为:索引信息 {"name":"张三","age",55} \\请求体 {"index":{"_id":2}} {"name":"李四","age",45} {"index":{"_id":3}} {"name":"王五","age",35} {"index":{"_id":4}} {"name":"赵六","age",25}
2、更新一个文档
局部字段更新,要使用到doc属性,name是字段名
POST /<index>/_update/<_id>
POST /test001/_update/1 { "doc": { "title": "new_title_name" } }
更新整个文档(底层是生成一个新文档替换了旧文档)
PUT /test001/_doc/1 { "title": "My first blog entry2", "text": "I am starting to get the hang of this...", "date": "2014/01/02" }
doc as upsert,文档不存在时,将doc属性里的内容insert成为一个文档
POST /test001/_update/1 { "doc": { "title": "new_title_name" }, "doc_as_upsert": true }
3、删除一个文档
DELETE /test001/_doc/1
4、一个综合的bulk的例子
POST /test001/_bulk { "index" : { "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_id" : "2" } } { "create" : { "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1"} } { "doc" : {"field2" : "value2"} }