1,查看集群状态:

windows: http://127.0.0.1:9200/_cat/health?v
linux: curl -XGET 'localhost:9200/_cat/health?v'

2.创建索引,并解决创建索引后集群健康问题:

windows:http://localhost:9200/index_name
linux: curl -XPUT 'localhost:9200/index_name?pretty'
rest方式:PUT http://localhost:9200/index_name
{
"settings":{
"number_of_shards":1, //
"number_of_replicas":1
}
}

index_name:索引名称
number_of_shards:分片
number_of_replicas:副本
上面三种方式创建的索引集群健康会为黄,会出现Unassigned;
下面这种方式会避免集群健康问题:

rest方式:PUT http://localhost:9200/index_name
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
}
}

rest方式:将副本数设置为0;
注意:在同一个节点上既保存原始数据又保存副本是没有意义的,因为一旦失去了那个节点,我们也将丢失该节点上的所有副本数据。因此副本数设置为0;
3,查看索引:

windows: http://localhost:9200/_cat/indices?v
linux: curl -XGET 'localhost:9200/_cat/indices?v'

4,查看索引settings设置

windows: http://localhost:9200/index_name/_settings
linux: curl -XGET "localhost:9200/index_name/_settings?pretty"

5,查看mapping结构

windows: http://localhost:9200/index_name/_mapping
Linux: curl -XGET "localhost:9200/index_name/_mapping?pretty"

6,数据的crud
(1)增加数据

http://localhost:9200/index_name/index_type/1
{
"name":"wes"
}

(2)删除数据:

POST http://localhost:9200/index_name/_delete_by_query
{
"query": {
"match": {
"message": "some message"
}
}
}

(3)修改数据:

curl -XPUT localhost:9200/index_name/type1/1 -d '{
"counter" : 1,
"tags" : ["red"]
}'

(4)查询数据:

curl -XGET localhost:9200/index_name/type1/1   
根据id查询:1:id

(5)根据条件查询(查询name为123123的用户,如果es没有模板需要在字段后加上.keyword才能精确查询):

http://localhost:9200/index_name/_search

{
{
"query": {
"bool": {
"must": [{
"term": {
"name.keyword": "123123"
}
}],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

}

linux: curl -XPUT 'localhost:9200/index_name/index_type/1?pretty' -d'{"name":"wes"}'

7,删除索引

windows: http://localhost:9200/index_name
linux: curl -XDELETE 'localhost:9200/index_name?pretty'

8,查看模板

windows: http://localhost:9200/_template
linux: curl -XGET 'localhost:9200/_template'

9,删除模板

windows: http://localhost:9200/_template/no_analyzed
linux: curl -XDELETE localhost:9200/_template/no_analyzed

10,添加模板

windows: http://localhost:9200/_template/t1
{
"template" : "*", "settings": {"number_of_replicas": "0"}
}
linux: curl -XPUT "http://localhost:9200/_template/t1" -d'{"template" : "*", "settings": {"number_of_replicas": "0"}}'

11,添加模板-设置不分词

windows:http://localhost:9200/_template/no_analyzed
{
"template": "*",

"mappings": {
"_default_": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}

}

linux: curl -XPUT "http://localhost:9200/_template/no_analyzed" -d '
{
"template": "*",

"mappings": {
"_default_": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}

}
]
}
}

}'

12、添加模板-设置别名,例如heart_beat_station_real

windows: http://localhost:9200/_template/aliases
{
"template": "heart_beat_station_real*",

"aliases" : {
"a_heart_beat_station_real" : [
{ "add" : { "index" : "logs*", "alias" : "a_heart_beat_station_real" } }
]
}

}

linux: curl -XPUT "http://localhost:9200/_template/aliases" -d '
{
"template": "heart_beat_station_real*",

"aliases" : {
"a_heart_beat_station_real" : [
{ "add" : { "index" : "logs*", "alias" : "a_heart_beat_station_real" } }
]
}

}'

13、添加模板-设置数据写入时间及索引warm模式

windows: http://localhost:9200/_template/t1
{
"template" : "video*", "settings": {"index.refresh_interval": "30s","index.routing.allocation.require.box_type": "warm"}
}
linux: curl -XPUT "http://localhost:9200/_template/t1" -d'{"template" : "video*", "settings": {"index.refresh_interval": "30s","index.routing.allocation.require.box_type": "warm"}}'

14、设置索引一次查询显示数量最大值(可以将_all改为index名)

windows: http://localhost:9200/_all/_settings?preserve_existing=true
{
"index.max_result_window" : "1000000"
}
linux: curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
"index.max_result_window" : "1000000"
}'

15、reindex索引,reindex前先设置新索引的mapping

windows: http://localhost:9200/_reindex
{
"source": {
"index": "my_index_name"
},
"dest": {
"index": "my_index_name_new"
}
}
linux: curl -XPOST 'localhost:9200/_reindex' -d '
{
"source": {
"index": "my_index_name"
},
"dest": {
"index": "my_index_name_new"
}
}'

16、创建仓库

linux: curl -XPUT "http://localhost:9200/_snapshot/bak_name"  -d '
{
"type": "fs",
"settings": {
"location": "/mnt/esbak",
"compress": true
}
}'

bak_name: 仓库名

17、查看仓库状态

linux: curl -XGET 'localhost:9200/_snapshot/bak_name?pretty'

18、删除仓库

linux: curl -XDELETE "localhost:9200/_snapshot/bak_name

19、备份索引数据,快照名最好跟索引名保持一致

linux: curl -XPUT "localhost:9200/_snapshot/bak_name/video" -d '
{
"indices": "video_violation_201805,video_police_case_201805,video_treat_case_201805"
}'

20、查看快照名及包含的索引

linux: curl -XGET 'localhost:9200/_snapshot/bak_name/_all'

21、删除快照

linux: curl -XDELETE  'localhost:9200/_snapshot/bak_name/zhongshi-06-20'

zhongshi-06-20: 快照名
22、增加每分钟可执行的脚本次数

linux: curl -XPUT localhost:9200/_cluster/settings -d '
{
"transient" : {
"script.max_compilations_per_minute" : 150
}
}'

或者在配置文件中修改:script.max_compilations_per_minute: 150

23、x-pack修改用户密码

linux: curl -XPUT -u elastic '192.168.75.14:9200/_xpack/security/user/elastic/_password' -d '{
"password" : "123456"
}'

24、es-head插件http-basic认证登录

http://localhost:9200/?auth_user=username&auth_password=password

username: 用户名

password: 用户密码

25、创建索引并设置副本为0,mapping如下

linux: curl  -XPUT "http://localhost:9200/test4/" -d '

{
"settings": {
"number_of_replicas": "0"
},
"mappings" : {
"test" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}


}
}
}
}'

26、查看ES节点node信息(heap内存,sm等),直接在浏览器运行,curl运行显示信息为默认参数信息

`http://10.65.3.5:9200/_cat/nodes?`v&h=ip,port,v,disk.used,hc,hp,hm,rc,rp,rm,load_1m,qcm,rcm,gc,sc,sm

详细参数参考:​​https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-nodes.html​

27、动态设置副本为0

linux: curl -XPUT 'http://10.65.3.5:9200/device/_settings ' -d '
{
"index" :{
"number_of_replicas":0
}
}'

28、mapping,动态匹配字段后设置类型

linux: curl  -XPUT "http://192.168.75.14:9200/_template/point-shape" -d '
{
"template" : "heart_beat_recorder*",
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"integers" : {
"match" : "*point",
"mapping" : {
"type" : "geo_point"
}
}
},
{


"*" : {
"match" : "*shape",
"mapping" : {
"type" : "geo_shape"
}
}
}
]
}
}
}'

创建模板,索引heart_beat_recorder*动态匹配以point或shape结尾的字段,并设置其类型

29、添加别名

linux: curl  -XPOST 'http://192.168.75.14:9200/_aliases' -d '
{
"actions" : [
{ "add" : { "index" : "heart_beat_recorder_real_*", "alias" : "a_heart_beat_recorder_real" } }
]
}'

30、添加别名模板

linux: curl  -XPUT "http://192.168.75.14:9200/_template/heart_beat_recorder_real_aliase" -d '
{
"template": "heart_beat_recorder_real_*",

"aliases" : {
"a_heart_beat_recorder_real" : [
{ "add" : { "index" : "heart_beat_recorder_real_*", "alias" : "a_heart_beat_recorder_real" } }
]
}

}'

31、添加更改时间类型模板(long_or_text处信息相当于备注)

linux: curl  -XPUT "http://10.65.3.5:9200/_template/date_date" -d '
{
"template" : "*",
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"date" : {
"match" : "*Date",
"mapping" : {
"type" : "date"
}
}
}
]
}
}
}'

linux: curl -XPUT "http://10.65.3.5:9200/_template/time_date" -d '
{
"template" : "*",
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"long_or_text" : {
"match" : "*Time",
"mapping" : {
"type" : "date"
}
}
}
]
}
}
}'

32、查看ES集群整体的信息

linux: curl -XGET 'http://localhost:9200/_cluster/stats?pretty

33、设置慢查询日志

linux: curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
"index.indexing.slowlog.level" : "info",
"index.indexing.slowlog.source" : "1000",
"index.indexing.slowlog.threshold.index.debug" : "2s",
"index.indexing.slowlog.threshold.index.info" : "5s",
"index.indexing.slowlog.threshold.index.trace" : "500ms",
"index.indexing.slowlog.threshold.index.warn" : "10s",
"index.search.slowlog.level" : "info",
"index.search.slowlog.threshold.fetch.debug" : "500ms",
"index.search.slowlog.threshold.fetch.info" : "800ms",
"index.search.slowlog.threshold.fetch.trace" : "200ms",
"index.search.slowlog.threshold.fetch.warn" : "1s",
"index.search.slowlog.threshold.query.debug" : "2s",
"index.search.slowlog.threshold.query.info" : "5s",
"index.search.slowlog.threshold.query.trace" : "500ms",
"index.search.slowlog.threshold.query.warn" : "10s"
}'

注意:1,windows下的方式可以在postman或者google中执行,在head中有些无法
2,windows下的创建方式其实就是根据Linux的方式改过来的。
3,备份数据迁移数据需要增加一些配置。