elasticsearch简单操作(一)

1、增加记录

例如1:向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。

curl -X POST  'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'

  系统提示如下错误:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

  

需要制定插入数据格式:如下

curl -H "Content-Type: application/json" -X POST  'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'

系统提示:

{"_index":"accounts","_type":"person","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

  说明插入成功

例如2:

curl -H "Content-Type: application/json" -X PUT  'localhost:9200/accounts/person/4' -d '{"user":"李四","title": "软件工程师","desc":"系理集成"}'

  系统提示:

{"_index":"accounts","_type":"person","_id":"4","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

说明插入成功。

可以使用PUT、POST插入数据,需要制定插入数据的数据格式,用 -H "Content-Type: application/json"制定为json格式。

2、数据查询

curl 'localhost:9200/accounts/person/4?pretty'

系统返回:

{
  "_index" : "accounts",
  "_type" : "person",
  "_id" : "4",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "user" : "李四",
    "title" : "软件工程师",
    "desc" : "系理集成"
  }
}

说明查询成功。如果查询一个不存在id的记录,如命令:

curl 'localhost:9200/accounts/person/5?pretty'

id等于5的person因为没有对应的记录,所以,系统返回:

{
  "_index" : "accounts",
  "_type" : "person",
  "_id" : "5",
  "found" : false
}

3、删除记录

删除记录就是发出 DELETE 请求。

原文地址:https://www.cnblogs.com/dyh004/p/8917121.html