ElasticSearch02_Restful风格基本操作

清疚 2022-11-29 08:49 256阅读 0赞

ElasticSearch02


Restful 风格操作索引:

1. 创建索引示例:

  1. PUT /索引名/类型名/文档id
  2. {请求体}
  3. PUT /test1/_doc/1
  4. {
  5. "name": "BLU",
  6. "age": 3,
  7. "birth":"1998-09-30"
  8. }

在这里插入图片描述
2. 查询索引信息(创建时没有指定字段的类型,但ES智能的配置了字段类型):

  1. GET test1
  2. {
  3. "test1" : {
  4. "aliases" : { },
  5. "mappings" : {
  6. "properties" : {
  7. "age" : {
  8. "type" : "long"
  9. },
  10. "birth" : {
  11. "type" : "date"
  12. },
  13. "name" : {
  14. "type" : "text",
  15. "fields" : {
  16. "keyword" : {
  17. "type" : "keyword",
  18. "ignore_above" : 256
  19. }
  20. }
  21. }
  22. }
  23. },
  24. "settings" : {
  25. "index" : {
  26. "creation_date" : "1597834637327",
  27. "number_of_shards" : "1",
  28. "number_of_replicas" : "1",
  29. "uuid" : "tp92cyypT0yXtJ9EsRW1MA",
  30. "version" : {
  31. "created" : "7070199"
  32. },
  33. "provided_name" : "test1"
  34. }
  35. }
  36. }
  37. }

基本数据类型:

  1. 字符串类型: text keyword 注:text类型可以被分词器解析,而keyword类型不会被分词器解析
  2. 数值类型: long integer short byte double float half float scaled float
  3. 日期类型: date
  4. 布尔类型:boolean
  5. 二进制类型:binary
  6. ......

3. 创建索引并指定字段的类型:

  1. PUT /test2
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "text"
  7. },
  8. "age":{
  9. "type": "long"
  10. },
  11. "birthday":{
  12. "type": "date"
  13. }
  14. }
  15. }
  16. }

在这里插入图片描述
查询索引信息:

  1. GET test2
  2. {
  3. "test2" : {
  4. "aliases" : { },
  5. "mappings" : {
  6. "properties" : {
  7. "age" : {
  8. "type" : "long"
  9. },
  10. "birthday" : {
  11. "type" : "date"
  12. },
  13. "name" : {
  14. "type" : "text"
  15. }
  16. }
  17. },
  18. "settings" : {
  19. "index" : {
  20. "creation_date" : "1597833899073",
  21. "number_of_shards" : "1",
  22. "number_of_replicas" : "1",
  23. "uuid" : "KpZ-7iOOTR2dpxh6WloWUw",
  24. "version" : {
  25. "created" : "7070199"
  26. },
  27. "provided_name" : "test2"
  28. }
  29. }
  30. }
  31. }

4. 查看健康值信息:

  1. GET _cat/health
  2. 1597835176 11:06:16 elasticsearch yellow 1 1 7 7 0 0 3 0 - 70.0%

5. 查看ES所有index:

  1. GET _cat/indices?v
  2. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  3. yellow open test2 KpZ-7iOOTR2dpxh6WloWUw 1 1 0 0 208b 208b
  4. yellow open test3 QxA5gefcSM6-uzqYrfH3uw 1 1 1 0 4kb 4kb
  5. green open .apm-custom-link U03Ki1pkRU63zQ3IydY96w 1 0 0 0 208b 208b
  6. green open .kibana_task_manager_1 MdDmfW_2QGecdl0-bqAWWQ 1 0 5 0 75.9kb 75.9kb
  7. green open .apm-agent-configuration b3RptmMRS_W3YXSPL4Abzw 1 0 0 0 208b 208b
  8. green open .kibana_1 R4adYkTXTLC5dKfJhEgS5A 1 0 46 7 175.6kb 175.6kb
  9. yellow open test1 tp92cyypT0yXtJ9EsRW1MA 1 1 1 0 4kb 4kb

6. 修改文档数据:

  1. POST /test1/_doc/1/_update
  2. {
  3. "doc":{
  4. "name": "法外狂徒张三",
  5. "age": 30
  6. }
  7. }
  8. {
  9. "_index" : "test1",
  10. "_type" : "_doc",
  11. "_id" : "1",
  12. "_version" : 2
  13. ,
  14. "result" : "updated",
  15. "_shards" : {
  16. "total" : 2,
  17. "successful" : 1,
  18. "failed" : 0
  19. },
  20. "_seq_no" : 2,
  21. "_primary_term" : 1
  22. }

在这里插入图片描述
7. 删除文档 / 删除索引:

  1. DELETE test1/_doc/1
  2. DELETE test1

Restful 风格操作文档:

1. 创建索引并添加数据:

  1. PUT /blu/user/1
  2. {
  3. "name": "法外狂徒张三",
  4. "age": 23,
  5. "desc": "Hello! My name is 张三",
  6. "tags": ["Spring","Mybatis","JAVA","MySQL"]
  7. }
  8. PUT /blu/user/2
  9. {
  10. "name": "李四",
  11. "age": 30,
  12. "desc": "Hello! My name is 李四",
  13. "tags": ["SpringBoot","JPA","Python","Oracle"]
  14. }
  15. PUT /blu/user/3
  16. {
  17. "name": "王五",
  18. "age": 65,
  19. "desc": "Hello! My name is 王五",
  20. "tags": ["Hibernate","C++","ElasticSearch"]
  21. }
  22. PUT /blu/user/4
  23. {
  24. "name": "小张三",
  25. "age": 24,
  26. "desc": "Hello! My name is 张三",
  27. "tags": ["Hibernate","C","ElasticSearch"]
  28. }

2. PUT 更新数据(不推荐):

  1. PUT /blu/user/2
  2. {
  3. "name": "lisi23333",
  4. "age": 30,
  5. "desc": "Hello! My name is lisi",
  6. "tags": ["SpringBoot","JPA","Python","Oracle"]
  7. }

3. POST 更新数据(推荐):

  1. POST /blu/user/2/_update
  2. {
  3. "doc":{
  4. "name": "lisihahaha"
  5. }
  6. }

4. 查询:

  1. GET blu/user/1
  2. GET blu/user/_search?q=name:法外狂徒
  3. GET blu/user/_search
  4. {
  5. "query": {
  6. "match": {
  7. "name": "法外狂徒"
  8. }
  9. }
  10. }
  11. 结果过滤:查询name中带张三的文档数据,只返回namedesc字段
  12. GET blu/user/_search
  13. {
  14. "query": {
  15. "match": {
  16. "name": "张三"
  17. }
  18. },
  19. "_source": ["name","desc"]
  20. }
  21. 排序查询:此时结果的_score字段为null
  22. GET blu/user/_search
  23. {
  24. "query": {
  25. "match": {
  26. "name": "张三"
  27. }
  28. },
  29. "sort": [
  30. {
  31. "age": {
  32. "order": "desc"
  33. }
  34. }
  35. ]
  36. }
  37. 分页查询:form从第几个数据开始(0开始),size每页几个数据
  38. GET blu/user/_search
  39. {
  40. "query": {
  41. "match": {
  42. "name": "张三"
  43. }
  44. },
  45. "sort": [
  46. {
  47. "age": {
  48. "order": "asc"
  49. }
  50. }
  51. ],
  52. "from": 0,
  53. "size": 2
  54. }
  55. 多条件查询:
  56. must 表示所有条件都要符合,等价and
  57. should 表示只要符合一个条件即可,等价or
  58. must_not 表示查询过滤该条件的文档数据,等价!=
  59. GET blu/user/_search
  60. {
  61. "query": {
  62. "bool": {
  63. "must": [
  64. {
  65. "match": {
  66. "name": "张三"
  67. }
  68. },
  69. {
  70. "match": {
  71. "age": "14"
  72. }
  73. }
  74. ]
  75. }
  76. }
  77. }
  78. 范围过滤,查询name带张三,年龄大于10,小于等于20的数据:
  79. GET blu/user/_search
  80. {
  81. "query": {
  82. "bool": {
  83. "must": [
  84. {
  85. "match": {
  86. "name": "张三"
  87. }
  88. }
  89. ],"filter": [
  90. {
  91. "range": {
  92. "age": {
  93. "gt": 10,
  94. "lte": 20
  95. }
  96. }
  97. }
  98. ]
  99. }
  100. }
  101. }
  102. 多条件匹配:查询name中有张三或四的,条件使用空格隔开
  103. GET blu/user/_search
  104. {
  105. "query": {
  106. "match": {
  107. "name": "张三 四"
  108. }
  109. }
  110. }
  111. 查询结果高亮:在查询出的结果中,搜索关键字会自动添加<em>标签
  112. GET blu/user/_search
  113. {
  114. "query": {
  115. "match": {
  116. "name": "张三"
  117. }
  118. },
  119. "highlight": {
  120. "fields": {
  121. "name":{ }
  122. }
  123. }
  124. }
  125. 自定义高亮标签:
  126. GET blu/user/_search
  127. {
  128. "query": {
  129. "match": {
  130. "name": "张三"
  131. }
  132. },
  133. "highlight": {
  134. "pre_tags": "<span class='key' style='color:red'>",
  135. "post_tags": "</span>",
  136. "fields": {
  137. "name":{ }
  138. }
  139. }
  140. }

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RESTful风格

    写这篇,主要是之前呀,和团队一起接项目,我作为后台,既然把对数据的增删改查定义了4个不同uri接口。 所以我要用统一规范,对一个数据的增删改查只定义一个uri,不同的请求做相

    相关 restful风格

    概念          一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,

    相关 Restful风格

    什么是REST REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用