ElasticSearch API

 

索引CRUD

添加           allowed: [PUT, DELETE, HEAD, GET]
PUT /lib/
{
    "settings":{
        "index":{
            "number_of_shards": 5,
            "number_of_replicas": 1
        }
    }
}
删除
DELETE /lib # /不严格

查看:
GET /lib/_settings
GET _all/_settings
判断是否存在
HEAD /lib

 

一个索引库下,只有一个类型type   ?

 

 

文档CURD

创建:
POST /lib/user/
{
    "first_name" : "Douglas",
    "last_name" : "Fir",
    "age" : 23,
    "about": "I like to build cabinets",
    "interests": [ "forestry" ]
}
修改,(覆盖更新)
PUT /lib/user/1
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
更新某个字段
POST /lib/user/1/_update
{
  "doc":{
    "age":33
    }
}
删除
DELETE /lib/user/1

 

查看
GET /lib/user/1

GET /lib/user/

GET /lib/user/1?_source=age,interests
批量获取
GET /_mget
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":1,
    "_source":["age"]
} ] }
{
  "docs" : [
    {
      "_index" : "lib",
      "_type" : "user",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 5,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {"age" : 32,
      }
    }
  ]
}
获取同索引同类型下的不同文档:
GET /lib/user/_mget

{
    "docs":[
       {
           "_id": 1
       },
       {"_id": 2,
       }
     ]
}
GET /lib/user/_mget
{
   "ids": ["1","2"]
}

 

 批量操作

或者将数据写入一个文件
curl -H 'Content-Type: application/json' -XPOST 'http://192.168.81.131:9200/_bulk' --data-binary @requests;
 
{action:{metadata}}\n
{requstbody}\n
action:(行为)
  create:文档不存在时创建
  update:更新文档
  index:创建新文档或替换已有文档
  delete:删除一个文档   ,不需要   requestbody
metadata:_index,_type,_id
  
create 和index的区别

如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。
实例: 
{
"delete":{"_index":"lib","_type":"user","_id":"1"} }
批量添加
POST
/lib2/books/_bulk {"index":{"_id":1}} {"title":"Java","price":55} {"index":{"_id":2}} {"title":"Html5","price":45} {"index":{"_id":3}} {"title":"Php","price":35} {"index":{"_id":4}} {"title":"Python","price":50}

 

删除:没有请求体
POST /lib2/books/_bulk
{"delete":{"_index":"lib2","_type":"books","_id":4}}
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"tt","_type":"ttt"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":"4"}}
{"doc":{"price":58}}

 

posted @ 2019-06-16 20:37  慕沁  阅读(159)  评论(0)    收藏  举报