Elasticsearch增、删、改、查操作深入详解

Elasticsearch增、删、改、查操作深入详解引言:对于刚接触ES的童鞋,经常搞不明白ES的各个概念的含义。尤其对“索引”二字更是与关系型数据库混淆的不行。本文通过对比关系型数据库,将ES中常见的增、删、改、查操作进行图文呈现。能加深你对ES的理解。同时,也列举了kibana下的图形化展示。ESRestfulAPIGET、POST、PUT、DELETE、HEAD含义:1)GET:获取请求对象的当前状态。2)POST:改变对象的当前

大家好,又见面了,我是你们的朋友全栈君。

全网最新 | Elasticsearch 7.X 进阶实战视频课

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

}

这里写图片描述

5、Kibana可视化分析

##5.1、在索引blog上查询包含”university”字段的信息。
这里写图片描述
##5.2、Kibana多维度分析
这里写图片描述

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

更多ES相关实战干货经验分享,请扫描下方【铭毅天下】微信公众号二维码关注。
(每周至少更新一篇!)

这里写图片描述
和你一起,死磕Elasticsearch
——————————————————————————————————

2016年7月17日 13:31思于家中床前

作者:铭毅天下
转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/51931981
如果感觉本文对您有帮助,请点击‘顶’支持一下,您的支持是我坚持写作最大的动力,谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131658.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • host地址_hostname在哪个目录

    host地址_hostname在哪个目录https://laod.cn/hosts/hosts-position.html

    2022年8月4日
    7
  • Java基础之int和Integer有什么区别

    Java基础之int和Integer有什么区别1int与Integer的基本使用对比(1)Integer是int的包装类;int是基本数据类型;(2)Integer变量必须实例化后才能使用;int变量不需要;(3)Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值;(4)Integer的默认值是null;int的默认值是0。2int与Integer的深入对比(1)由于In…

    2022年7月16日
    16
  • Ubuntu 18.04 浏览器安装flash[通俗易懂]

    flash官网(注意下载和系统浏览器相对应的包)PPAPI:供Opera(15以上)、Chromium(开源谷歌)浏览器使用NPAPI:适用于FireFox(火狐)、Safari(苹果)、Opera(欧朋,12.17版以下)然后解压:tar-zxvfflash_player_npapi_linux.x86_64.tar.gz复制:sudocplibflashplay…

    2022年4月12日
    67
  • django配置文件详解_pycharm配置python

    django配置文件详解_pycharm配置python前言Django的配置文件settings.py用于配置整个网站的环境和功能,核心配置必须有项目路径、密钥配置、域名访问权限、App列表、中间件、资源文件、模板配置、数据库的连接方式基本配置信息

    2022年7月28日
    9
  • quartus ii 12.0安装教程_系统安装教程

    quartus ii 12.0安装教程_系统安装教程安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,安装包路径也不要有中文。1.鼠标右击【QuartusII12.0】压缩包选择【解压到QuartusII12.0】。2.双击打开解压后的【QuartusII12.0】文件夹。3.双击打开【Quartus】文件夹。4.鼠标右击【12.0_178_quartus_windows.exe】选择【以管理员身份运行】。5.点击【Install】。6.解压中。7.勾选【AllowA…

    2022年10月8日
    1
  • idea for mac 激活码2021【中文破解版】

    (idea for mac 激活码2021)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月27日
    569

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号