Elasticsearch 版本:5.4
Elasticsearch疾速入门 第1篇:Elasticsearch入门
Elasticsearch疾速入门 第2篇:Elasticsearch和Kibana装置
Elasticsearch疾速入门 第3篇:Elasticsearch索引和文档操纵
Elasticsearch疾速入门 第4篇:Elasticsearch文档查询
列出一切索引
GET /_cat/indices?v
返回内容以下:
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 的索引,然后再次列出一切的索引
PUT /customer?pretty GET /_cat/indices?v
实行第一行返回以下内容,这里我们运用PUT谓词竖立了一个名叫 customer 的索引,在背面跟上 pretty 示意假如有数据返回的话,用格式化后的JSON返回数据
{ "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 。
索引和查询文档
接下来我们放一些东西到 customer 索引中。之前提过的,为了索引某个文档,我们必需通知 Elasticsearch ,该文档应当属于该索引的哪一个范例,下面我们索引一个简朴的文档到 customer 索引,范例名称为 external , 而且ID为1
PUT /customer/external/1?pretty { "name": "John Doe"}
返回内容以下:
{ "_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将自动竖立该索引。
再来看下我们方才索引的文档
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/1DELETE /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返回效果时,每一个操纵都邑供应状况(和接收到的递次一致),你可以经由过程这个状况搜检操纵是不是实行胜利。
总结
简朴的索引操纵
1、检察集群中的索引, GET /_cat/indices?v
2、竖立索引 PUT /product?pretty 。(es会自动竖立index和type,不须要提早竖立,而且es默许会对document每一个field都竖立倒排索引,让其可以被搜刮)
3、删除索引, DELETE /test_index?pretty
文档的CRUD操纵
1、新增商品
PUT /product/goods/1{"goods_id": "10","goods_name": "索爱C702c","createTime": "2016-12-21","goods_type": ["华为","乐视","小米"] }
2、查询商品, GET /product/goods/1
3、修正商品
体式格局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手机" } }
比较竖立,更新,替代文档返回效果:
4、删除商品, DELETE /product/goods/4
以上就是Elasticsearch索引和文档操纵实例教程的细致内容,更多请关注ki4网别的相干文章!