es相关优化
2017-08-19 本文已影响0人
老夫刘某
首先得认识到:
Max number of nodes = Number of shards * (number of replicas +1),也就是说节点的最大数量=分片数*(分片副本+1),其中number of replicas是指为每个副本设置几个分片副本(也就是备份)。过多的分配分片副本,会消耗磁盘空间,对于服务器的新能有很大的影响,如果我为每个shard都分配一个replica,那么最多消耗20个节点,索引创建格式一般:
curl -XPUT localhost:9200/{index}/{type}/{id},比如:
[root@centos2 elasticsearch-5.1.2]# curl -XPUT 192.168.220.130:9200/7777/blog/123 -d'{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
插件上对应的位置是:

。
对于一般情况下,如果对es集群不进行一些设置,节点的服务关闭,会影响到分片数据不完整,导致搜索到的数据不完整,如下开启2个es节点,创建索引和分片:
curl -XPUT localhost:9200/documents -d '{
settings: {
number_of_replicas: 0,
number_of_shards: 2
}
}'
这里创建了2个分片,0个分片副本,根据上面的公式,最多有2个node节点,然后往节点里放入数据:
curl -XPUT localhost:9200/documents/doc/1 -d '{ "title" : "Document No. 1" }'
curl -XPUT localhost:9200/documents/doc/2 -d '{ "title" : "Document No. 2" }'
curl -XPUT localhost:9200/documents/doc/3 -d '{ "title" : "Document No. 3" }'
curl -XPUT localhost:9200/documents/doc/3 -d '{ "title" : "Document No. 4" }'
这里我创建个测试的索引111,命令行创建111索引命令如下:
[root@centos2 elasticsearch-5.1.2]# curl -XPUT 192.168.220.130:9200/1111 -d '{"settings" : {"index" : {"number_of_shards" : 3,"number_of_replicas" : 2}}}'
执行结果如下:
{"acknowledged":true,"shards_acknowledged":true}

创建了3个索引,并且每一个索引都有2个分片副本(备份),获取索引信息:
[root@centos2 elasticsearch-5.1.2]# curl -XGET 192.168.220.130:9200/1111
{"1111":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1503138217545","number_of_shards":"3","number_of_replicas":"2","uuid":"bMffoqpORdOQ-_LQlwJsRA","version":{"created":"5010299"},"provided_name":"1111"}}}}[root@centos2 elasticsearch-5.1.2]#
创建索引并制定map
curl -XPUT 192.168.220.130:9200/1111 -d'{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}'
关闭索引:
[root@centos2 elasticsearch-5.1.2]# curl -XPOST 192.168.220.130:9200/1111/_close
{"acknowledged":true}[root@centos2 elasticsearch-5.1.2]#

打开索引:
curl -XPOST 192.168.220.130:9200/1111/_open
设置mapping:
这个可以给索引或者字段增加type的maping,如下我创建db索引:
curl -XPOST 192.168.220.130:9200/gb/tweet -d'{
"gb": {
"mappings": {
"tweet": {
"properties": {
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"name": {
"type": "string"
},
"tweet": {
"type": "string"
},
"user_id": {
"type": "long"
}
}
}
}
}
}'
这里我给新创建的索引的type设置成了tweet,反应在页面如下:

设置主分片以及副本分片数量,要去更改配置文件:
index.number_of_shards: 5
设置默认索引分片个数,默认为5片。
index.number_of_replicas: 1
设置默认索引副本个数,默认为1个副本。
索引删除:
[root@centos2 elasticsearch-5.1.2]# curl -XDELETE 192.168.220.130:9200/6666666/blog/123/ -d'{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
{"found":true,"_index":"6666666","_type":"blog","_id":"123","_version":4,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0}}[root@centos2 elasticsearch-5.1.2]# curl -XDELETE 192.168.220.130:9200/6666666/b^C
[root@centos2 elasticsearch-5.1.2]# curl -XDELETE 192.168.220.130:9200/6666666/blog/123/ -d'{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
{"found":false,"_index":"6666666","_type":"blog","_id":"123","_version":5,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0}}[root@centos2 elasticsearch-5.1.2]# ^C
```我们看到fonud返回值已经是false,并且"_version"的值还在增加,这样删除了该索引,只不过插件展示上会有延迟。
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
http://blog.csdn.net/napoay/article/details/73251965
https://wizardforcel.gitbooks.io/mastering-elasticsearch/content/chapter-4/42_README.html