2-Rest API

常见的两种交互方式

  • curl 命令行
[es@c1 kibana]$ curl -XPUT 'http://192.168.88.1:9200/employee/doc/1' \
> -i -H "Content-Type:application/json" -d '
> {
> "username": "heboan",
> "job": "sa"
> }
> '
  • Kibana DevTools
PUT /employee/doc/1
{
  "username": "heboan",
  "job": "sa"
}

索引API

//创建索引
PUT /test_index

//查看现有索引
GET _cat/indices
    yellow open employee   cRPbOGJyTEu1HEPGgdazOQ 5 1 1 0 4.8kb 4.8kb
    yellow open test_index _AqMb8EXTteDsazWSpqX5w 5 1 0 0 1.1kb 1.1kb

//删除索引
DELETE /test_index

文档API

# 创建文档
# test_index是索引名,doc是type(以后用doc就可以了),1是id
# 创建文档时索引不存在,es会自动创建并指定type,若没有指定id,es会自动分配
PUT /test_index/doc/1
{
  "username": "heboan",
  "age": 28
}

# 查询文档
GET /test_index/doc/1  //_source存储了文档完整的原始数据
//搜索所有文档,找出id为1的文档
GET /test_index/doc/_search
{
  "query": {
    "term": {
      "_id": "1"
    }
  }
}

#es允许一次创建多个文档,从而减少网络传输开销,提升写入速率
POST _bulk
{"index":{"_index":"text_index", "_type":"doc", "_id":"3"}}   //index和create区别是当索引存在index会覆盖,而create会报错
{"username":"alfred", "age":10}
{"delete":{"_index":"test_index","_type":"doc","_id":"1"}}
{"update":{"_id":"2","_index":"test_index","_type":"doc"}}
{"doc":{"age":"20"}}


#es允许一次查询多个文档
GET /_mget
{
  "docs": [
    {
    "_index": "test_index",
    "_type": "doc",
    "_id": "1"
    },
    {
      "_index": "test_index",
      "_type": "doc",
      "_id": "3"
    }
  ]
}

猜你喜欢

转载自www.cnblogs.com/sellsa/p/9228911.html