Elasticsearch增、刪、改、查操做深刻詳解

目錄html

 

正文mysql

 

引言:

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

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)下插入一行數據。app

一、新建文檔(相似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"]
}

 

建立成功以下顯示:curl

{

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

}

 

這裏寫圖片描述

二、檢索文檔(相似mysql search 搜索select*操做)

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

檢索結果以下:url

{

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

}

 

若是未找到會提示:spa

{

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

}

 

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

{

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

 

三、更新文檔(相似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

四、刪除文檔(相似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

 

這裏寫圖片描述

五、Kibana可視化分析

回到頂部

5.一、在索引blog上查詢包含」university」字段的信息。

這裏寫圖片描述

回到頂部

5.二、Kibana多維度分析

這裏寫圖片描述

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

相關文章
相關標籤/搜索