参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html

一、索引操作

1、查看当前节点的所有的index

  1. 查看当前节点的所有的index
  2.  
  3. [root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
  4. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  5. green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb

2、查看每个index下的type和filed的对应的数据类型

  1. 查看每个index下的typefiled的类型
  2. [root@es1 ~]# curl 'http://10.87.6.2:9200/_mapping?pretty=true'
  3. {
  4. "students" : {
  5. "mappings" : {
  6. "class1" : {
  7. "properties" : {
  8. "passwd" : {
  9. "type" : "text",
  10. "fields" : {
  11. "keyword" : {
  12. "type" : "keyword",
  13. "ignore_above" : 256
  14. }
  15. }
  16. },
  17. "username" : {
  18. "type" : "text",
  19. "fields" : {
  20. "keyword" : {
  21. "type" : "keyword",
  22. "ignore_above" : 256
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

3、创建索引的操作

  1. 创建索引,返回true,意味着创建成功了
  2. [root@es1 ~]# curl -X PUT 'http://10.87.6.2:9200/weather'
  3. {"acknowledged":true,"shards_acknowledged":true,"index":"weather"}[root@es1 ~]#
  4.  
  5. 创建完成后,查看索引
  6. [root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
  7. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  8. green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb
  9. green open weather VWnR2eM9RIG8A0MJgNWORw 5 1 0 0 1.5kb 690b

4、删除索引操作

  1. 删除索引,返回true,意味着删除成功
  2. [root@es1 ~]# curl -X DELETE 'http://10.87.6.2:9200/weather'
  3. {"acknowledged":true}[root@es1 ~]#
  4.  
  5. 删除完成后,查看索引
  6. [root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
  7. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  8. green open students Btx9nIaLQ1GnVqy5ncbipg 5 1 6 0 48kb 24kb
  9. [root@es1 ~]#

二、文档操作

1、新增一个文档记录,指定索引为数字

  1. [root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty' -d '{"usernmae":"baoliang","passwd":"44444444"}' -H "Content-Type: application/json"
  2. {
  3. "_index" : "students",
  4. "_type" : "class1",
  5. "_id" : "7",
  6. "_version" : 1,
  7. "result" : "created",
  8. "_shards" : {
  9. "total" : 2,
  10. "successful" : 2,
  11. "failed" : 0
  12. },
  13. "_seq_no" : 2,
  14. "_primary_term" : 1
  15. }
  16.  
  17. 查看新增的记录
  18. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty'
  19. {
  20. "_index" : "students",
  21. "_type" : "class1",
  22. "_id" : "7",
  23. "_version" : 1,
  24. "_seq_no" : 2,
  25. "_primary_term" : 1,
  26. "found" : true,
  27. "_source" : {
  28. "usernmae" : "baoliang",
  29. "passwd" : "44444444"
  30. }
  31. }

2、创建一个文档,指定索引为字符串

  1. 这里我们注意,这条文档的id7,但是id不一定为7,为abc也是可以的
  2.  
  3. [root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/abc?pretty' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
  4. {
  5. "_index" : "students",
  6. "_type" : "class1",
  7. "_id" : "abc",
  8. "_version" : 1,
  9. "result" : "created",
  10. "_shards" : {
  11. "total" : 2,
  12. "successful" : 2,
  13. "failed" : 0
  14. },
  15. "_seq_no" : 1,
  16. "_primary_term" : 1
  17. }
  18. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/abc?pretty'
  19. {
  20. "_index" : "students",
  21. "_type" : "class1",
  22. "_id" : "abc",
  23. "_version" : 1,
  24. "_seq_no" : 1,
  25. "_primary_term" : 1,
  26. "found" : true,
  27. "_source" : {
  28. "usernmae" : "wxz",
  29. "passwd" : "5555555555"
  30. }
  31. }

3、创建文档,不指定索引,由elasticsearch为我们指定索引,但是方法要采用XPOST方法,不能使用XPUT方法

  1. 这里还需要注意,上面都指定id了,我们有可以不指定id进行新增文档,但是不能XPUT的方法,要用XPOST方法
  2. [root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
  3. {"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":1}[root@es1 ~]#
  4.  
  5. 我们看到这次的id是随机生成的2oUVBmkBGaSC379Rl48e
  6.  
  7. 通过这个随机的id我们查看我们的文档
  8. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty'
  9. {
  10. "_index" : "students",
  11. "_type" : "class1",
  12. "_id" : "2oUVBmkBGaSC379Rl48e",
  13. "_version" : 1,
  14. "_seq_no" : 3,
  15. "_primary_term" : 1,
  16. "found" : true,
  17. "_source" : {
  18. "usernmae" : "wxz",
  19. "passwd" : "5555555555"
  20. }
  21. }

4、这里还需要注意一点

这里还需要注意,如果我们输入的index不存在,elasticsearch也不会报错,他会为我们新建一个索引,所以这里要非常的注意,index一定不能写错

5、查看文档的操作

  1. 查看某个文档的
  2. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty=true'
  3. {
  4. "_index" : "students",
  5. "_type" : "class1",
  6. "_id" : "2oUVBmkBGaSC379Rl48e",
  7. "_version" : 1,
  8. "_seq_no" : 3,
  9. "_primary_term" : 1,
  10. "found" : true,
  11. "_source" : {
  12. "usernmae" : "wxz",
  13. "passwd" : "5555555555"
  14. }
  15. }
  16.  
  17. 如果id输错了,就会查不到数据
  18. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
  19. {
  20. "_index" : "students",
  21. "_type" : "class1",
  22. "_id" : "2oUVBmkBGaSC379Rl48",
  23. "found" : false
  24. }

6、删除文档的操作

  1. 删除记录的方法,输入indextypeid就可以删除指定的文档
  2. [root@es1 ~]# curl -XDELETE 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e'
  3. {"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":2,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1}[root@es1 ~]#
  4.  
  5. 删除成功后,我们在查看这个文档,就已经查不到了
  6. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
  7. {
  8. "_index" : "students",
  9. "_type" : "class1",
  10. "_id" : "2oUVBmkBGaSC379Rl48",
  11. "found" : false
  12. }

7、更新操作,采用_update方法

  1. 更新操作
  2. [root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/7/_update?pretty=true' -d '{"doc":{"passwd":"55555555"}}' -H "Content-Type: application/json"
  3. {
  4. "_index" : "students",
  5. "_type" : "class1",
  6. "_id" : "7",
  7. "_version" : 2,
  8. "result" : "updated",
  9. "_shards" : {
  10. "total" : 2,
  11. "successful" : 2,
  12. "failed" : 0
  13. },
  14. "_seq_no" : 5,
  15. "_primary_term" : 1
  16. }
  17.  
  18. 查看更新后的结果
  19. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
  20. {
  21. "_index" : "students",
  22. "_type" : "class1",
  23. "_id" : "7",
  24. "_version" : 2,
  25. "_seq_no" : 5,
  26. "_primary_term" : 1,
  27. "found" : true,
  28. "_source" : {
  29. "usernmae" : "baoliang",
  30. "passwd" : "55555555"
  31. }
  32. }

8、覆盖操作,采用XPUT方法,id输入已有的id,也就是我们要覆盖的文档的id

  1. xput方法是覆盖,update方法更新,上面我们介绍了一下update方法,下面我们看下xput方法,我们看到新的数据只有一个字段了,username字段被覆盖掉了
  2.  
  3. [root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty=true' -d '{"passwd":"55555555"}' -H "Content-Type: application/json"
  4. {
  5. "_index" : "students",
  6. "_type" : "class1",
  7. "_id" : "7",
  8. "_version" : 3,
  9. "result" : "updated",
  10. "_shards" : {
  11. "total" : 2,
  12. "successful" : 2,
  13. "failed" : 0
  14. },
  15. "_seq_no" : 6,
  16. "_primary_term" : 1
  17. }
  18.  
  19. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
  20. {
  21. "_index" : "students",
  22. "_type" : "class1",
  23. "_id" : "7",
  24. "_version" : 3,
  25. "_seq_no" : 6,
  26. "_primary_term" : 1,
  27. "found" : true,
  28. "_source" : {
  29. "passwd" : "55555555"
  30. }
  31. }

8、查询所有的数据

  1. 查询的所有的数据
  2. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search?pretty=true'
  3. {
  4. "took" : 25,
  5. "timed_out" : false,
  6. "_shards" : {
  7. "total" : 5,
  8. "successful" : 5,
  9. "skipped" : 0,
  10. "failed" : 0
  11. },
  12. "hits" : {
  13. "total" : 8,
  14. "max_score" : 1.0,
  15. "hits" : [
  16. {
  17. "_index" : "students",
  18. "_type" : "class1",
  19. "_id" : "5",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "username" : "zyb",
  23. "passwd" : "111111"
  24. }
  25. },
  26. {
  27. "_index" : "students",
  28. "_type" : "class1",
  29. "_id" : "4",
  30. "_score" : 1.0,
  31. "_source" : {
  32. "username" : "chr",
  33. "passwd" : "111111"
  34. }
  35. },
  36. {
  37. "_index" : "students",
  38. "_type" : "class1",
  39. "_id" : "6",
  40. "_score" : 1.0,
  41. "_source" : {
  42. "username" : "cyr",
  43. "passwd" : "abcdef"
  44. }
  45. },
  46. {
  47. "_index" : "students",
  48. "_type" : "class1",
  49. "_id" : "1",
  50. "_score" : 1.0,
  51. "_source" : {
  52. "username" : "fxk",
  53. "passwd" : "111111"
  54. }
  55. },
  56. {
  57. "_index" : "students",
  58. "_type" : "class1",
  59. "_id" : "7",
  60. "_score" : 1.0,
  61. "_source" : {
  62. "passwd" : "55555555"
  63. }
  64. },
  65. {
  66. "_index" : "students",
  67. "_type" : "class1",
  68. "_id" : "3",
  69. "_score" : 1.0,
  70. "_source" : {
  71. "username" : "chy",
  72. "passwd" : "111111"
  73. }
  74. },
  75. {
  76. "_index" : "students",
  77. "_type" : "class1",
  78. "_id" : "abc",
  79. "_score" : 1.0,
  80. "_source" : {
  81. "usernmae" : "wxz",
  82. "passwd" : "5555555555"
  83. }
  84. }
  85. ]
  86. }
  87. }

这里的

took是表示操作的耗时,单位是毫秒

timeout表示是否超时

hits表示命中的数目的详细信息

total表示命中的数目

2、搜索满足指定的条件的文档

  1. 搜索,查看usernamechy的文档
  2.  
  3. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy"}}}' -H "Content-Type: application/json"

搜索结果如下

  1. {
  2. "took" : 6,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 1,
  12. "max_score" : 0.2876821,
  13. "hits" : [
  14. {
  15. "_index" : "students",
  16. "_type" : "class1",
  17. "_id" : "3",
  18. "_score" : 0.2876821,
  19. "_source" : {
  20. "username" : "chy",
  21. "passwd" : "111111"
  22. }
  23. }
  24. ]
  25. }
  26. }

3、搜索满足指定条件的文档,但是设置返回的条数,使用size参数,设置返回的条数为1,默认是返回10条

  1. [root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy"}},"size":1}' -H "Content-Type: application/json"

搜索结果

  1. {
  2. "took" : 4,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 1,
  12. "max_score" : 0.2876821,
  13. "hits" : [
  14. {
  15. "_index" : "students",
  16. "_type" : "class1",
  17. "_id" : "3",
  18. "_score" : 0.2876821,
  19. "_source" : {
  20. "username" : "chy",
  21. "passwd" : "111111"
  22. }
  23. }
  24. ]
  25. }
  26. }

4、搜索满足多个条件的语句,多个条件为or的关系

  1. [root@es3 elasticsearch]# curl -XGET 'http://10.87.6.3:9200/students/class1/_search' -d '{"query":{"match":{"username":"chy chr"}}}' -H "Content-Type: application/json"

搜索结果

  1. {
  2. "took" : 6,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 2,
  12. "max_score" : 0.9808292,
  13. "hits" : [
  14. {
  15. "_index" : "students",
  16. "_type" : "class1",
  17. "_id" : "4",
  18. "_score" : 0.9808292,
  19. "_source" : {
  20. "username" : "chr",
  21. "passwd" : "111111"
  22. }
  23. },
  24. {
  25. "_index" : "students",
  26. "_type" : "class1",
  27. "_id" : "3",
  28. "_score" : 0.2876821,
  29. "_source" : {
  30. "username" : "chy",
  31. "passwd" : "111111"
  32. }
  33. }
  34. ]
  35. }
  36. }

5、搜索满足多个条件的语句,多个条件为and的关系

  1. [root@es3 elasticsearch]#  curl -XGET 'http://10.87.6.3:9200/students/class1/_search?pretty' -d '{"query":{"bool":{"must":[{"match":{"username":"chr"}},{"match":{"passwd":22222}}]}}}' -H "Content-Type: application/json"

搜索结果

  1. {
  2. "took" : 5,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 0,
  12. "max_score" : null,
  13. "hits" : [ ]
  14. }
  15. }

6、更为复杂的查询,比如查询名称为smith,同时年龄大于30的人,这个时候就需要采用过滤器filter

  1. {
  2. "query" : {
  3. "filtered" : {
  4. "filter" : {
  5. "range" : {
  6. "age" : { "gt" : 30 }
  7. }
  8. },
  9. "query" : {
  10. "match" : {
  11. "last_name" : "smith"
  12. }
  13. }
  14. }
  15. }
  16. }

7、全文搜索

  1. {
  2. "query" : {
  3. "match" : {
  4. "about" : "rock climbing"
  5. }
  6. }
  7. }

8、

至此,我们的elasticsearch的索引操作和文档操作的总结暂时就完成了!

elasticsearch的索引操作和文档操作总结的更多相关文章

  1. Jquery的事件操作和文档操作

    对于熟悉前端开发的小伙伴,相信对于Jquery一定不陌生,相对于JavaScript的繁琐,Jquery更加的简洁,当然简洁不意味着简单,我们可以使用Jquery完成我们想要实现全部功能,这里为小白们 ...

  2. Elasticsearch索引和文档操作

    列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...

  3. elasticsearch 第五篇(文档操作接口)

    INDEX API 示例: 1 2 3 4 5 PUT /test/user/1 { "name": "silence", "age": 2 ...

  4. 前端开发之jQuery属性和文档操作

    主要内容: 1.jQuery属性操作 2.jQuery文档操作 一.jQuery属性操作 1.什么是jQuery的属性操作? jQuery的属性操作模块包括四个部分:html属性操作,dom属性操作, ...

  5. Elasticsearch (1) - 索引库 文档 分词

    创建索引库 ES的索引库是一个逻辑概念,它包括了分词列表及文档列表,同一个索引库中存储了相同类型的文档.它就相当于MySQL中的表,或相当于Mongodb中的集合. 关于索引这个语: 索引(名词):E ...

  6. ES 10 - Elasticsearch的索引别名和索引模板

    目录 1 索引模板概述 1.1 什么是索引模板 1.2 索引模板中的内容 1.3 索引模板的用途 2 创建索引模板 3 查看索引模板 4 删除索引模板 5 模板的使用建议 5.1 一个index中不能 ...

  7. 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...

  8. 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...

  9. ElasticSearch 基本概念 and 索引操作 and 文档操作 and 批量操作 and 结构化查询 and 过滤查询

    基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...

随机推荐

  1. ABAP-串口通信-道闸设备

    最近SAP系统需要与道闸设备集成,通过串口通讯模式控制道闸栏杆升降,在此将开发过程中的思路及问题点做个备注. 一.相关设备 道闸设备型号:富士智能FJC-D618 串口模块:康耐德 C2000-A1- ...

  2. subsets 回溯 给定集合,枚举子集。元素不重复

    这个回溯感觉掌握的有些熟练了. 两种方式,递归和循环. 感觉就是套框架了. /** * Return an array of arrays of size *returnSize. * The siz ...

  3. ElasticSearch centos7 安装

    参考: https://blog.csdn.net/u014180504/article/details/78733827 https://blog.csdn.net/youzhouliu/artic ...

  4. css实现角标

    效果图: 简单方式可以使用背景图片,但这里我使用的css来实现,最笨的方式是使用矩形div然后旋转遮挡就可以, <div class='checked-item'>    角标实现 < ...

  5. JS 实现分页打印

    在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印. 在样式中有规定几个打印的样式 page-break-before和page-break-after CSS属性并不 ...

  6. UploadFtp

    #!/bin/bash FILENAME=$ DSTDIR=$ FTPSRV=ip FTPUSER="user" FTPPWD="password" SRCDI ...

  7. Android sdk测试方法链接

    https://blog.csdn.net/u013059441/article/details/79030998?utm_source=blogxgwz0

  8. C# 根据Excel生成树

    需求: 根据Excel生成树,Excel的某些节点为属性节点, 如: 列(桩号.构件编码.测试属性1) 是列(分项工程名称) 的属性,非节点. 列(桩号.构件编码.测试属性1) 以属性的方式存在 导入 ...

  9. window document树

  10. web访问命令行

    https://github.com/yudai/gotty go get github.com/yudai/gotty gotty -p 8000 -w kubectl exec -it mysql ...