网站首页 文章专栏 elasticsearch学习(一)----构建结构
基本概念:
_index(索引)、_type(类别)、_id(唯一标识) ===>>> 数据库 数据表 主键索引
1)分片(shard)
Elasticsearch集群允许系统存储的数据量超过单机容量,实现这一目标引入分片策略shard。在一个索引index中,数据(document)被分片处理(sharding)到多个分片上。Elasticsearch屏蔽了管理分片的复杂性,使得多个分片呈现出一个大索引的样子。
2)副本(replica)
为了提升访问压力过大是单机无法处理所有请求的问题,Elasticsearch集群引入了副本策略replica。副本策略对index中的每个分片创建冗余的副本,处理查询时可以把这些副本当做主分片来对待(primary shard),此外副本策略提供了高可用和数据安全的保障,当分片所在的机器宕机,Elasticsearch可以使用其副本进行恢复,从而避免数据丢失。
1.分片(shard)与副本(replica)的数量
ElasticSearch在创建索引数据时,最好指定相关的shards数量和replicas,否则会使用服务器中的默认配置参数shards=5,replicas=1。
index.number_of_shards: 5
index.number_of_replicas: 1
对于一个索引来说,number_of_shards只能设置一次,而number_of_replicas可以使用索引更新设置API在任何时候被增加或者减少。
那么如何确定分片和副本的数量呢?
依照经验,最理想的分片数量应该依赖于节点的数量。假设索引index配置了10个分片,1个副本,那么总共的分片数应该是20个,10 *(1+1),那么最大的Elasticsearch节点数应该就是20。
节点最大数 = 分片数 * (副本数 + 1)
3) 映射(mapping)
1.动态映射
字段和属性不需要预先事先定义。在你添加文档的时候,就会自动添加到索引,这个过程不需要事先在索引进行字段数据类型匹配之类,他会自己推断数据类型,动态映射是可以配置的。
2.静态映射
和动态映射相反,显示映射需要我们在索引映射中进行预先定义(推荐,可用于有分词拆词需求的)
实例:
#创建新索引 curl -XPUT http://192.168.1.133:9200/hbh -H 'Content-Type:application/json' -d ' { "settings" : { "number_of_shards" : 3, #分片数量 "number_of_replicas" : 1 #副本数量 } } ' #创建MAPPING curl -XPUT http://192.168.1.133:9200/hbh/test1/_mapping -H 'Content-Type:application/json' -d ' { "properties": { "id": { "type": "integer" }, "name":{ "type":"keyword"#不需要进行分词,可以被用来检索过滤、排序和聚合 }, "love":{ "type":"text",#被用来索引长文本,在创建索引前会将这些文本进行分词,转化为词的组合,建立索引;允许es来检索这些词,text类型不能用来排序和聚合 "analyzer": "ik_smart"" #粗分 }, "abstract":{ "type":"text", "analyzer": "ik_max_word"" #细分 }, "weight":{ "type":float }, "work_time":{ "type":date_range, "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "ip":{ "type": "ip" } } } '
结合脚本,新建映射文档、填充数据、删除旧文档、起别名 (适用于不需要频繁更新的内容),配合定时任务 一段时间执行一次
#!/bin/bashsource /etc/profile date=`date +%Y.%m.%d -d '10 days ago'` date_now=`date '+%Y-%m-%d %H:%M'` newindexnum=`date +%H%M` hostname=`hostname` echo $date_nowcurl http://10.0.0.111:9200/test > es.txt #记录日志 oldindex=`awk -F'"' '{print $2}' es.txt` #创建新索引 curl -XPUT http://192.168.1.1:9200/test_${newindexnum} -H 'Content-Type:application/json' -d ' { "settings" : { "number_of_shards" : 3, #分片 "number_of_replicas" : 1 #数据备份数 } }' #创建MAPPING curl -XPUT http://192.168.1.1.:9200/test_${newindexnum}/model/_mapping -H 'Content-Type:application/json' -d ' { "properties": { "gather_id": { "type": "integer" # 整形 可排序 }, "title": { "type": "text", #全文索引 字段不用于排序 、聚合 "analyzer": "ik_max_word" #中文拆词 }, "cover": { "type": "text" # 不参与搜索 }, "last_update_time": { "type": "long" # 整形 }, "addtime": { "type":"date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, } } ' #写入数据 curl "http://192.168.1.222/es/index.php?indexname=test_${newindexnum}" #利用代码批量写入数据 #替换别名 curl -XPOST 192.168.1.1:9200/_aliases -H 'Content-Type:application/json' -d ' { "actions": [ { "remove": { "alias": "test", "index": "'${oldindex}'" } }, { "add": { "alias": "test", "index": "test_'${newindexnum}'" } } ] } ' > es.txt echo '替换别名' if [ `grep -c "error" es.txt` -eq '0' ]; then curl -XDELETE 192.168.1.1:9200/${oldindex} #删除就数据 echo 'delete' else echo '替换别名出错!' fi echo -e '\n'
转载请注明出处