Elasticsearch Version: 5.4
Elasticsearch Quick Start Part 1: Getting Started with Elasticsearch
Elasticsearch Quick Start Part 1 2 articles: Elasticsearch and Kibana installation
Elasticsearch quick start article 3: Elasticsearch index and document operations
Elasticsearch quick start article 4: Elasticsearch document query
Course Recommendation→:"Elasticsearch Full Text Search Practical Combat" (Practical Video)
From the course "Concurrency Solution for Tens of Millions of Data (Theoretical + Practical Combat)"
GET /_cat/indices?v
The returned content is as follows:
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
You can see that there is an index in the cluster
Now let us create an index named customer and then list all the indexes again
PUT /customer?pretty GET /_cat/indices?v
Executing the first line returns the following, here we use the PUT predicate An index named customer is created, followed by pretty to indicate that if there is data to return, use formatted JSON to return the data
{ "acknowledged": true, "shards_acknowledged": true}
Executing the second line returns the following content. The result tells us that an index named customer has been created. It has 5 primary shards and 1 replica shard (by default 1), there are no documents in this index yet.
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
Maybe you have noticed that the health value of the customer index is marked as yellow. Looking back at what we discussed earlier, yellow Indicates that the replicated shards (copies) of the index have not been allocated. The reason for this situation in the index is that Elasticsearch will create a copy of the index by default. Since we only have one node at this time, this copy cannot be allocated (for high availability). Until another node is added to the cluster later. Once the replica is assigned to another node, the health status of the index will become green .
Next we put something into the customer index. As mentioned before, in order to index a document, we must tell Elasticsearch which type of the index the document should belong to. Below we index a simple document to the customer index, type The name is external , and the ID is 1
PUT /customer/external/1?pretty { "name": "John Doe"}
The returned content is as follows:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "result": "created", "_shards": {"total": 2,"successful": 1,"failed": 0 }, "created": true}
As can be seen from the above, A new customer document is successfully indexed into the extenal type of the customer index, and we specify the internal id value of the document as 1 when indexing.
It is worth noting that Elasticsearch does not need to explicitly create an index before you index documents into an index. For example, in the previous example, if the customer index does not exist, Elasticsearch will automatically create the index.
Let’s take a look at the document we just indexed
GET /customer/external/1?pretty
The returned content is as follows:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true, "_source": {"name": "John Doe" } }
The special thing here is found field, which indicates that we found a document with an id of 1, and another special field, _source, which saves the document indexed in the previous step.
Now let’s delete the index we just created and view all indexes again.
DELETE /customer?pretty GET /_cat/indices?v
The first line returns the following content:
{ "acknowledged": true}
The second line returns the following content:
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
You can see from the above that our customer index has been deleted.
Before continuing to study, let us quickly review the API commands learned in this section
PUT /customer PUT /customer/external/1{ "name": "John Doe"} GET /customer/external/1DELETE /customer
If you study the above commands carefully, you should find elasticsearch The mode used to access data is summarized as follows:
<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
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
The above is the detailed content of Elasticsearch index and document operation example tutorial. For more information, please follow other related articles on the PHP Chinese website!