ElasticSearch学习(二)--ElasticSearch在curl中的基本操作

开始第一步

让我们建立一个员工目录

我们首先要做的是存储员工数据,每个文档(document)(行)代表一个员工。所以为了创建员工目录,我们将进行如下操作:

  • 为每个员工的文档(document)建立索引,每个文档包含了相应员工的所有信息。
  • 每个文档的类型为employee
  • employee类型归属于索引megacorp
  • megacorp索引存储在ElasticSearch集群中。

实际上这些都是很容易的,我们通过一个命令执行完成操作。

curl -XPUT "localhost:9200/megacorp/employee/1?pretty" -d '{
   "first_name":"John",
   "last_name":"Smith",
   "age":25,
   "about":"I love to go rock climbing",
   "interests":["sports","music"]
}'

注:在Windows下命令如下:

curl -H "application/json" -XPUT "localhost:9200/megacorp/employee/1?pretty" -d "{
  \ "first_name\":\"John\",
   \"last_name\":\"Smith\",
  \ "age\":25,
   \"about\":\"I love to go rock climbing\",
  \ "interests\":[\"sports\",\"music\"]
}"
自增ID
curl -XPOST "localhost:9200/website/blog/" -d '{
    "title" : "My first blog entry",
    "text" : "Just trying this out...",
    "date" : "2014/01/01"
}'

响应如下:

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "X7cGE2UB2XL2Aw5Pme58",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
检索文档
curl -XGET "localhost:9200/website/blog/123?pretty"

响应如下

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "My first blog entry",
    "text" : "Just trying this out...",
    "date" : "2014/01/01"
  }
}
检索文档的一部分

通常,GET请求将返回文档的全部,存储在_source参数中。但是你可能感兴趣的字段只是title。请求个别字段可以使用_source参数。多个字段可以使用逗号分隔:

curl -i -XGET "localhost:9200/website/blog/123?_source=title,text&pretty"

_source字段现在只包含我们请求的字段,而且过滤了date字段:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 197

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "text" : "Just trying this out...",
    "title" : "My first blog entry"
  }
}

或者你只想得到_source字段而不要其他的元数据,你可以这样请求:

curl -i -XGET "localhost:9200/website/blog/123/_source?pretty"

它仅仅返回:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 101

{
  "title" : "My first blog entry",
  "text" : "Just trying this out...",
  "date" : "2014/01/01"
}
检查文档是否存在

如果你想做的只是检查文档是否存在——-你对内容完全不感兴趣,那就使用HEAD方法代替GET,HEAD请求不会返回响应体。只有HTTP头:

curl -i -XHEAD "localhost:9200/website/blog/123?pretty"

ElasticSearch将会返回 200OK 状态(如果你的文档存在):

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 224

如果不存在返回 404 Not Found:

HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 83

当然,这只代表你在查询的那一刻文档不存在,但并不带表几毫秒后依旧不存在。另一进程在这期间可能创建新文档。

更新整个文档

文档在ElasticSearch中是不可变的——–我们不能修改他们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值