search
HomeBackend DevelopmentC#.Net TutorialElasticsearch index and document operation example tutorial
Elasticsearch index and document operation example tutorialJun 23, 2017 pm 04:02 PM
elasticsearchoperatedocumentindex

  • 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

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

Create index

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 .

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"}

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.

Delete Index

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> /<index>/<type>/<id></id></type></index></rest>

使用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 

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!

Statement
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
Redis与Elasticsearch的区别与使用场景Redis与Elasticsearch的区别与使用场景May 11, 2023 am 08:01 AM

Redis与Elasticsearch的区别与使用场景随着互联网信息的快速发展和海量化,数据的高效存储和检索变得越来越重要。为此,NoSQL(NotOnlySQL)类型的数据库出现了,其中又以Redis和Elasticsearch较为流行。本文将对Redis和Elasticsearch进行比较,并探讨它们的使用场景。Redis与Elasticsearch

如何在PHP编程中使用Elasticsearch?如何在PHP编程中使用Elasticsearch?Jun 12, 2023 pm 01:10 PM

随着大数据和云计算技术的发展,搜索引擎也在不断创新。Elasticsearch,作为一个基于Lucene的全文搜索引擎,已经成为了一种流行的选择。这里将会介绍如何在PHP编程中使用Elasticsearch。安装Elasticsearch首先,我们需要安装和设置Elasticsearch。可以在官方网站下载和安装Elasticsearch,具体安装方法可以参

MySQL数据同步Elasticsearch的方案有哪些MySQL数据同步Elasticsearch的方案有哪些Jun 01, 2023 pm 06:37 PM

商品检索大家应该都在各种电商网站检索过商品,检索商品一般都是通过什么实现呢?搜索引擎Elasticsearch。那么问题来了,商品上架,数据一般写入到MySQL的数据库中,那么用于检索的数据又是怎么同步到Elasticsearch的呢?MySQL同步ES1.同步双写这是能想到的最直接的方式,在写入MySQL,直接也同步往ES里写一份数据。同步双写对于这种方式:优点:实现简单缺点:业务耦合,商品的管理中耦合大量数据同步代码影响性能,写入两个存储,响应时间变长不便扩展:搜索可能有一些个性化需求,需要

PHP和Elasticsearch集成实现全文检索功能详解PHP和Elasticsearch集成实现全文检索功能详解Jun 25, 2023 am 10:14 AM

随着互联网的发展,企业面对的文本数据越来越庞大。如何快速、准确地检索出相关内容,成为企业在信息化领域的重要课题之一。Elasticsearch作为一个基于Lucene的开源搜索引擎,具有高可用性、高可扩展性和快速检索的特点,成为企业全文检索的首选方案之一。而PHP作为一门流行的服务器端编程语言,也能够快速进行Web开发和API开发,成为与Elasticsea

如何使用Elasticsearch和PHP构建智能问答系统如何使用Elasticsearch和PHP构建智能问答系统Jul 07, 2023 pm 03:55 PM

如何使用Elasticsearch和PHP构建智能问答系统引言:随着人工智能技术的快速发展,智能问答系统正逐渐成为人们获取信息的重要方式。Elasticsearch作为一个强大的搜索引擎,拥有快速、高效的全文搜索和分析能力,可以为智能问答系统提供强大的支持。本文将介绍如何使用Elasticsearch和PHP构建一个简单的智能问答系统,并提供相应的代码示例。

PHP和Elasticsearch实现的高性能的文本分类技术PHP和Elasticsearch实现的高性能的文本分类技术Jul 07, 2023 pm 02:49 PM

PHP和Elasticsearch实现的高性能文本分类技术引言:在当前的信息时代,文本分类技术被广泛应用于搜索引擎、推荐系统、情感分析等领域。而PHP是一种广泛使用的服务器端脚本语言,具有简单易学、效率高等特点。在本文中,我们将介绍如何利用PHP和Elasticsearch实现高性能的文本分类技术。一、Elasticsearch简介Elasticsearch

同步MySQL数据至Elasticsearch的方式有哪些同步MySQL数据至Elasticsearch的方式有哪些May 30, 2023 pm 08:49 PM

1.业务层同步由于对MySQL数据的操作也是在业务层完成的,所以在业务层同步操作另外的数据源也是很自然的,比较常见的做法就是在ORM的hooks钩子里编写相关同步代码。这种方式的缺点是,当服务越来越多时,同步的部分可能会过于分散从而导致难以更新迭代,例如对ES索引进行不兼容迁移时就可能会牵一发而动全身。2.中间件同步当应用架构演变为微服务时,各个服务里可能不再直接调用MySQL,而是通过一层middleware中间件,这时候就可以在中间件操作MySQL的同时同步其它数据源。这种方式需要中间件去适

springboot中如何集成elasticsearchspringboot中如何集成elasticsearchJun 01, 2023 am 08:22 AM

1,引入依赖org.springframework.bootspring-boot-starter-data-elasticsearch2,编写实体映射类@Data@Document(indexName="index",createIndex=true)publicclassIndex{@IdprivateStringid;@Field(type=FieldType.Text,analyzer="ik_max_word",searchAnalyzer=&q

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!