博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch增、删、改、查操作深入详解
阅读量:5919 次
发布时间:2019-06-19

本文共 4928 字,大约阅读时间需要 16 分钟。

hot3.png

目录

 

正文

 

引言:

对于刚接触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*操作)

 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的信息。 
 
_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操作)

 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
  • }

}

查询&验证更新后结果:(对比可知,版本号已经更新完毕) 

{- "_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操作)

回结果

{- "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多维度分析

这里写图片描述

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

转载于:https://my.oschina.net/u/3482619/blog/1940834

你可能感兴趣的文章
[流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[3]
查看>>
进程管理1--进程的概念与操作
查看>>
基于pgrouting的任意两点间的最短路径查询函数二
查看>>
Android中的Animation应用
查看>>
[经典面试题]回文串专题
查看>>
android CountDownTimer 倒计时
查看>>
一分钟了解阿里云产品:表格存储
查看>>
Android应用架构概述
查看>>
2017十年腾讯大牛讲解---- Java并发编程:volatile关键字解析
查看>>
一文学会 Java 动态代理机制
查看>>
一个Promise面试题

查看>>
swift ++ 运算符实现
查看>>
详解webpack code splitting
查看>>
Xposed快速入门例子(一)----- 拉黑好友
查看>>
包含 min 函数的栈
查看>>
用for循环实现indexof
查看>>
js/es6/react/webpack
查看>>
从小白到大神程序员必读15本经典电子书免费送第二季(先到先得)
查看>>
[译] Data Binding 库使用的经验教训
查看>>
os x 搭建redis集群
查看>>