当前位置:   article > 正文

总结工作中常用ES脚本

es脚本

总结工作中常用ES脚本,方便日后查阅,此次脚本应用于es5

增删操作

建立索引

curl -XPUT 'http://192.168.6.122:9200/major_info'

完整创建

curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json'  'http://127.0.0.1:9200/farer_index' -d@test.json  

test.json

  1. {
  2. "settings":{
  3. "number_of_shards":5,
  4. "number_of_replicas":0
  5. },
  6. "mappings":{
  7. "article":{
  8. "dynamic":"strict",
  9. "_all":{
  10. "enabled":false
  11. },
  12. "properties":{
  13. "title":{
  14. "type":"string",
  15. "index":"analyzed",
  16. "analyzer":"ik_max_word",
  17. "search_analyzer":"ik_max_word"
  18. },
  19. "describe":{
  20. "type":"string",
  21. "index":"analyzed",
  22. "analyzer":"ik_max_word",
  23. "search_analyzer":"ik_max_word"
  24. },
  25. "content":{
  26. "type":"string",
  27. "index":"analyzed",
  28. "analyzer":"ik_max_word",
  29. "search_analyzer":"ik_max_word"
  30. },
  31. "author":{
  32. "type":"string",
  33. "index":"no"
  34. },
  35. "time":{
  36. "type":"date",
  37. "index":"not_analyzed",
  38. "format":"yyyy-MM-dd HH:mm:ss"
  39. }
  40. }
  41. }
  42. }
  43. }

 删除索引


curl -XDELETE 'http://192.168.6.122:9200/major_info_202006'

mapping操作

设置mapping

curl -XPUT '192.168.10.183:9200/test2/entities/_mapping' -d@test.json

mapping文件样例

  1. {
  2. "entities" : {
  3. "dynamic" : "false",
  4. "properties" : {
  5. "concept_id" : {
  6. "type" : "long",
  7. "store" : true
  8. },
  9. "entity_id" : {
  10. "type" : "long",
  11. "store" : true
  12. },
  13. "entity_name" : {
  14. "type" : "text",
  15. "store" : true
  16. }
  17. }
  18. }
  19. }

拉取mapping


curl -XGET '192.168.6.122:9200/weibo_info_202002/_mapping?pretty' > weibo_info_202002.json

整体状况

查看实体以及数据量


curl -XGET 'http://192.168.6.122:9200/_cat/indices?v'

查看所有节点

curl '10.128.55.188:9200/_cat/nodes?v'

健康状况检查

来源

elasticsearch 查看集群健康、节点状态_u011250186的博客-CSDN博客_es查看集群健康状态

 elasticsearch启动后查看是否启动成功:
curl -XGET"http://$(hostname):9200/_cluster/health?pretty=true"

2. 停止elasticsearch应用:
curl -XPOST "http:// $(hostname):9200/_shutdown"

3. 查看集群健康:
curl $(hostname):9200/_cluster/health?pretty

4. 检查集群状态:
curl $(hostname):9200/_cluster/stats?pretty

5. 节点状态:
curl $(hostname):9200/_nodes/process?pretty

curl $(hostname):9200/_nodes/node1/process?pretty

6. 当你不知道有那些属性可以查看时:
curl '$(hostname):9200/_cat/',会返回可以查看的属性


查看指定索引库的健康状态
http://localhost:9200/_cluster/health/index_name?pretty
http://localhost:9200/_cluster/health/index_name,index_name2?pretty

模板相关操作

来源

es 使用curl创建索引模板_laimao8079的博客-CSDN博客_es创建索引模板

创建模板 

curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json'  'http://127.0.0.1:9200/_template/people_trail' -d@tmp_people_trail.json 

 tmp_people_trail.json

  1. {
  2. "template":"people_trail*",
  3. "settings":{
  4. "index":{
  5. "number_of_shards":"1",
  6. "number_of_replicas":"0",
  7. "analysis":{
  8. "analyzer":{
  9. "pinyin":{
  10. "tokenizer":"pinyin_tokenizer"
  11. }
  12. },
  13. "tokenizer":{
  14. "pinyin_tokenizer":{
  15. "type":"pinyin",
  16. "keep_joined_full_pinyin":true
  17. }
  18. }
  19. }
  20. }
  21. },
  22. "mappings":{
  23. "_default_":{
  24. "_all":{
  25. "enabled":true,
  26. "analyzer":"standard"
  27. },
  28. "dynamic_templates":[
  29. {
  30. "xms":{
  31. "mapping":{
  32. "fields":{
  33. "py":{
  34. "analyzer":"pinyin",
  35. "type":"text"
  36. }
  37. },
  38. "type":"keyword"
  39. },
  40. "match_mapping_type":"string",
  41. "match":"XM"
  42. }
  43. },
  44. {
  45. "strings":{
  46. "match_mapping_type":"string",
  47. "mapping":{
  48. "type":"keyword",
  49. "fields":{
  50. "full":{
  51. "type":"text",
  52. "analyzer":"standard"
  53. }
  54. }
  55. }
  56. }
  57. }
  58. ]
  59. }
  60. }
  61. }

新版本

  1. {
  2. "index_patterns": ["my_index*"],
  3. "aliases": {
  4. "my_alias": {}
  5. },
  6. "settings": {
  7. "number_of_shards": 1,
  8. "number_of_replicas": 1
  9. },
  10. "mappings": {
  11. "properties": {
  12. "field1": {
  13. "type": "text"
  14. },
  15. "field2": {
  16. "type": "keyword"
  17. }
  18. }
  19. }
  20. }

旧版本

  1. {
  2. "template":"log_info*",
  3. "mappings":{
  4. "article":{
  5. "dynamic":"strict",
  6. "_all":{
  7. "enabled":false
  8. },
  9. "properties":{
  10. "title":{
  11. "type":"string",
  12. "index":"analyzed",
  13. "analyzer":"ik_max_word",
  14. "search_analyzer":"ik_max_word"
  15. },
  16. "author":{
  17. "type":"string",
  18. "index":"no"
  19. },
  20. "itemId":{
  21. "type":"long"
  22. },
  23. "site":{
  24. "type":"keyword"
  25. },
  26. "time":{
  27. "type":"date",
  28. "index":"not_analyzed",
  29. "format":"yyyy-MM-dd HH:mm:ss"
  30. },
  31. "laudio":{
  32. "type":"nested",
  33. "properties":{
  34. }
  35. },
  36. "nfri":{
  37. "type":"long"
  38. }
  39. }
  40. }
  41. }
  42. }

查询所有模板

curl -XGET 127.0.0.1:9200/_cat/templates

查看某一个模板

curl -XGET http://127.0.0.1:9200/_template/article_type?pretty

删除一个索引模板

curl -XDELETE http://127.0.0.1:9200/_template/article_type

添加别名

  1. curl -H "Content-Type: application/json" -XPOST 'http://192.168.6.122:9200/_aliases' -d '
  2. {
  3. "actions": [
  4. {"add": {"index": "major_info_202007_v2", "alias": "main_info"}}
  5. ]
  6. }'

查看索引别名


curl -XGET http://192.168.6.122:9200/major_info_202007/_aliases

查看别名下的索引


curl -XGET '192.168.6.122:9200/_alias/major_info'

删除别名

  1. curl -XPOST 'http://192.168.6.122:9200/_aliases' -d '
  2. {
  3. "actions" : [
  4. { "remove" : { "index" : "major_info_202007_v2","alias" : "major_info" } }
  5. ]
  6. }'

查看所有别名

curl -XGET '192.168.6.122:9200/_alias'

查询操作

查询


curl -XGET http://192.168.6.122:9200/major_info_202007/major_info_202007/e7af0e55d1307e021f8eb7594bfd3737?pretty

搜索


curl -XGET 'http://192.168.6.122:9200/major_info/major_info/_search' -d '{"query":{"match":{"uuid":""}}}'

 reindex

  1. curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "source": {
  4. "index": "twitter"
  5. },
  6. "dest": {
  7. "index": "new_twitter"
  8. }
  9. }
  10. '

按照pubtime倒叙排列

curl -XGET 'http://10.128.55.7:6200/news_ref/news_ref/_search' -d '{ "sort" : [{ "pubtime" : {"order" :"desc"}} ],"from":0,"size":10 }'

term搜索以后排序

  1. curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '{"query" : {"term" : { "site_group" : "106" } }, "sort" : [{ "id" : {"order" :"asc"}} ],"from":0,"size":1 }'

根据site_group查询

  1. curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '
  2. {
  3. "query":{
  4. "term":{
  5. "site_group":"106"
  6. }
  7. },
  8. "sort":[
  9. {
  10. "id":{
  11. "order":"asc"
  12. }
  13. }
  14. ],
  15. "from":0,
  16. "size":1
  17. }
  18. '

根据复合条件查询

  1. curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '
  2. {
  3. "query":{
  4. "bool":{
  5. "must":[
  6. {
  7. "term":{
  8. "data_type":"1"
  9. }
  10. },
  11. {
  12. "term":{
  13. "site_group":"106"
  14. }
  15. }
  16. ]
  17. }
  18. },
  19. "sort":[
  20. {
  21. "id":{
  22. "order":"asc"
  23. }
  24. }
  25. ],
  26. "from":0,
  27. "size":1
  28. }
  29. '

查询删除 delete_by_query

这种删除最快,但是一定要慎重,先查询确认,再删除

  1. curl -X POST "localhost:9200/twitter/_delete_by_query?scroll_size=5000" -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "term": {
  5. "user": "kimchy"
  6. }
  7. }
  8. }
  9. '

range query 范围查询

  1. curl -XGET 'x.x.55.188:9200/second_short_video_info_ref/second_short_video_info_ref/_search' -d'
  2. {
  3. "query": {
  4. "range": {
  5. "pubtime": {
  6. "gte": "2022-03-23 00:00:00",
  7. "lte": "2022-03-23 23:59:00"
  8. }
  9. }
  10. },
  11. "size":0
  12. }'

来源

es常用curl整理_谬也的博客-CSDN博客_es查询curl

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

闽ICP备14008679号