当前位置:   article > 正文

elasticsearch 常用命令_elasticsearch 命令

elasticsearch 命令

给es设置连接密码

1、修改es根目录下的config/elasticsearch.yml配置文库,写入下面配置。

  1. xpack.security.enabled: true
  2. xpack.license.self_generated.type: basic
  3. xpack.security.transport.ssl.enabled: true

2.启动es(切换es用户)

3.设置密码,进入bin目录下执行命令。

./elasticsearch-setup-passwords interactive
指令交互过程中需要这只很多个密码

测试访问

默认账号:elastic

浏览器访问:http://localhost:9200/,此时浏览器弹出需要密码了。

访问控制台:http://127.0.0.1:9100/?auth_user=elastic&auth_password=密码

es基本操作命令

es 没有表的概念,每一个索引就是一个库,数据都放在库下面

创建索引

类似mysql创建表

  1. # 创建名称为shapping的索引
  2. PUT http://127.0.0.1:9200/shapping

返回结果:

  1. {
  2. "acknowledged": true, //创建成功
  3. "shards_acknowledged": true, //示索引创建、更新或删除的操作是否已被所有相关分片确认。
  4. "index": "shapping" //索引名为shapping
  5. }

索引具有幂等性,重复创建会报错

查看shapping索引(查看某个索引)

  1. get http://127.0.0.1:9200/shapping
  2. {
  3. "shapping": {
  4. "aliases": {},
  5. "mappings": {},
  6. "settings": {
  7. "index": {
  8. "routing": {
  9. "allocation": {
  10. "include": {
  11. "_tier_preference": "data_content"
  12. }
  13. }
  14. },
  15. "number_of_shards": "1",
  16. "provided_name": "shapping",
  17. "creation_date": "1692721844404",
  18. "number_of_replicas": "1",
  19. "uuid": "VeVq2ZAWQAyJtAcjmW6z8Q",
  20. "version": {
  21. "created": "8020199"
  22. }
  23. }
  24. }
  25. }
  26. }

查看全部索引

  1. get http://127.0.0.1:9200/_cat/indices?v
  2. v : 显示详细信息
  3. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  4. yellow open shapping VeVq2ZAWQAyJtAcjmW6z8Q 1 1 0 0 225b 225b

删除索引

  1. delete http://127.0.0.1:9200/shapping
  2. {
  3. "acknowledged": true
  4. }

创建文档(添加数据

  1. 请求方式 :post
  2. 路径 : http://127.0.0.1:9200/shapping/_doc
  3. http://127.0.0.1:9200/shapping/_doc/id #自己指定ID
  4. //也可以吧_doc换成_create,这样看起来更加规范 http://127.0.0.1:9200/shapping/_create/ID
  5. //这样请求会自动创建ID,如:"_id": "4iMnHooBhKSDaY6labt-",
  6. //自定义创建ID方式在_doc后面加上ID : http://127.0.0.1:9200/shapping/_doc/1001, 1001 是这个数据的ID
  7. 请求体:
  8. {
  9. "title" : "小米手机",
  10. "img" : "http://www.test.com/1.jpg",
  11. "price" : "3999"
  12. }
  13. 返回结果:
  14. {
  15. "_index": "shapping",
  16. "_id": "4iMnHooBhKSDaY6labt-",
  17. "_version": 1,
  18. "result": "created",
  19. "_shards": {
  20. "total": 2,
  21. "successful": 1,
  22. "failed": 0
  23. },
  24. "_seq_no": 0,
  25. "_primary_term": 1
  26. }

根据ID查询索引下数据

  1. get http://127.0.0.1:9200/shapping/_doc/1000
  2. 返回:
  3. {
  4. "_index": "shapping",
  5. "_id": "1000",
  6. "_version": 1,
  7. "_seq_no": 1,
  8. "_primary_term": 1,
  9. "found": true,
  10. "_source": {
  11. "title": "小米手机",
  12. "img": "http://www.test.com/1.jpg",
  13. "price": "3999"
  14. }
  15. }

 查询索引下的所有数据

  1. get http://127.0.0.1:9200/shapping/_search
  2. {
  3. "took": 328,
  4. "timed_out": false,
  5. "_shards": {
  6. "total": 1,
  7. "successful": 1,
  8. "skipped": 0,
  9. "failed": 0
  10. },
  11. "hits": {
  12. "total": {
  13. "value": 2,
  14. "relation": "eq"
  15. },
  16. "max_score": 1.0,
  17. "hits": [
  18. {
  19. "_index": "shapping",
  20. "_id": "4iMnHooBhKSDaY6labt-",
  21. "_score": 1.0,
  22. "_source": {
  23. "title": "小米手机",
  24. "img": "http://www.test.com/1.jpg",
  25. "price": "3999"
  26. }
  27. },
  28. {
  29. "_index": "shapping",
  30. "_id": "1000",
  31. "_score": 1.0,
  32. "_source": {
  33. "title": "小米手机",
  34. "img": "http://www.test.com/1.jpg",
  35. "price": "3999"
  36. }
  37. }
  38. ]
  39. }
  40. }

 根据ID修改某条数据

局部数据修改(推荐)

  1. post http://127.0.0.1:9200/shapping/_update/1000
  2. 请求json体
  3. // 华为修改为苹果
  4. {
  5. "doc" :{
  6. "title" : "苹果手机"
  7. }
  8. }


全量修改:
需要把json中的每个字段都写上(不推荐)

  1. put http://127.0.0.1:9200/shapping/_doc/1000
  2. //需要修改的数据
  3. {
  4. "title" : "华为手机", //小米改华为
  5. "img" : "http://www.test.com/123.jpg",
  6. "price" : "399999"
  7. }

根据ID删除数据

delete  http://127.0.0.1:9200/shapping/_doc/1000

给索引添加映射关系

【1】

  1. PUT /demo/_mapping
  2. {
  3. "properties":{
  4. "name":{
  5. "type":"text", // 类型为text 【text会被分词】
  6. "index":true //当前name建立索引【可以被索引查询】
  7. },
  8. "age":{
  9. "type":"keyword", // 不会被分词
  10. "index":true
  11. },
  12. "tel":{
  13. "type":"keyword",
  14. "index":false // 索引中不缓存tel,不能通过tel 搜索
  15. }
  16. }
  17. }

 多字段模式

 时间格式自动识别

查看所有索引

GET _cat/indices?v

查询指定的索引信息

GET api

给索引添加数据

  1. POST /api/_doc #给api索引添加数据,自动分配ID
  2. #/api/_doc/1 指定ID1
  3. #/api/_create/100
  4. # _create == _doc
  5. {
  6. "name":"123"
  7. }

查询文档信息

  1. GET api/_doc/1 #查询指定文档
  2. GET /api/_search #查询所有文档

删除指定索引

DELETE /api

修改文档记录

全部修改】[不建议使用]

  1. PUT /api/_doc/1
  2. #全部修改
  3. {
  4. "name":"66666"
  5. }
  6. return
  7. {
  8. "_index": "api",
  9. "_id": "1",
  10. "_version": 2,
  11. "result": "updated",
  12. "_shards": {
  13. "total": 2,
  14. "successful": 1,
  15. "failed": 0
  16. },
  17. "_seq_no": 17,
  18. "_primary_term": 1
  19. }

部分修改

  1. POST /api/_update/1000
  2. {
  3. "doc":{
  4. "age":666
  5. }
  6. }

es查询

url查询

  1. q: 查询条件 age = 1
  2. http://127.0.0.1:9200/api/_search?q=age:1

body请求体查询

  1. get /api/_search
  2. // 查询name=a,根据age 小到大排序
  3. {
  4. "query" : { //query 查询
  5. "match":{ //match 匹配查询 match_all 查询所有
  6. "name":"a" //要查询的字段name=a
  7. }
  8. },
  9. "from":0, //分页其实值
  10. "size":6 , //查询总数
  11. "_source":["age"] , //只显示age
  12. "sort":{
  13. "age":{ // 根据age循序排序
  14. "order":"asc"
  15. }
  16. }
  17. }
  18. ==================================
  19. return
  20. {
  21. "took": 1,
  22. "timed_out": false,
  23. "_shards": {
  24. "total": 1,
  25. "successful": 1,
  26. "skipped": 0,
  27. "failed": 0
  28. },
  29. "hits": {
  30. "total": {
  31. "value": 13, //返回匹配的总数
  32. "relation": "eq"
  33. },
  34. "max_score": null,
  35. "hits": [
  36. {
  37. "_index": "api",
  38. "_id": "YkFG_oABrskZ6DWlXrba",
  39. "_score": null,
  40. "_source": {
  41. "age": 1
  42. },
  43. "sort": [
  44. 1
  45. ]
  46. }
  47. ]
  48. }
  49. }

多条件查询

  1. {
  2. "query" : {
  3. "bool":{
  4. "should":[ // 必须一个条件即可 or
  5. {
  6. "match":{
  7. "name" :"a"
  8. }
  9. },
  10. {
  11. "match":{
  12. "age":5
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

  1. {
  2. "query" : {
  3. "bool":{
  4. "must":[ // 同时满足以下条件
  5. {
  6. "match":{
  7. "name" :"小米"
  8. }
  9. },
  10. {
  11. "match":{
  12. "status":1
  13. }
  14. }
  15. ],
  16. "filter":{ //二次过滤
  17. "range": {
  18. "money": {
  19. "gt":1000
  20. }
  21. }
  22. }
  23. }
  24. },
  25. "highlight" : { //高亮显示
  26. "fields":{
  27. "name": { //高亮显示的字段
  28. "pre_tags": ["<strong>"], //亮显示的字段的起始标签
  29. "post_tags": ["</strong>"] //亮显示的字段的结束标签
  30. }
  31. }
  32. }
  33. }

聚合查询

  1. // get /api/_search
  2. {
  3. "aggs":{ //聚合操作
  4. "money_group":{ //分组名称【随便取名】
  5. "terms":{ //分组关键词【类似mysql group关键词】
  6. "field":"money" //分组的字段
  7. }
  8. },
  9. "money_avg":{ //平均数名 随便取名
  10. "avg":{ //平均数
  11. "field":"money" //需要求平均数的字段
  12. }
  13. }
  14. },
  15. "size":0 //只看聚合数据内容,不查看原始数据
  16. }
  17. //【return】
  18. {
  19. "took": 52,
  20. "timed_out": false,
  21. "_shards": {
  22. "total": 1,
  23. "successful": 1,
  24. "skipped": 0,
  25. "failed": 0
  26. },
  27. "hits": {
  28. "total": {
  29. "value": 64,
  30. "relation": "eq"
  31. },
  32. "max_score": null,
  33. "hits": []
  34. },
  35. "aggregations": {
  36. "money_avg": {
  37. "value": 9680.818181818182
  38. },
  39. "money_group": {
  40. "doc_count_error_upper_bound": 0,
  41. "sum_other_doc_count": 6,
  42. "buckets": [
  43. {
  44. "key": 2999,
  45. "doc_count": 3
  46. },
  47. {
  48. "key": 5999,
  49. "doc_count": 3
  50. },
  51. {
  52. "key": 1999,
  53. "doc_count": 2
  54. },
  55. {
  56. "key": 3999,
  57. "doc_count": 2
  58. },
  59. {
  60. "key": 299,
  61. "doc_count": 1
  62. },
  63. {
  64. "key": 399,
  65. "doc_count": 1
  66. },
  67. {
  68. "key": 699,
  69. "doc_count": 1
  70. },
  71. {
  72. "key": 799,
  73. "doc_count": 1
  74. },
  75. {
  76. "key": 999,
  77. "doc_count": 1
  78. },
  79. {
  80. "key": 4999,
  81. "doc_count": 1
  82. }
  83. ]
  84. }
  85. }
  86. }

多层json映射与查询

1.创建多层json映射

  1. PUT /test/_mapping
  2. {
  3. "mappings":{
  4. "properties":{
  5. "name":{
  6. "type":"text",
  7. "index":true
  8. },
  9. "age":{
  10. "type":"keyword",
  11. "index":true
  12. },
  13. "tel":{
  14. "properties":{
  15. "msg":{
  16. "type":"keyword",
  17. "index":true
  18. },
  19. "data":{
  20. "properties":{
  21. "resu":{
  22. "type":"text",
  23. "index":true
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }

2.给映射结构添加数据

  1. post /test/_doc/
  2. {
  3. "name":"小米3",
  4. "age":3,
  5. "tel":{
  6. "msg": 3,
  7. "data":{
  8. "resu": "hello world"
  9. }
  10. }
  11. }

3.查询多层json

  1. get /test/_search
  2. {
  3. "query" : {
  4. "match":{
  5. "tel.data.resu":"hello"
  6. }
  7. }
  8. }
  9. //【return】
  10. {
  11. "took": 1,
  12. "timed_out": false,
  13. "_shards": {
  14. "total": 1,
  15. "successful": 1,
  16. "skipped": 0,
  17. "failed": 0
  18. },
  19. "hits": {
  20. "total": {
  21. "value": 2,
  22. "relation": "eq"
  23. },
  24. "max_score": 0.52354836,
  25. "hits": [
  26. {
  27. "_index": "test",
  28. "_id": "hkFkBIEBrskZ6DWlrbYT",
  29. "_score": 0.52354836,
  30. "_source": {
  31. "name": "小米",
  32. "age": 1,
  33. "tel": {
  34. "msg": 1,
  35. "data": {
  36. "resu": "hello"
  37. }
  38. }
  39. }
  40. },
  41. {
  42. "_index": "test",
  43. "_id": "iEFlBIEBrskZ6DWlR7aJ",
  44. "_score": 0.39019167,
  45. "_source": {
  46. "name": "小米3",
  47. "age": 3,
  48. "tel": {
  49. "msg": 3,
  50. "data": {
  51. "resu": "hello world"
  52. }
  53. }
  54. }
  55. }
  56. ]
  57. }
  58. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/518861
推荐阅读
相关标签
  

闽ICP备14008679号