Elasticsearch增、刪、改、查操作深入詳解


 

引言:

對於剛接觸ES的童鞋,經常搞不明白ES的各個概念的含義。尤其對“索引”二字更是與關系型數據庫混淆的不行。本文通過對比關系型數據庫,將ES中常見的增、刪、改、查操作進行圖文呈現。能加深你對ES的理解。同時,也列舉了kibana下的圖形化展示。

ES Restful API GET、POST、PUT、DELETE、HEAD含義: 
1)GET:獲取請求對象的當前狀態。 
2)POST:改變對象的當前狀態。 
3)PUT:創建一個對象。 
4)DELETE:銷毀對象。 
5)HEAD:請求獲取對象的基礎信息。

Mysql與Elasticsearch核心概念對比示意圖 
這里寫圖片描述 
以上表為依據, 
ES中的新建文檔(在Index/type下)相當於Mysql中(在某Database的Table)下插入一行數據。

1、新建文檔(類似mysql insert插入操作)

http://localhost:9200/blog/ariticle/1 put { "title":"New version of Elasticsearch released!", "content":"Version 1.0 released today!", "tags":["announce","elasticsearch","release"] }

 

創建成功如下顯示:

{

- "_index": "blog", - "_type": "ariticle", - "_id": "1 -d", - "_version": 1, - "_shards": { - "total": 2, - "successful": 1, - "failed": 0 - }, - "created": true }

 

這里寫圖片描述

2、檢索文檔(類似mysql search 搜索select*操作)

http://localhost:9200/blog/ariticle/1/ GET

檢索結果如下:

{

- "_index": "blog", - "_type": "ariticle", - "_id": "1", - "_version": 1, - "found": true, - "_source": { - "title": "New version of Elasticsearch released!", - "content": "Version 1.0 released today!", - "tags": [ - "announce" - , - "elasticsearch" - , - "release" - ] - } }

 

如果未找到會提示:

{

- "_index": "blog", - "_type": "ariticle", - "_id": "11", - "found": false }

 

查詢全部文檔如下: 
這里寫圖片描述 
具體某個細節內容檢索, 
查詢舉例1:查詢cotent列包含版本為1.0的信息。 
http://localhost:9200/blog/ 
_search?pretty&q=content:1.0

{

- "took": 2, - "timed_out": false, - "_shards": { - "total": 5, - "successful": 5, - "failed": 0 - }, - "hits": { - "total": 1, - "max_score": 0.8784157, - "hits": [ - { - "_index": "blog", - "_type": "ariticle", - "_id": "6", - "_score": 0.8784157, - "_source": { - "title": "deep Elasticsearch!", - "content": "Version 1.0!", - "tags": [ - "deep" - , - "elasticsearch" - ] - } - } - ] - } }

 

查詢舉例2:查詢書名title中包含“enhance”字段的數據信息: 
[root@5b9dbaaa1a ~]# curl -XGET 10.200.1.121:9200/blog/ariticle/_search?pretty -d ‘

> { "query" : { > "term" : > {"title" : "enhance" } > } > }' { "took" : 189, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.8784157, "hits" : [ { "_index" : "blog", "_type" : "ariticle", "_id" : "4", "_score" : 0.8784157, "_source" : { "title" : "enhance Elasticsearch!", "content" : "Version 4.0!", "tags" : [ "enhance", "elasticsearch" ] } }, { "_index" : "blog", "_type" : "ariticle", "_id" : "5", "_score" : 0.15342641, "_source" : { "title" : "enhance Elasticsearch for university!", "content" : "Version 5.0!", "tags" : [ "enhance", "elasticsearch" ] } } ] } }

 

查詢舉例3:查詢ID值為3,5,7的數據信息: 
[root@5b9dbaaa148a ~]# curl -XGET 10.200.1.121:9200/blog/ariticle/_search?pretty -d ‘

{ "query" : { "terms" : {"_id" : [ "3", "5", "7" ] } } }' { "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 0.19245009, "hits" : [ { "_index" : "blog", "_type" : "ariticle", "_id" : "5", "_score" : 0.19245009, "_source" : { "title" : "enhance Elasticsearch for university!", "content" : "Version 5.0!", "tags" : [ "enhance", "elasticsearch" ] } }, { "_index" : "blog", "_type" : "ariticle", "_id" : "7", "_score" : 0.19245009, "_source" : { "title" : "deep Elasticsearch for university!", "content" : "Version 2.0!", "tags" : [ "deep", "elasticsearch", "university" ] } }, { "_index" : "blog", "_type" : "ariticle", "_id" : "3", "_score" : 0.19245009, "_source" : { "title" : "init Elasticsearch for university!", "content" : "Version 3.0!", "tags" : [ "initialize", "elasticsearch" ] } } ] } }

 

3、更新文檔(類似mysql update操作)

http://localhost:9200/blog/ariticle/1/_update/ POST 
{“script”:”ctx._source.content = \”new version 2.0 20160714\”“}

更新后結果顯示: 
{

  • “_index”: “blog”,
  • “_type”: “ariticle”,
  • “_id”: “1”,
  • “_version”: 2,
  • “_shards”: { 
    • ”total”: 2,
    • “successful”: 1,
    • “failed”: 0
  • }

}

查詢&驗證更新后結果:(對比可知,版本號已經更新完畢) 
http://localhost:9200/blog/ariticle/1/

{

- "_index": "blog", - "_type": "ariticle", - "_id": "1", - "_version": 2, - "found": true, - "_source": { - "title": "New version of Elasticsearch released!", - "content": "new version 2.0 20160714", - "tags": [ - "announce" - , - "elasticsearch" - , - "release" - ] - } } `![這里寫圖片描述](https://img-blog.csdn.net/20160717132407353)`` 注意更新文檔需要在elasticsearch_win\config\elasticsearch.yml下新增以下內容:

script.groovy.sandbox.enabled: true 
script.engine.groovy.inline.search: on 
script.engine.groovy.inline.update: on 
script.inline: on 
script.indexed: on 
script.engine.groovy.inline.aggs: on 
index.mapper.dynamic: true

4、刪除文檔(類似mysql delete操作)

http://localhost:9200/blog/ariticle/8/回結果

{

- "found": true, - "_index": "blog", - "_type": "ariticle", - "_id": "8", - "_version": 2, - "_shards": { - "total": 2, - "successful": 1, - "failed": 0 - } }

1

 

這里寫圖片描述

5、Kibana可視化分析

5.1、在索引blog上查詢包含”university”字段的信息。

這里寫圖片描述

5.2、Kibana多維度分析

這里寫圖片描述

——————————————————————————————————

更多ES相關實戰干貨經驗分享,請掃描下方【銘毅天下】微信公眾號二維碼關注。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM