kibana开发工具语法
1、创建index和type
POST /dmfun/user/ { "settings": { "index": { "number_of_shards": "1", "number_of_replicas": "1" } }, "mappings": { "type_name": { "properties": { "uid": { "type": "integer" }, "name": { "analyzer": "standard", "boost": 2, "type": "text" }, "address": { "type": "text" }, "age": { "type": "integer" }, "sex": { "type": "integer" }, "createdate": { "type": "date" } } } } }
2、搜索索引
搜索主键
GET /dmfun/user/1
搜索全部
GET /dmfun/user/_search { "query": { "match_all": {} } }
搜索指定字段和排序
POST /dmfun/user/_search { "query": { "match_all": {} }, "_source" : ["uid","name","address","createdata"], "sort":[ { "uid":{"order":"asc"} } ] }
搜索数字条件,分页
GET /dmfun/user/_search { "query": { "range":{ "age":{"from":10,"to":20} } }, "from":0, "size":10 }
模糊搜索1(operator:分词后的关联)
GET /dmfun/user/_search { "query": { "match": { "name": { "query": "张改", "fuzziness": "AUTO", "operator": "and" } } }, "from":0, "size":10 }
模糊搜索2
GET /dmfun/user/_search { "query": { "multi_match": { "fields": [ "name", "address" ], "query": "软件园", "fuzziness": "AUTO" } }, "from":0, "size":10 }
聚合查询
指标聚合,类似count
POST /dmfun/user/_search?size=0 { "aggs": { "maxage": { "max": { "field": "age" } } } }
桶聚合,类似group by
POST /dmfun/user/_search?size=0 { "aggs":{ "group_by_age":{ "terms":{ "field":"age" } } } }
3、插入数据
单笔插入
POST /dmfun/user/ {"uid":1,"name":"张三","address":"01号","age":15,"sex":1,"createdt":"2020-12-13"}
批量插入数据
POST /dmfun/user/_bulk {"index":{"_index":"dmfun","_type":"user","_id":"3"}} {"uid":3,"name":"王五","address":"05号","age":19,"sex":0,"createdt":"2020-12-14"} {"index":{"_index":"dmfun","_type":"user","_id":"4"}} {"uid":4,"name":"赵六","address":"101号","age":25,"sex":1,"createdt":"2020-12-15"} {"index":{"_index":"dmfun","_type":"user","_id":"5"}} {"uid":5,"name":"钱七","address":"29号","age":27,"sex":0,"createdt":"2020-12-16"}
4、更新数据
更新数据
//POST /{INDEX}/{TYPE}/{_ID}/_update POST /dmfun/user/nXuy_3YBrSPXZR_vqYwc/_update {"doc":{"name":"张三改","address":"软件园路01号"}}
批量更新数据
POST _bulk {"update":{"_index":"dmfun","_type":"user","_id":"3","retry_on_conflict":3}} {"doc":{"name":"王五改","address":"软件园路05号"}} {"update":{"_index":"dmfun","_type":"user","_id":"4"}} {"doc":{"name":"赵六改","address":"软件园路101号"}}
批量更新和插入
POST _bulk {"update":{"_index":"dmfun","_type":"user","_id":"3"}} {"doc":{"name":"王五改2","address":"软件园路05号"}} {"update":{"_index":"dmfun","_type":"user","_id":"4"}} {"doc":{"name":"赵六改2","address":"软件园路101号"}} {"index":{"_index":"dmfun","_type":"user","_id":"7"}} {"uid":7,"name":"李九九","address":"999号","age":27,"sex":0,"createdt":"2020-12-16"}
5、删除数据
单笔删除
DELETE /dmfun/user/8/
批量删除
POST _bulk {"delete":{"_index":"dmfun","_type":"user","_id":"7","retry_on_conflict":3}}
其他
_bulk总结
bulk的格式:
{action:{metadata}} {requstbody}
url:
(1) /_bulk (2)/{index}/_bulk (3)/{index}/{type}/_bulk
action:(行为)
• create:文档不存在时创建 • update:更新文档 • index:创建新文档或替换已有文档 • delete:删除一个文档
metadata:
_index,_type,_id
requstbody:
(1)index 和 create 第二行是source数据体 (2)delete 没有第二行 (3)update 第二行可以是partial doc,upsert或者是script
requstbody内容:
(1)doc (2)upsert (3)doc_as_upsert (4)script (5)params ,lang ,source
//案例
POST _bulk { "update" : {"_id" : "1", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} } { "doc" : {"field" : "value"} } { "update" : { "_id" : "0", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} } { "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}} { "update" : {"_id" : "2", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} } { "doc" : {"field" : "value"}, "doc_as_upsert" : true } { "update" : {"_id" : "3", "_type" : "_doc", "_index" : "index1", "_source" : true} } { "doc" : {"field" : "value"} } { "update" : {"_id" : "4", "_type" : "_doc", "_index" : "index1"} } { "doc" : {"field" : "value"}, "_source": true}
操作状态码
CREATE201 创建成功
UPDATE
200 更新成功
201 创建成功(UPSERT)
400 更新失败(比如需要更新的记录不存在)
DELETE
200
指定版本后,版本冲突
409