ES安装
官方网站: ElasticSearch入门指南
启动:bin -> elasticsearch.bat
Head插件:head插件git网站下载
分布式安装(修改配置文件 config/elasticsearch.yml)
-master
# 跨域配置修改(是的head插件能够连接elacticsearch)
http.cors.enabled: true
http.cors.allow-origin: "*"
# 集群名字
cluster.name: yinhai
# master名字
node.name: yinhai_master
node.master: true
# 绑定IP
network.host: 127.0.0.1
# 端口
http.port: 9200
-slave
# 保证和集群master的cluster.name一致
cluster.name: yinhai
# 节点名称
node.name: yinhai_slave_1
# 绑定IP
network.host: 127.0.0.1
# 绑定端口
http.port: 8200
# 配置master地址
discovery.zen.ping.unicast.hosts: [“127.0.0.1”]
Elasticsearch基础概念
集群:一个集群是由一个或多个节点组成,所有节点都是由cluster.name 加入集群的
索引:含有相同属性的文档集合(英文字母小写,不含下划线,通过索引对文档进行CRUD操作)
类型:索引可以定义一个或多个类型,文档必须属于一个类型
文档:文档是可以被索引的基本数据单位
索引:相当于sql中的database
类型:相当于sql中的table
文档:相当于sql中的一行记录
分片:每个索引都有多个分片,每个分片是一个Lucene索引
备份:拷贝一份分片就完成了分片的备份
为什么要有分片和备份
- 假设索引数据量大,造成硬盘压力大,搜索速度出现瓶颈,将索引分为多个分片,分摊压力,分片也允许用户进行水平扩展和拆分,以及分布式的操作,可以提高搜索的效率
- 当一个主分片失败或者出现问题时,备份的分片可以代替工作,提高了es的可用性,备份的分片还可以执行搜索操作,分摊搜索的压力
ES索引默认5个分片、1个备份,分片的数量只能在创建索引的时候指定,进行分片指定后不可以修改,备份数可以修改。
RESTFul API
- API基本格式
http://<ip>:<port>/<索引>/<类型>/<文档id>
- 常用HTTP动词
GET/PUT/POST/DELETE
使用head插件创建索引:
请求URL:IP:Prot/分片名称
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"yzz530": {
"type": "keyword"
},
"yzz531": {
"type": "text"
},
"yzz535": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"jurisdictions": {
"type": "object"
}
}
}
}