通过HTTP RESTful API 操作elasticsearch文档

Elasticsearch提供近乎实时的数据操作和搜索能力。
当创建、修改、删除索引中数据后,检索数据后能有一秒左右的延迟。索引elasticsearch 是不同于像其他平台的SQL,一个事务操作结束后,可立即获取检索到的数据。

索引或替换文档

  • 索引文档:
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'
结果:
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "created" : true
}
  • 替换文档:
    当我们索引文档时,使用已经存在的ID,那就将会替换该文档
 curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
 {
   "name": "John Doe"
 }'
结果:
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "created" : false
}

_version:2,版本号递增了
created:false,表示索引文档失败了,换句话说就是替换文档了

  • 使用elasticsearch内置递增ID:
    When indexing, the ID part is optional. If not specified, Elasticsearch will generate a random ID and then use it to index the document. The actual ID Elasticsearch generates (or whatever we specified explicitly in the previous examples) is returned as part of the index API call.
curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
  "name": "Jane Doe"
}'
结果:
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "AVAcT81d1wMGS9JQLaGH",
  "_version" : 1,
  "created" : true
}

如果不指定ID,我们就要使用-XPOST,类似于web开发 提交一个post请求,而put是发起一个请求

 curl -XGET 'localhost:9200/customer?pretty' -d '
 "query":{
  "match_all":{}
  }
 }'
结果:
{
  "customer" : {
    "aliases" : { },
    "mappings" : {
      "external" : {
        "properties" : {
          "name" : {
            "type" : "string"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1443583562200",
        "uuid" : "ylh9eJSkQ0iyArY8LoRkxQ",
        "number_of_replicas" : "1",
        "number_of_shards" : "5",
        "version" : {
          "created" : "1070299"
        }
      }
    },
    "warmers" : { }
  }
}

更新文档

elasticsearch 并不是做那种类似关系型数据那种更新操作(update external set name =’stark_summer’ where name=’Jane Doe’),而是一气呵成完成删除索引文档,然后重新创建新的索引文档

  • 更新文档ID=1,修改文档中字段:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe" }
}'
结果:
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3
}
  • 更新文档ID=1,修改文档中字段并增加age字段:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'
结果:
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4
}
  • 使用简单脚本更新文档
    在1.4.3版本,动态脚本操作默认是禁止的,了解详细信息看这个链接
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "script" : "ctx._source.age += 5"
}'

ctx._source 是指你当前要更新文档的源文档


删除文档

  • 指定文档ID
curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  • 匹配检索调节多文档删除
curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '
{
  "query": { "match": { "name": "John" } }
}'
结果:
{
  "_indices" : {
    "customer" : {
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      }
    }
  }
}

URI更改为/ _query表示delete-by-query API,满足匹配删除查询条件,但我们仍使用删除动词。


批处理

除了创建、更新、删除索引文档,elasticsearch还支持批量处理创建、更新、删除索引文档,API是_bulk,这是一个高效机制处理批量操作

  • 批量索引文档 (ID 1 - John Doe and ID 2 - Jane Doe)
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
结果:
{
  "took" : 36,
  "errors" : false,
  "items" : [ {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "1",
      "_version" : 5,
      "status" : 200
    }
  }, {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "2",
      "_version" : 1,
      "status" : 201
    }
  } ]
}
  • 批量更新文档(ID of 1) & 删除文档 (ID of 2):
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
结果:
{
  "took" : 84,
  "errors" : false,
  "items" : [ {
    "update" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "1",
      "_version" : 6,
      "status" : 200
    }
  }, {
    "delete" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "2",
      "_version" : 2,
      "status" : 200,
      "found" : true
    }
  } ]
}

注意上面的删除操作,没有相应的源文档后,因为删除只需要删除文档的ID。
bulk API 批量执行是循序地、有序地。无论什么原因单个动作失败了,仍然会继续执行剩下的动作。所有执行完毕,后返回每个动作的状态,方便排查问题。


尊重原创,拒绝转载
http://blog.csdn.net/stark_summer/article/details/48827595

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值