5、ElasticSearch 核心技术
cluster
shards
replicas
revovery
gateway
discovery.zen
transport
settings
mapping
cluster
集群
一个主节点(选举产生)多个从节点,去中心化(无master,slave)。
主节点的职责是负责管理集群状态(分片的状态,副本的状态,以及节点的发现和删除)
http://192.168.81.131:9200/_cluster/health?pretty
{ "cluster_name" : "elasticsearch", "status" : "yellow", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 5, "active_shards" : 5, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 5, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 50.0 }
shards
分片
1、把一个完整的索引划分成多个分片,这样的好处是可以把一个大的索引水平拆分成多个,分不到不同的节点上。
构成分布式搜索,提高性能和吞吐量。
2、分片的数量只能在创建索引库时指定,索引库创建后不能更改。
curl -H 'Content-Type: application/json' -XPUT
http://192.168.81.131:9200/test3/ -d'{"settings":{"number_of_shards":3}}'
默认一个索引库是 5个分片,每个分片最多存储 2,147,483,519条数据
replicas
索引副本(数量可以随时修改) 默认1个::主副本和从副本不会再同一个节点中。
1、提高系统的容错性
2、提高es的查询效率,es会自动对搜索请求进行负载均衡
curl -H 'Content-Type: application/json' -XPUT
http://192.168.81.131:9200/test3/ -d'{"settings":{"number_of_replicas":3}}'
recovery
代表数据恢复 或叫数据重新分布,
es在有节点加入或退出时会根据机器的负载对索引份片进行重新分配,挂掉的节点重新启动时也会进行数据恢复
gateway
持久化的存储方式,

discovery.zen
自动发现节点机制,es是一个基于p2p的系统,它先通过广播寻找存在的节点,在通过多播协议来进行节点之间的通信,
同时也支持点对点的交互。 如果是不同网段的节点如何组成es集群 禁用自动发现机制 discovery.zenping.multicast.enabled:false 设置新节点被启动时能够发现的主节点列表 discovery.zen.ping.unicast.hosts:["192.168.20.210"]
transport
es内部节点或集群 与客户端的交互方式
默认是 使用tcp 协议进行交互,同时它支持http协议(json格式)、
thrift、servlet、memcached、zeromq等的传输协议(通过插件方式集成)
settings
例如分片数量,副本数量 查看:
curl -XGET http://192.168.81.131:9200/test3/_settings?pretty
操作不存在索引(创建)
curl -H "Content-Type: application/json" -XPUT 'http://192.168.81.131:9200/test3/' -d'{"settings":
{"number_of_shards":3,"number_of_replicas":2}}'
操作已存在索引(修改)
curl -H "Content-Type: application/json" -XPUT 'http://192.168.81.131:9200/test3/_settings' -d'
{"index":{"number_of_replicas":1}}'
mapping
映射是定义文档及其包含的字段如何存储和索引的过程。例如,使用映射来定义:
哪些字符串字段应该被视为全文字段。
哪些字段包含数字、日期或地理位置。
日期值的格式。
用于控制动态添加字段的映射的自定义规则。
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
查询索引库的mapping信息
curl -XGET http://192.168.81.131:9200/test/user/_mapping?pretty
操作不存在的索引(创建)
curl -H "Content-Type: application/json" -XPUT 'http://192.168.81.131:9200/test3/' -d
'{"mapings":{"user":{"properties":{"name":{"type":"text","analyer":"ik_max_word"}}}}}'
操作已存在的索引(修改)
curl -H "Content-Type: application/json" -XPUT 'http://192.168.81.131:9200/test3/_mapping' -d
'{"properties":{"name":{"type":"text","analyer":"ik_max_word"}}}'
PUT my_index { "mappings": { "properties": { "title": { "type": "text" }, "name": { "type": "text" }, "age": { "type": "integer" }, "created": { "type": "date", "format": "strict_date_optional_time||epoch_millis" } },
{
"user":{
"properties":{
"name":{
"type":"text",
"analyzer":"ik_max_word", 分词器
}
}
}
} } }
Create an index called my_index. Specify the fields or properties in the mapping. Specify that the title field contains text values. Specify that the name field contains text values. Specify that the age field contains integer values. Specify that the created field contains date values in two possible formats.