当前位置:   article > 正文

Elasticsearch增删改(postman)_postman elasticsearch

postman elasticsearch

目录

前言:

Rest Api

查看全部索引:

请求URL:

响应内容:

创建索引:

请求URL:

body内容:

查看索引:

请求URL:

响应内容:

查询索引全部数据:

请求URL:

响应内容:

或者:

请求方式:POST

请求URL:

Body内容:

新增索引数据:

请求URL:

body内容:

响应内容:

修改索引数据:

请求URL:

body内容:

响应内容:

普通匹配查询:

请求URL:

body内容:

 响应内容:

新增索引字段:

请求URL:

body内容:

响应内容:

删除索引:

请求URL:

删除索引具体数据:

请求URL:

body内容:

body内容:

Json批量新增Es数据

文件格式:

请求方式:POST

请求URL:

Es全局更新字段值:

请求URL:

请求方式:POST

请求体:

Es索引新加某字段:

请求URL:

请求方式:PUT

请求体:

Es筛选数据

请求URL:

请求方式:GET

请求体:

Es多字段匹配数据

请求URL:

请求方式:GET

请求体:

Es全量更新特定值

请求URL:

请求方式:POST

请求体:

前言:

Elasticsearch是一个非常强大的搜索引擎,它可以帮我们对数据进行存储,并快速地搜索及分析数据,如果对Elasticsearch感兴趣的话,可以看一下ElasticSearch7.9.0安装和开启登录密码_Java王小怪的博客-CSDN博客

Elasticsearch在某种程度上我们都是作为数据库使用,那么它肯定是和数据库有着相似之处的。Elasticsearch最关键的就是索引、类型和映射了,其中:

索引_index:(相当于数据库名称);类型 _type:(相当于库中的表,默认为_doc);映射_mapping:(相当于数据库中表的字段)。

Rest Api

查看全部索引:

请求URL:

GET  http://localhost:9200/_cat/indices?v

如果开启了登陆密码,记得在postman中配置Authorization

响应内容:

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. green open .security-7 iB-5JsanSAG4hVvMkg9EIw 1 0 7 0 25.4kb 25.4kb
  3. green open books WrAL0CMrQuimG7dnMqy0Rg 1 0 5 1 22.6kb 22.6kb

 解释返回的数据体内容:

  • health: 集群的健康状态,可以是green(健康)、yellow(可能存在问题)或red(严重问题)。
  • status: 索引的状态,可以是open(开放搜索)、close(关闭搜索)或hidden(不可用于搜索,可能是因为设置了index.hidden属性)。
  • index: 索引的名称。
  • uuid: 索引的唯一标识符。
  • pri: 主分片的数量。
  • rep: 副本分片的数量。
  • docs.count: 索引中文档的数量。
  • docs.deleted: 自上次刷新以来已删除的文档数量。
  • store.size: 索引存储的大小(以字节为单位)。
  • pri.store.size: 主分片存储的大小(以字节为单位)。

创建索引:

请求URL:

PUT http://localhost:9200/books

body内容:

  1. {
  2. "settings":{
  3. "number_of_shards":3,
  4. "number_of_replicas":2
  5. },
  6. "mappings":{
  7. "log_doc":{
  8. "properties":{
  9. "id": {
  10. "type": "keyword"
  11. },
  12. "language": {
  13. "type": "keyword"
  14. },
  15. "price": {
  16. "type": "keyword"
  17. },
  18. "remark": {
  19. "type": "keyword"
  20. },
  21. "title": {
  22. "type": "keyword"
  23. },
  24. "description": {
  25. "type": "keyword"
  26. }
  27. }
  28. }
  29. }
  30. }

查看索引:

请求URL:

GET http://localhost:9200/books

响应内容:

  1. {
  2. "books": {
  3. "aliases": {},
  4. "mappings": {
  5. "properties": {
  6. "author": {
  7. "type": "keyword"
  8. },
  9. "description": {
  10. "type": "text"
  11. },
  12. "id": {
  13. "type": "keyword"
  14. },
  15. "language": {
  16. "type": "keyword"
  17. },
  18. "price": {
  19. "type": "float"
  20. },
  21. "remark": {
  22. "type": "keyword"
  23. },
  24. "title": {
  25. "type": "text"
  26. }
  27. }
  28. },
  29. "settings": {
  30. "index": {
  31. "refresh_interval": "1s",
  32. "number_of_shards": "1",
  33. "provided_name": "books",
  34. "creation_date": "1691123775089",
  35. "store": {
  36. "type": "fs"
  37. },
  38. "number_of_replicas": "0",
  39. "uuid": "WrAL0CMrQuimG7dnMqy0Rg",
  40. "version": {
  41. "created": "7090099"
  42. }
  43. }
  44. }
  45. }
  46. }

查询索引全部数据:

请求URL:

GET  http://localhost:9200/books/_search

响应内容:

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "books",
  19. "_type": "esbook",
  20. "_id": "1",
  21. "_score": 1.0,
  22. "_source": {
  23. "id": "1",
  24. "title": "Java 程序设计",
  25. "language": "汉语",
  26. "author": "盖伦",
  27. "price": 18.88,
  28. "description": "哈哈嗨"
  29. }
  30. },
  31. {
  32. "_index": "books",
  33. "_type": "esbook",
  34. "_id": "2",
  35. "_score": 1.0,
  36. "_source": {
  37. "id": "2",
  38. "title": "Python程序设计",
  39. "language": "英语",
  40. "author": "赵信",
  41. "price": 66.88,
  42. "description": "陷阵之至有死无生"
  43. }
  44. },
  45. {
  46. "_index": "books",
  47. "_type": "esbook",
  48. "_id": "3",
  49. "_score": 1.0,
  50. "_source": {
  51. "id": "3",
  52. "title": "PHP 程序设计",
  53. "language": "test",
  54. "author": "宝石",
  55. "price": 88.88,
  56. "description": "我曾踏足山巅,也曾跌入低谷"
  57. }
  58. },
  59. {
  60. "_index": "books",
  61. "_type": "esbook",
  62. "_id": "TWoQy4oBtacv4iJ4HMye",
  63. "_score": 1.0,
  64. "_source": {
  65. "id": "6",
  66. "title": "新增title",
  67. "language": "language",
  68. "author": "xg",
  69. "price": 2799,
  70. "description": "K60Utrl"
  71. }
  72. }
  73. ]
  74. }
  75. }

或者:

请求方式:POST

请求URL:

http://localhost:9200/sdwan_device/_search

Body内容:

  1. {
  2.     "query": {
  3.         "match_all":  {} 
  4.     } 
  5. }

新增索引数据:

请求URL:

POST http://localhost:9200/books/_doc

body内容:

  1. {
  2. "id": "7",
  3. "title": "新增title",
  4. "language": "新增language",
  5. "author": "xg",
  6. "price": 2999,
  7. "description": "Redmi K60Ultra"
  8. }

响应内容:

  1. {
  2. "_index": "books",
  3. "_type": "_doc",
  4. "_id": "UWony4oBtacv4iJ46czs",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 1,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 6,
  13. "_primary_term": 3
  14. }

修改索引数据:

请求URL:

POST  http://localhost:9200/books/_doc/UWony4oBtacv4iJ46czs/_update

body内容:

  1. {
  2. "doc": {
  3. "author": "卢十瓦"
  4. }
  5. }

响应内容:

  1. {
  2. "_index": "books",
  3. "_type": "_doc",
  4. "_id": "UWony4oBtacv4iJ46czs",
  5. "_version": 2,
  6. "result": "updated",
  7. "_shards": {
  8. "total": 1,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 7,
  13. "_primary_term": 3
  14. }

普通匹配查询:

请求URL:

POST  http://localhost:9200/books/_search

body内容:

  1. {
  2. "query": {
  3. "term": {
  4. "id": 7
  5. }
  6. }
  7. }

 响应内容:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.1631508,
  16. "hits": [
  17. {
  18. "_index": "books",
  19. "_type": "esbook",
  20. "_id": "UWony4oBtacv4iJ46czs",
  21. "_score": 1.1631508,
  22. "_source": {
  23. "id": "7",
  24. "title": "新增title",
  25. "language": "新增language",
  26. "author": "卢十瓦",
  27. "price": 2999,
  28. "description": "Redmi K60Ultra"
  29. }
  30. }
  31. ]
  32. }
  33. }

新增索引字段:

请求URL:

PUT http://localhost:9200/books/_mapping

body内容:

  1. {
  2. "properties": {
  3. "createTime": {
  4. "type": "date",
  5. "format": "yyyy-MM-dd HH:mm:ss"
  6. }
  7. }
  8. }

响应内容:

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.540445,
  16. "hits": [
  17. {
  18. "_index": "books",
  19. "_type": "esbook",
  20. "_id": "UWony4oBtacv4iJ46czs",
  21. "_score": 1.540445,
  22. "_source": {
  23. "id": "7",
  24. "title": "新增title",
  25. "language": "新增language",
  26. "author": "卢十瓦",
  27. "price": 2999,
  28. "description": "Redmi K60Ultra",
  29. "createTime": "2023-09-25 15:18:53"
  30. }
  31. }
  32. ]
  33. }
  34. }

删除索引:

请求URL:

DELETE http://localhost:9200/books

删除索引具体数据:

请求URL:

POST http://localhost:9200/books/_doc/_delete_by_query

body内容:

  1. {
  2. "query": {
  3. "match": {
  4. "id": "1"
  5. }
  6. }
  7. }

按照日期删除数据:

body内容:

  1. {
  2. "query": {
  3. "bool": {
  4. "filter": [
  5. {
  6. "range": {
  7. "createTime": {
  8. "from": "2023-08-01 00:00:00",
  9. "to": "2023-08-31 23:59:59"
  10. }
  11. }
  12. }
  13. ]
  14. }
  15. }
  16. }

2023/11/15新增: 

Json批量新增Es数据

文件格式:

  1. {"index":{"_index":"books","_type":"_doc"}}
  2. {"id":"d980674","title":"三国演义","language":"汉语","author":"罗贯中","price":158,"description":"四大名著","create Time":"2023-11-15 09:00"}
  3. {"index":{"_index":"books","_type":"_doc"}}
  4. {"id":"d9sad462","title":"海底两万里","language":"English","author":"儒勒·凡尔纳","price":19,"description":"是一本小说","create Time":"2023-11-15 09:00"}

请求方式:POST

请求URL:

http://localhost:9200/_bulk

Body中选择binary格式,选中文件发送请求即可。

------------------------------分割线-------------------------------------------

2024/04/16更新

Es全局更新字段值:

目的:把索引下全部的siteId值更新为"1234567"。

请求URL:

http://localhost:9200/index/_update_by_query

请求方式:POST

请求体:

  1. {
  2. "script": {
  3. "source": "ctx._source.siteId = \"1234567\""
  4. },
  5. "query": {
  6. "match_all": {}
  7. }
  8. }

Es索引新加某字段:

请求URL:

http://localhost/index/_mapping

请求方式:PUT

请求体:

  1. {
  2. "properties": {
  3. "name": {
  4. "type": "keyword"
  5. }
  6. }
  7. }

注意:name为新加的字段,keyword为字段类型

Es筛选数据

目的:筛选deviceNum为"2e0ea896-123ferwa"的数据,并根据timestamp倒序排列。

请求URL:

http:localhost:9200/index/_search

请求方式:GET

请求体:

  1. {
  2. "size": 500,
  3. "query": {
  4. "match": {
  5. "deviceNum": "2e0ea896-123ferwa"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "timestamp": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

---------------------------------------------------分割线-------------------------------------------------------------------

2024/04/16更新

Es多字段匹配数据

请求URL:

http:localhost:9200/index/_search

请求方式:GET

请求体:

  1. {
  2. "size": "500",
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "tenantId": "103245"
  9. }
  10. },
  11. {
  12. "terms": {
  13. "cityId": [
  14. 101,
  15. 202,
  16. 303
  17. ]
  18. }
  19. }
  20. ]
  21. }
  22. },
  23. "sort": {
  24. "dateTime": "desc"
  25. }
  26. }

注意:查询tenantId为103245,且cityId是101、202、303的数据,并且按照dateTime进行倒叙排列。

---------------------------------------------------分割线-------------------------------------------------------------------

2024/05/28更新

Es全量更新特定值

目的:把siteId值为"00000"的数据全量更新为"6666"。

请求URL:

http:localhost:9200/index/_search

请求方式:POST

请求体:

  1. {
  2.     "query": {
  3.         "bool": {
  4.             "must": [
  5.                 {
  6.                     "exists": {
  7.                         "field""siteId"
  8.                     }
  9.                 },
  10.                 {
  11.                     "term": {
  12.                         "siteId""00000"
  13.                     }
  14.                 }
  15.             ]
  16.         }
  17.     },
  18.     "script": {
  19.         "source""ctx._source.siteId= \"6666\""
  20.     }
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/966399
推荐阅读
相关标签
  

闽ICP备14008679号