搜尋
首頁資料庫mysql教程Elasticsearch中的CRUD

Elasticsearch中的CRUD

Jun 07, 2016 pm 03:56 PM
crudelasticsearch

在《玩玩儿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,我们可以再找些其他好玩儿的来玩儿了

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何在MySQL中刪除或修改現有視圖?如何在MySQL中刪除或修改現有視圖?May 16, 2025 am 12:11 AM

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

MySQL視圖:我可以使用哪些設計模式?MySQL視圖:我可以使用哪些設計模式?May 16, 2025 am 12:10 AM

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

在MySQL中使用視圖的優點是什麼?在MySQL中使用視圖的優點是什麼?May 16, 2025 am 12:09 AM

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

如何在MySQL中創建一個簡單的視圖?如何在MySQL中創建一個簡單的視圖?May 16, 2025 am 12:08 AM

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

MySQL創建用戶語句:示例和常見錯誤MySQL創建用戶語句:示例和常見錯誤May 16, 2025 am 12:04 AM

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

北端:融合系統,解釋
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用