引言:

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

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

创建成功如下显示:

  1. {
  2. - "_index": "blog",
  3. - "_type": "ariticle",
  4. - "_id": "1 -d",
  5. - "_version": 1,
  6. - "_shards": {
  7. - "total": 2,
  8. - "successful": 1,
  9. - "failed": 0
  10. - },
  11. - "created": true
  12. }

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"
- ]
- } }
`![这里写图片描述](http://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多维度分析

分布式搜索Elasticsearch增、删、改、查操作深入详解的更多相关文章

  1. 怎样从C#中打开数据库并进行 增 删 改 查 操作

    首先 在C#中引用数据库的操作! (因为我们用的是SQLserver数据库,所以是SqlClient) using System.Data.SqlClient; 1:要实现对数据库的操作,我们必须先登 ...

  2. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  3. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  4. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  5. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  6. SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...

  7. SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码) 概述: 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE tabl ...

  8. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  9. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

随机推荐

  1. 记一次Oracle数据库迁移部署

    --20141230部署脚本(按照时间顺序从上往下) --命令行,导出要部署的数据库数据(无分号) --expdp RMB3/test123@orcl3 SCHEMAS=RMB3 directory= ...

  2. js复制黏贴

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. jsp include包含html页面产生的乱码问题

    大家都知道在jsp中include的有两种方式,一种是<jsp:include page="">,另一种是<%@ include file="" ...

  4. J2EE的十三种技术(规范)

    J2EE的十三种技术(规范)  Java数据库连接(JDBC) JDBC API以一个统一的方式访问各种数据库.与ODBC类似,JDBC将开发者和私有数据库之间的问题隔离开来.由于它建立在Java上, ...

  5. [unity菜鸟] 笔记1 —— 函数篇

    SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...

  6. 浅析Android的窗口

    一.窗口的概念 在开发过程中,我们经常会遇到,各种跟窗口相关的类,或者方法.但是,在 Android 的框架设计中,到底什么是窗口?窗口跟 Android Framework 中的 Window 类又 ...

  7. 手势识别官方教程(3)识别移动手势(识别速度用VelocityTracker)

    moving手势在onTouchEvent()或onTouch()中就可识别,编程时主要是识别积云的速度用VelocityTracker等, Tracking Movement This lesson ...

  8. 【HDOJ】1061 Rightmost Digit

    这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...

  9. Linux Kernel ‘oz_cdev_write()’函数本地缓冲区溢出漏洞

    漏洞名称: Linux Kernel ‘oz_cdev_write()’函数本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-060 发布时间: 2013-11-07 更新时间: 201 ...

  10. 分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)

    http://www.cocoachina.com/ios/20151231/14846.html 笔者分享总结如下(本篇会不定期进行更新) : Objective-C 1.让Xcode的控制台支持L ...