GET /_cat/indices?v
返回内容以下:html
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb
能够看到在集群中有一个索引git
如今让咱们建立一个名叫 customer 的索引,而后再次列出全部的索引github
PUT /customer?pretty
GET /_cat/indices?v
执行第一行返回如下内容,这里咱们使用PUT谓词建立了一个名叫 customer 的索引,在后面跟上 pretty 表示若是有数据返回的话,用格式化后的JSON返回数据api
{ "acknowledged": true, "shards_acknowledged": true }
执行第二行返回如下内容,结果告诉咱们,已经建立了一个名叫 customer 的索引,它有5个主分片和1个复制分片(默认状况下是1个),在这个索引中尚未文档。网络
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb yellow open customer M8i1ZxhsQJqk7HomOA7c_Q 5 1 0 0 650b 650b
可能你已经注意到 customer 索引的健康值被标记为 yellow ,回顾咱们前面讨论的内容, yellow 表示该索引的复制分片(副本)尚未被分配。该索引出现这种状况的缘由是, Elasticsearch 默认会为该索引建立1个副本,因为此时咱们只有1个节点,那么这副本就无法被分配(为了高可用),直到之后为该集群加入了另外一个节点。一旦该副本分配到了另外一个节点,该索引的健康状态就会变成 green 。iphone
接下来咱们放一些东西到 customer 索引中。以前提过的,为了索引某个文档,咱们必须告诉 Elasticsearch ,该文档应该属于该索引的哪一个类型,下面咱们索引一个简单的文档到 customer 索引,类型名称为 external , 而且ID为1elasticsearch
PUT /customer/external/1?pretty { "name": "John Doe" }
返回内容以下:ide
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
从以上能够看出,一个新的客户文档成功被索引到 customer索引的 extenal 类型中,而且咱们在索引的时候指定文档的内部id值为1。学习
值得注意的是, Elasticsearch 不须要在你索引文档到某个索引以前,明确的建立一个索引。好比上一个例子,若是 customer索引不存在, Elasticsearch将自动建立该索引。ui
再来看下咱们刚刚索引的文档
GET /customer/external/1?pretty
返回内容以下:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true, "_source": { "name": "John Doe" } }
这里比较特殊的是found字段,它说明咱们查到了一个id为1的文档,另外一特殊的字段_source,保存了在上一个步骤索引的的文档。
如今让咱们删除刚刚已经建立的索引,并再次查看全部索引。
DELETE /customer?pretty
GET /_cat/indices?v
第一行返回内容如下:
{ "acknowledged": true }
第二行返回内容以下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb
从以上内容能够看到咱们的 customer索引已经被删除了。
在继续学习以前,让咱们快速回顾一下,本节学的API命令
PUT /customer PUT /customer/external/1 { "name": "John Doe" } GET /customer/external/1 DELETE /customer
若是仔细学习了以上命令,应该会发现 elasticsearch 访问数据所使用的模式,归纳以下:
<REST Verb> /<Index>/<Type>/<ID>
使用REST 访问模式,在全部的API命令中是十分广泛的,若是你能够简单记住它,对于掌握 Elasticsearch ,那么已经开了一个好头。
Elasticsearch 具备近实时的操做和查询数据的能力,默认状况下,从你索引,更新或者删除你的数据到用户能够搜索到新的结果这个过程大概须要1秒(基于refresh 频率)。它们和相似SQL这样的平台不同,SQL的数据在事务完成后就立刻就生效,不会有延迟。
以前已经演示了怎么索引单个文档,再来回顾一下:
PUT /customer/external/1?pretty { "name": "John Doe" }
上面的命令将会索引指定文档到 customer 索引的 external 类型,文档的id值是1。若是咱们用不一样的文档内容(或者相同)再次执行上面的命令,elasticsearch将会用一个新的文档取代旧的文档(即重建索引)。
PUT /customer/external/1?pretty { "name": "Jane Doe" }
上面的操做把id为1的文档的name字段由"john doe"改为"jane doe"。另外一方面,若是咱们使用不一样的id执行上述命令,将会建立一个新的文档,旧的文档会保持原样。
PUT /customer/external/2?pretty { "name": "Jane Doe" }
以上操做索引了一个新的id为2文档。
索引新文档的时候,id值是可选的。若是没有指定, elasticsearch 将会为文档生成一个随机的id。实际生成的id将会保存在调用api接口的返回结果中。
下面的例子展现不指定文档id的时候是如何索引文档的:
POST /customer/external?pretty { "name": "Jane Doe" }
返回内容以下:
{ "_index": "customer", "_type": "external", "_id": "AVyc9L6dtgHksqXKpTlM", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
注意,在上面的例子中,由于没有指定id,咱们须要使用POST谓词取代以前的PUT谓词。
除了能够索引和替换文档以外,咱们还能够更新文档。注意, elasticsearch 并无在原来的文档基础上进行更新,每当进行更新时, Elasticsearch 将删除旧的文档,而后索引新的文档。如下例子演示了如何更新文档,把以前ID为1的name字段改成"Jane Doe":
POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe" } }
如下例子演示了如何更新先前ID为1的文档,改变name字段为"Jane Doe" 的同时添加age字段
POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } }
也可使用简单的脚原本执行更新。如下示例使用脚本将年龄增长5:
POST /customer/external/1/_update?pretty { "script" : "ctx._source.age += 5" }
在以上例子中, ctx._source 指当前即将被更新的源文档。请注意,在撰写本文时,只能一次更新单个文档。未来, Elasticsearch 可能会提供经过查询条件(如SQL UPDATE-WHERE
语句)更新多个文档的功能。
删除文档很是简单,如下例子演示了怎么删除 customer 索引下ID为2的文档,查阅Delete By Query API 删除与特定查询匹配的全部文档。值得注意的是,直接删除整个索引比经过query api 删除全部文档更高效。
DELETE /customer/external/2?pretty
除了可以索引,更新和删除单个文档以外, Elasticsearch 也提供了使用 _bulk API 批量执行上述任何操做的功能。这个功能是很是重要的,由于它提供了一个很是有效的机制来尽量快地进行多个操做,而且尽量减小网络的往返行程。简单举个例子,下面会在一个 bulk操做中索引两个文档:
POST /customer/external/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
返回内容以下:
{ "took": 27, "errors": false, "items": [ { "index": { "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true, "status": 201 } }, { "index": { "_index": "customer", "_type": "external", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true, "status": 201 } } ] }
下面的例子会在一个操做内更新第一个文档同时删除第二个文档:
POST /customer/external/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
返回内容以下:
{ "took": 25, "errors": false, "items": [ { "update": { "_index": "customer", "_type": "external", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "status": 200 } }, { "delete": { "found": true, "_index": "customer", "_type": "external", "_id": "2", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "status": 200 } } ] }
注意以上的删除操做,在它以后并无相应的源文档,由于只须要文档的ID就能删除。
若是某个操做因某些缘由执行失败,不会影响后面的操做,它会继续执行剩下的操做。api返回结果时,每个操做都会提供状态(和接收到的顺序一致),你能够经过这个状态检查操做是否执行成功。
一、查看集群中的索引, GET /_cat/indices?v
二、建立索引 PUT /product?pretty 。(es会自动创建index和type,不须要提早建立,并且es默认会对document每一个field都创建倒排索引,让其能够被搜索)
三、删除索引, DELETE /test_index?pretty
一、新增商品
PUT /product/goods/1 { "goods_id": "10", "goods_name": "索爱C702c", "createTime": "2016-12-21", "goods_type": [ "华为", "乐视", "小米" ] }
二、查询商品, GET /product/goods/1
三、修改商品
方式1:替换文档(和建立同样,全部字段必须写全)
PUT /product/goods/4 { "goods_id": "40", "goods_name": "联想笔记本", "createTime": "2017-05-21", "goods_type": [ "电脑" ] }
字段不写全的状况
方式2:更新文档
POST /product/goods/1/_update { "doc":{ "goods_name":"iphone手机" } }
比较建立,更新,替换文档返回结果:
四、删除商品, DELETE /product/goods/4
官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_cluster.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html
参考文档
https://github.com/13428282016/elasticsearch-CN/wiki/es-gettting-started