Elasticsearch index and document operation example tutorial

零下一度
Release: 2022-05-13 11:06:11
Original
3397 people have browsed it
  • 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)"

List all indexes

GET /_cat/indices?v
Copy after login

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
Copy after login
Copy after login

You can see that there is an index in the cluster

Create index

Now let us create an index named customer and then list all the indexes again

PUT /customer?pretty
GET /_cat/indices?v
Copy after login

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}
Copy after login

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
Copy after login

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 .

Indexing and querying documents

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"}
Copy after login
Copy after login

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}
Copy after login

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
Copy after login

The returned content is as follows:

{  "_index": "customer",  "_type": "external",  "_id": "1",  "_version": 1,  "found": true,  "_source": {"name": "John Doe"
  }
}
Copy after login

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.

Delete Index

Now let’s delete the index we just created and view all indexes again.

DELETE /customer?pretty
GET /_cat/indices?v
Copy after login

The first line returns the following content:

{  "acknowledged": true}
Copy after login

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
Copy after login
Copy after login

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
Copy after login

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>
Copy after login

使用REST 访问模式,在所有的API命令中是十分普遍的,如果你可以简单记住它,对于掌握 Elasticsearch ,那么已经开了一个好头。

修改数据

 Elasticsearch  具有近实时的操作和查询数据的能力,默认情况下,从你索引,更新或者删除你的数据到用户可以搜索到新的结果这个过程大概需要1秒(基于refresh 频率)。它们和类似SQL这样的平台不一样,SQL的数据在事务完成后就马上就生效,不会有延迟。

索引/替换文档

之前已经演示了怎么索引单个文档,再来回顾一下:

PUT /customer/external/1?pretty
{  "name": "John Doe"}
Copy after login
Copy after login

上面的命令将会索引指定文档到 customer 索引的 external 类型,文档的id值是1。如果我们用不同的文档内容(或者相同)再次执行上面的命令,elasticsearch将会用一个新的文档取代旧的文档(即重建索引)。

PUT /customer/external/1?pretty
{  "name": "Jane Doe"}
Copy after login

上面的操作把id为1的文档的name字段由"john doe"改成"jane doe"。另一方面,如果我们使用不同的id执行上述命令,将会创建一个新的文档,旧的文档会保持原样。

PUT /customer/external/2?pretty
{  "name": "Jane Doe"}
Copy after login

以上操作索引了一个新的id为2文档。

索引新文档的时候,id值是可选的。如果没有指定, elasticsearch 将会为文档生成一个随机的id。实际生成的id将会保存在调用api接口的返回结果中。

下面的例子展示不指定文档id的时候是如何索引文档的:

POST /customer/external?pretty
{  "name": "Jane Doe"}
Copy after login

返回内容如下:

{  "_index": "customer",  "_type": "external",  "_id": "AVyc9L6dtgHksqXKpTlM",  "_version": 1,  "result": "created",  "_shards": {"total": 2,"successful": 1,"failed": 0
  },  "created": true}
Copy after login

注意,在上面的例子中,因为没有指定id,我们需要使用POST谓词取代之前的PUT谓词。

更新文档

除了可以索引和替换文档之外,我们还可以更新文档。注意, elasticsearch 并没有在原来的文档基础上进行更新,每当进行更新时, Elasticsearch 将删除旧的文档,然后索引新的文档。以下例子演示了如何更新文档,把之前ID为1的name字段改为"Jane Doe":

POST /customer/external/1/_update?pretty
{  "doc": { "name": "Jane Doe" }
}
Copy after login

以下例子演示了如何更新先前ID为1的文档,改变name字段为"Jane Doe" 的同时添加age字段

POST /customer/external/1/_update?pretty
{  "doc": { "name": "Jane Doe", "age": 20 }
}
Copy after login

也可以使用简单的脚本来执行更新。以下示例使用脚本将年龄增加5:

POST /customer/external/1/_update?pretty
{  "script" : "ctx._source.age += 5"}
Copy after login

在以上例子中, ctx._source 指当前即将被更新的源文档。请注意,在撰写本文时,只能一次更新单个文档。将来, Elasticsearch 可能会提供通过查询条件(如SQL UPDATE-WHERE语句)更新多个文档的功能。

删除文档

删除文档非常简单,以下例子演示了怎么删除 customer 索引下ID为2的文档,查阅Delete By Query API 删除与特定查询匹配的所有文档。值得注意的是,直接删除整个索引比通过query api 删除所有文档更高效。

DELETE /customer/external/2?pretty
Copy after login

批处理

除了能够索引,更新和删除单个文档之外, Elasticsearch  也提供了使用  _bulk API 批量执行上述任何操作的功能。这个功能是非常重要的,因为它提供了一个非常有效的机制来尽可能快地进行多个操作,并且尽可能减少网络的往返行程。简单举个例子,下面会在一个 bulk操作中索引两个文档:

POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
Copy after login

返回内容如下:

{  "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  }
    }
  ]
}
Copy after login

下面的例子会在一个操作内更新第一个文档同时删除第二个文档:

POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
Copy after login

返回内容如下:

{  "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  }
    }
  ]
}
Copy after login

注意以上的删除操作,在它之后并没有相应的源文档,因为只需要文档的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": ["华为","乐视","小米"]
}
Copy after login

2、查询商品, GET /product/goods/1 

3、修改商品

方式1:替换文档(和创建一样,所有字段必须写全)

PUT /product/goods/4{"goods_id": "40","goods_name": "联想笔记本","createTime": "2017-05-21","goods_type": ["电脑"]
}
Copy after login

字段不写全的情况

 方式2:更新文档

POST /product/goods/1/_update
{  "doc":{"goods_name":"iphone手机"
  }
}
Copy after login

比较创建,更新,替换文档返回结果:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template