search
HomeBackend DevelopmentC#.Net TutorialElasticsearch index and document operation example tutorial

Elasticsearch index and document operation example tutorial

Jun 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
C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# vs. .NET: Clarifying the Key Differences and SimilaritiesC# vs. .NET: Clarifying the Key Differences and SimilaritiesMay 01, 2025 am 12:12 AM

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

Beyond the Hype: Assessing the Current Role of C# .NETBeyond the Hype: Assessing the Current Role of C# .NETApr 30, 2025 am 12:06 AM

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft