Elasticsearch中的CRUD
在《玩玩儿Elasticsearch》中简单介绍了一下elasticsearch。这篇文章,我们还是做些基础的学习,在Elasticsearch如何进行CRUD?
课程推荐→:《elasticsearch全文搜索实战》(实战视频)
假设我们正在创建的一个类似微博的应用,我们就姑且先叫它“kiwi”吧。kiwi这个应用就是一条条消息组成的。
在kiwi中,消息称为ksay。有两个部分组成,一是作者(author),而是消息本身(message)。
Create
curl -X POST http://localhost:9200/kiwi/ksay/ -d '{ "author": "rococojie", "message": "I am beautiful"}'
返回:{"_index":"kiwi","_type":"ksay","_id":"aaX3P2LJSP-dDYVy0USv7Q","_version":1,"created":true}
我们注意到elasticsearch默认不是按照自增的方式帮我们生成id的。而是自动生成22位的URL安全的_id。如刚才的例子中,返回的_id就是aaX3P2LJSP-dDYVy0USv7Q。如果要使用自定义的_id,则操作如下:
curl -X POST http://localhost:9200/kiwi/ksay/1 -d '{"author": "jerry", "message": "I hate Tom"}'
返回:{"_index":"kiwi","_type":"ksay","_id":"1","_version":1,"created":true}
Read
我们这里就只说用id取值
curl -X GET http://localhost:9200/kiwi/ksay/1
返回:{"_index":"kiwi","_type":"ksay","_id":"1","_version":1,"found":true, "_source" : { "author": "jerry", "message": "I hate Tom"}}
如果我们希望返回的知识原来我们存的数据,那么
curl -X GET http://localhost:9200/kiwi/ksay/1/_source
返回:{ "author": "jerry", "message": "I hate Tom"}
curl -X GET http://localhost:9200/kiwi/ksay/10000
返回{"_index":"kiwi","_type":"ksay","_id":"10000","found":false},没有找到我们刚才存的ksay。
Update
curl -X PUT http://localhost:9200/kiwi/ksay/1 -d '{"author": "jerry", "message": "I love Tom"}'
返回:{"_index":"kiwi","_type":"ksay","_id":"1","_version":2,"created":false}
我们注意到这里的_version变为了2,知识因为ksay发生了改变。created返回false,表示没有创建新的文档,只是更新。
虽然Elasticsearch支持进行文档更新,我们需要知道Elasticsearch中存储的文档是不可变的(immutable)。这种所谓的更新实际上是一种假象,在Elasticsearch内部,首先将比较旧的那条数据标明为“已经删除”,然后再把较新的那条数据进行index。(retrieve-change-reindex)
部分更新
curl -X POST http://localhost:9200/kiwi/ksay/1/_update -d '{ "doc": {"message": "I hate Tom, again"} }'
返回:{"_index":"kiwi","_type":"ksay","_id":"1","_version":3}
"doc"中即是我们需要更新的field。Elasticsearch会把最新的field“merge”到原来旧的文档中。这是我们再去查看这条ksay的信息。
curl -X GET http://localhost:9200/kiwi/ksay/1
返回:{"_index":"kiwi","_type":"ksay","_id":"1","_version":3,"found":true, "_source" : {"author":"jerry","message":"I hate Tom, again"}}
Delete
curl -X DELETE http://localhost:9200/kiwi/ksay/1
返回:{"found":true,"_index":"kiwi","_type":"ksay","_id":"1","_version":4}
再尝试去取ksay:
curl -X GET http://localhost:9200/kiwi/ksay/1
返回:{"_index":"kiwi","_type":"ksay","_id":"1","found":false}
就不能在访问到,found的值是false
学会了Elasticsearch最基本的CRUD,我们可以再找些其他好玩儿的来玩儿了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use Elasticsearch and PHP for product search and recommendation Introduction: In today's e-commerce field, a good search and recommendation system is very important for users. Elasticsearch is a powerful and flexible open source search engine. Combined with PHP as a back-end development language, it can provide efficient product search and personalized recommendation functions for e-commerce websites. This article will introduce how to use Elasticsearch and PHP to implement product search and recommendation functions, and attach

How to use Elasticsearch and PHP to build a user login and permission management system Introduction: In the current Internet era, user login and permission management are one of the necessary functions for every website or application. Elasticsearch is a powerful and flexible full-text search engine, while PHP is a widely used server-side scripting language. This article will introduce how to combine Elasticsearch and PHP to build a simple user login and permission management system

PHPElasticsearch: How to use dynamic mapping to achieve flexible search capabilities? Introduction: Search functionality is an integral part of developing modern applications. Elasticsearch is a powerful search and analysis engine that provides rich functionality and flexible data modeling. In this article, we will focus on how to use dynamic mapping to achieve flexible search capabilities. 1. Introduction to dynamic mapping In Elasticsearch, mapping (mapp

How to use MongoDB to develop a simple CRUD API In modern web application development, CRUD (Create, Delete, Modify, Check) operations are one of the most common and important functions. In this article, we will introduce how to develop a simple CRUD API using MongoDB database and provide specific code examples. MongoDB is an open source NoSQL database that stores data in the form of documents. Unlike traditional relational databases, MongoDB does not have a predefined schema

How to use PHP and Elasticsearch to achieve highlighted search results Introduction: In the modern Internet world, search engines have become the main way for people to obtain information. In order to improve the readability and user experience of search results, highlighting search keywords has become a common requirement. This article will introduce how to use PHP and Elasticsearch to achieve highlighted search results. 1. Preparation Before starting, we need to ensure that PHP and Elasticsearch have been installed and configured correctly.

In-depth study of Elasticsearch query syntax and practical introduction: Elasticsearch is an open source search engine based on Lucene. It is mainly used for distributed search and analysis. It is widely used in full-text search of large-scale data, log analysis, recommendation systems and other scenarios. When using Elasticsearch for data query, flexible use of query syntax is the key to improving query efficiency. This article will delve into the Elasticsearch query syntax and give it based on actual cases.

Summary of log analysis and exception monitoring based on Elasticsearch in PHP: This article will introduce how to use the Elasticsearch database for log analysis and exception monitoring. Through concise PHP code examples, it shows how to connect to the Elasticsearch database, write log data to the database, and use Elasticsearch's powerful query function to analyze and monitor anomalies in the logs. Introduction: Log analysis and exception monitoring are

How to use Vue and Axios to implement CRUD operations on data. In front-end development, it is often necessary to interact with the back-end server to perform CRUD operations on data. Vue is a popular JavaScript framework that helps us build interactive user interfaces. Axios is a Promise-based HTTP library that can help us easily communicate data with the server. By combining Vue and Axios, we can easily implement CRU of data
