赞
踩
目录
curl -XGET http://localhost:9200/_cat/health?pretty
参数说明:
pretty:友情美化展示json
curl -XGET http://localhost:9200/_cluster/health
curl -XGET http://localhost:9200/_cat/indices?v
参数说明:
v:让返回结果显示表头。
curl -XGET http://localhost:9200/_cat/indices?v
curl -XPUT http://ip:端口号/索引名称
curl -XPUT 'http://192.168.1.110:9200/cnl' -H 'Content-Type: application/json' -d '{"settings": {"number_of_shards": 5,"number_of_replicas" : 1}}'
创建索引指定mapping和settings配置:
curl -XPUT 'http://localhost:9200/索引名称' -H 'Content-Type: application/json' -d '{"settings": {"number_of_shards": 5,"number_of_replicas" : 1},"mappings":{"properties": {"time":{"type": "date","format": "yyyy-MM-dd HH:mm:ss"}}}}'
curl -XPUT -d '{"blocks.read":false}' http://ip:端口号/索引名称/_settings
参数说明:
_settings:表示的是此索引的settings属性
blocks.read:true表示开启读限制,false不开启读限制.
查看单个:
curl -XGET http://ip:端口号/索引名称1
查看多个:
curl -XGET http://ip:端口号/索引名称,索引名称2
查看全部索引状态:
curl -XGET http://10.10.2.22:9200/_cat/indices?v
curl -XDELETE http://ip:端口号/索引名称
关闭:
curl -XPOST http://ip:端口号/索引名称/_close
打开:
curl -XPOST http://ip:端口号/索引名称/_open
curl -XPUT 'http://10.10.32.110:9200/索引名/_settings' -H 'Content-Type: application/json' -d '{"index":{"number_of_replicas":0}}'
由于mapping要进行修改(修改字段的类型等),但是mapping是不能修改的,索引要新建索引之后进行数据搬迁。
curl -XPOST -d '{"source":{"index":"old_index"},"dest":{"index":"new_index"}}' http://localhost:9200/_reindex
参数说明:
source:指定源索引
dest:指定新索引
put方式:必须指定document的ID,重复保存是进行数据的修改。
curl -XPUT -H "Content-Type: application/json" -d '{"id":"123","ip":"0.0.0.0","create_time":"2022-01-23 14:29:56","organization_code":"1-c-4cd9-809a-123"}' http://localhost:9200/test_data/_doc/123
post方式:不用指定ID,重复保存是形成了多条文档数据。
curl -XPUST -d '{"id":1,"title":"es简介"}' http://localhost:9200/索引名/_doc
curl -XPOST -H "Content-Type:application/json" http://localhost:9200/_bulk?pretty --data-binary @/home/data/bulk.txt
参数说明:--data-binary 指定文件。
bulk.txt文件内容格式:
示例:
{"create":{"_index":"student","_id":"1213"}}
{"id":"1213","age":14,"name":"李四","sex":"男"}
{"create":{"_index":"student","_id":"1212"}}
{"id":"1212","age":12,"name":"张三","sex":"男"}
curl -XGET Http://myhost:9200/index/doc/01
curl -XGET -d "{"ids":["111","222"]}" http://ip:端口号/index/_doc/_mget
curl -XGET http://localhost:9200/index/_search
curl -XGET http://localhost:9200/index/_search?q=age,"20"
参数说明:
q:代表query的意思。查询该索引下满足age是20的值的所有文档。
curl -XPOET -d "{查询条件编写}" http://localhost:9200/index/_search
curl -XDELETE http://ip:端口号/索引名称/文档对象类型/文档对象id
curl -XPOST http://ip:端口号/索引名称/_delete_by_query?conflicts=proceed&scroll_size=10000&wait_for_completion=false -d '{"query":{"match_all":{"要删除的字段" : "对应的值"}}}'
参数说明:
conflicts=proceed : 有冲突不中断继续执行
scroll_size=2000 : 删除文档数
wait_for_completion=false : 开启异步
slices=2 : 线程数量
curl -POST http://ip:端口号/索引名称/文档对象类型/_delete_by_query {"query": {"match_all": {}}}
curl -XPOST -H "Content-Type:application/json" http://localhost:9200/_bulk?pretty --data-binary @/home/data/bulk.txt
参数说明:--data-binary 指定文件。
bulk.txt文件内容格式:
{"delete":{"_index":"test_data","_id":"f0c201bf-b47e-4680-8aed-e3aeb489a255-1111"}}
curl -XPOST -d "{"name":"haah","id":"111"}" http://localhost:9200/索引名/_doc/索引id
es的乐观锁机制:
默认的锁机制为乐观锁(默认不会有同时修改的操作)。可以用es的默认字段来控制锁:_seq_no字段(该文档修改的次数)和_primary_term字段(修改的轮数)控制锁的机制。(两个字段结合的理解就是第几轮的第几次修改。)
假设es的原始文档数据:{"id":"1","name":"hah","age":1},该文档当前的_primary_term字段为1,_primary_term字段为2
请求1:修改age为10
curl -XPOST -d "{"id":"1","name":"hah","age":10}" http://localhost:9200/索引名/_doc/索引id
请求2:修改name为xixi
curl -XPOST -d "{"id":"1","name":"xixi","age":1}" http://localhost:9200/索引名/_doc/索引id
如果不进行锁的控制:那么第二次请求会覆盖第一次请求的值。(age会修改不成功。)
进行锁控制的操作:
请求1:
curl -XPOST -d "{"id":"1","name":"hah","age":10}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=2
请求2:
curl -XPOST -d "{"id":"1","name":"xixi","age":1}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=2
进行了锁控制之后,第一次请求会修改成功,修改完之后_primary_term和_primary_term字段会变成1和3,那么请求2就不会修改成功,只能进行重新查询数据,查看那俩字段然后重新进行修改发送请求。
curl -XPOST -d "{"id":"1","name":"xixi","age":10}" http://localhost:9200/索引名/_doc/索引id?if_primary_term=1&if_seq_no=3
这样才能修改成功。而且也读取到了请求一修改的age为10,这样不会造成数据错乱。
json数据:
{"name":"周杰伦","song":[{"name":"稻香","data":"2004"},{"name":"双截棍","data":"2013"},{"name":"娃娃脸","data":"2014"}]}
存入es数据库,es默认处理song属性字段存储是进行扁平化处理储存的,song.name:["稻香","双截棍","娃娃脸"] song.data:["2004","2013","2014"],这样我们想精确查找一条数据,song.name=稻香&song.data=2013,如果不进行设置song属性为内嵌类型,那么这条数据是匹配上的数据,如果设置song属性为内嵌类型的,那么这条数据是匹配不上的。把song设置成内嵌属性的话,es存储会把song属性下的内容进行单条存储,不会进行扁平化存储,这样song属性下的每个对象是一个完整数据。
设置song属性为内嵌类型的:
curl -XPUT -d "{"mappings":{"properties":{"song":{"type":"nested"}}}}" http://localhost:9200/index
curl -XHEAD http://localhost:9200/index/_doc/id
curl -XPOST -d "{"query":{"exists":{"field":"name"}}}" http://localhost:9200/index/_search
参数说明:exists (用来查询字段是否存在。)
查询name为hah或者xixi的文档:
curl -XPOST -d "{"query":{"terms":{"name":["hah","xixi"]}}}" http://localhost:9200/index/_search
指定在name和nickname字段中查询为hah的值的所有文档:
curl -XPOST -d "{"query":{"multi_match":{"query":"hah","fields":["name","nicename"]}}}" http://ip:9200/index/_search
查询name为hah的多有文档,返回只要age字段。其他字段不要。
方式一:curl -XPOST -d "{"query":{"match":{"name":"hah"}},"_source":["age"]}" http://localhost:9200/index/_search
方式二:curl -XPOST -d "{"query":{"match":{"name":"hah"}},"_source":{"includes":["name"],"excludes":[""]}}" http://localhost:9200/index/_search
参数说明:includes指定想要返回值包含的字段,excludes指定不需要返回值包含的字段。
查询年龄在10到20(包含10和20)之间的全部文档:
curl -XPOST -d "{"query":{"range":{"age":{"gte":10,"lte":20}}}}" http://localhost:9200/index/_search
bool查询中可以包含多个查询条件,must表示应该满足的条件,should表示模糊匹配条件,mush_not表示不应该满足的条件。每个条件里边都可以包含多个条件,比如must条件中,包含了name必须是hah和age必须在10到20之间。
curl -XPOST -d "{"query":{"bool":{"must":[{"match":{"name":"hah"}},{"range":{"age":{"gte":20,"lte":30}}}],"should":[{}],"must_not":[{}]}}}" http://localhost:9200/index/_search
filter查询的must查询效率基本一样,区别是filter查询出来的文档是没有评分的,must有评分。
一般filter里边写非text字段类型的过滤条件,不用参与评分,must里边写text字段类型的条件,进参与评分。should里边的条件是非强制满足的条件查询,满足的会进行加分,不满足不进行加分。
查询到name为hah的所有人之后再过滤出来age满足10到20之间的文档。
{"query":{"bool":{"must":[{"match":{"name":"hah"}}],"filter":[{"range":{"age":{"gte":10,"lte":20}}},{}]}}}
fuzzy(近似匹配)
match_phrase(短句匹配)
curl -XPOST -d "{"query":{},"sort":[{},{}]}" http://localhost:9200/index/_search
设置字段name满足张三条件的内容高亮,设置被span标签包含
curl -XPOST -d "{"query":{"match":{"name":"张三"}},"highlight":{"fields":{"name":{"pre_tags":"<span style='color:red'> ","post_tags":"</span>"}}}}" http://localhost:9200/index/_search
curl -XPOST http://10.10.10.10:9200/索引名称/_count -H 'Content-Type: application/json' -d '{"query": {"range": {"time": {"gte": "2022-01-01 00:00:00","lt": "2022-01-02 00:00:00"}}}}'
curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"range":{"time":{"gte":"2021-08-07 23:59:58","lt":"2021-08-08 00:00:00"}}},"from":0,"size":1,"aggs":{"count":{"value_count":{"field":"id.keyword"}}}}'
curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"match":{"sip":"111.207.231.5 OR 59.252.254.165 OR 106.37.221.188"}},"aggs":{"group":{"terms":{"field":"login_email.keyword","size":10,"order":{"login_count_sum":"desc"}},"aggs":{"login_count_sum":{"sum":{"field":"login_count"}}}}}}'
curl -XPOST http://localhost:9200/test_data/_search -d '{"query":{"query_string":{"default_field":"from_email.keyword","query":"huwe@111.com OR li41@111.com OR uimei@111.com OR chnning87@111.com OR shiolin8@111.com OR lianhui89@111.com OR hong74@111.com OR gxyang@111.com OR chnyun1@111.com OR chenui3@111.com"}},"size":2,"aggs":{"group":{"terms":{"field":"subject.keyword","size":10000}}}}'
curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/_all
curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称
curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/_all
curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/快照名称
curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup -d '{"type": "fs", "settings": {"location": "/home/esbackup","max_restore_bytes_per_sec":"100mb","max_snapshot_bytes_per_sec":"100mb","compress":true,"chunk_size":"50MB"}}'
curl -XPOST -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup -d '{"type": "fs", "settings": {"location": "/home/esbackup","max_restore_bytes_per_sec":"100mb","max_snapshot_bytes_per_sec":"100mb","compress":true}}'
参数说明:
put请求用来创建新仓库,post请求可以新增和修改现有仓库的配置。
type:fs 表示以快照的方式存储文件
location:如果为相对路径时(123/aaa),仓库的地址 = es配置文件中path.reop的值 + location的值,如果为绝对路径时( /123),仓库的地址 = location的值
max_restore_bytes_per_sec:节点恢复速率(当从仓库恢复数据时)。默认20mb/s。
max_snapshot_bytes_per_sec:每个节点快照速率(当快照数据进入仓库时)。默认20mb/s。
compress:是否压缩,默认为是。
chunk_size:块大小,在快照过程中,大文件会被分解成块,该属性指定块的大小。1GB、500MB、5KB、500B,默认为null(不受块大小限制。)
readonly:让库只读,默认为false
curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup/snapshot_20220104?wait_for_completion=true -d '{"indices": "test_index,test01_index","include_global_state":false,"ignore_unavailable":"true","partial":"false"}'
参数说名:
esbackup:表示仓库名称。
snapshot_20220104:表示快照名称(快照名称必须是小写)
indices:表示创建哪些索引的快照,空表示所有。
ignore_unavailable:忽略不存在的索引(不设置会快照请求会失败)
include_global_state:防止集群的全局状态被作为快照的一部分储存起来
wait_for_completion=true:参数指定是在初始化快照(默认)后立即返回请求还是等待快照完成,true表示等待快照完成之后返回请求。不设置表示进行后台创建快照,立即返回请求。
partial:默认为false,默认情况下,如果快照红的1个或多个索引不是全部分片都可用会导致整个请求过程失败,将其设置为 true 可以改变此行为
curl -XPUT -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/创建的快照名称?wait_for_completion=true
curl -XPUT -u elastic:123321 -H 'Content-Type: application/json' http://localhost:9200/_snapshot/esbackup/snapshot_20220104 -d '{"indices": "test*","include_global_state":false,"ignore_unavailable":"true","partial":"false"}'
curl -XGET -u elastic:123321 http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_status
curl -XPOST -u elastic:123321 http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_restore?wait_for_completion=true
curl -XPOST http://localhost:9200/_snapshot/esbackup/snapshot_20220104/_restore?wait_for_completion=true -d '{"indices": "test01_index,test02_index","ignore_unavailable": "true","include_global_state": false, "rename_pattern": "index_(.+)","rename_replacement": "restored_index_$1" }'
参数说明:
rename_pattern:查找所提供的模式能匹配上的正在恢复的索引。
rename_replacement:然后把它们重命名成替代的模式。
wait_for_completion=true:参数指定是在恢复快照后立即返回请求还是等待快照完成,true表示等待快照完成之后返回请求。不设置表示立即返回请求,然后后台恢复索引。
curl -XGET -u elastic:123321 http://localhost:9200/_recovery
curl -XGET -u elastic:123321 http://localhost:9200/test_index/_recovery
curl -XDELETE -u elastic:123321 http://localhost:9200/_snapshot/仓库名称/快照名称
curl -XDELETE -u elastic:123321 http://localhost:9200/_snapshot/仓库名称
curl -XGET -u 'elastic:123123' http://localhost:9200/_template
curl -XGET -u 'elastic:1231223' http://localhost:9200/_template/test*
curl -XGET -u 'elastic:1231223' http://localhost:9200/_template/test
curl -XDELETE -u 'elastic:1231223' http://localhost:9200/_template/test
curl -XDELETE -u 'elastic:1231223' http://localhost:9200/_template/test*
curl -XPUT -u 'elastic:1231223' -H 'Content-Type: application/json' http://localhost:9200/_template/default_index -d '{"index_patterns":["test_*"],"settings":{"number_of_replicas":1,"refresh_interval":"10s","index.search.slowlog.threshold.query.warn":"10s","index.search.slowlog.threshold.query.info":"5s","index.search.slowlog.threshold.query.debug":"2s","index.search.slowlog.threshold.query.trace":"500ms"},"mappings":{"properties":{"time":{"type":"date","format":"yyyy-MM-dd HH:mm:ss"}}},"order":0}'
参数说明:
refresh_interval: "10s" #刷新间隔,当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。如果对实时性要求不高,可以增加该值提高写入性能
dynamic: false #是否关闭动态字段映射,默认为true,这里选择个人选择禁用
number_of_shards: 1 #分片数量
max_result_window: 100000 #查询最大返回值1万条
number_of_replicas: 1, #索引副本数量,推荐副本数为1
order:0 #表示该模板的优先等级,数字越大优先级越高。0-10
index.search.slowlog.threshold.query.warn: 10s #超过10秒的query产生1个warn日志
index.search.slowlog.threshold.query.info: 5s #超过5秒的query产生1个info日志
index.search.slowlog.threshold.query.debug: 2s #超过2秒的query产生1个debug日志
index.search.slowlog.threshold.query.trace: 500ms #超过500毫秒的query产生1个trace日志
#fetch阶段的配置
index.search.slowlog.threshold.fetch.warn: 1s #
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug: 500ms
index.search.slowlog.threshold.fetch.trace: 200ms
#Index Slow log:索引慢日志配置
index.indexing.slowlog.threshold.index.warn: 10s #索引阶段的配置
index.indexing.slowlog.threshold.index.info: 5s
index.indexing.slowlog.threshold.index.debug: 2s
index.indexing.slowlog.threshold.index.trace: 500ms
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。