ElasticSearch入门篇
一、安装部署
1.1 版本选择
//www.greatytc.com/p/c5c3e834c028
1.2 单机版
-
下载解压
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.tar.gz tar -zxvf elasticsearch-6.6.1.tar.gz -C /opt/es/
-
ES不能在ROOT账户下启动,必须创建子账户
useradd es 创建用户 passwd es 设置密码 chown -R es:root /opt/es/ 给文件指定所属用户和用户组 chmod -R 770 /opt/es/ 修改文件夹的权限 su es 切换到es账户,su是switch user的缩写
-
修改配置vim elasticsearch.yml
####### 若要外界能访问到虚拟机上的ES,必须要配置network.host ####### network.host: 192.168.114.100 ####### ES绑定端口 ####### http.port: 9200
-
./elasticsearch -d 后台启动
./elasticsearch -h 了解启动脚本可用选项 ps -ef | grep elastic 查找ES进程 kill -9 进程号 杀掉ES进程 启动日志在 elasticsearch-6.6.1/logs/elasticsearch.log curl 192.168.114.100:9200 启动成功
- 安装ES-Head
(1)安装NodeJs
由于elasticsaerch-head是由node.js编写的,Linux需确保安装了node6以上版本 .
到 Node.js 官网(https://nodejs.org/en/download/) 选择要下载的编译版本Linux Binaries (x64)
1、切换到安装路径
cd /usr/local/src
wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.xz
2、解压缩文件包
xz -d node-v10.13.0-linux-x64.tar.xz
tar -xvf node-v10.13.0-linux-x64.tar
3、node 环境配置
vim /etc/profile
在最下面加入
# node
export NODE_HOME=/usr/local/src/node-v10.13.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
source /etc/profile // 使配置文件生效
4、查看 Node.js 是否安装成功
node -v
(2)安装elasticsaerch-head
github官网 https://github.com/mobz/elasticsearch-head,下载mobz开头的elasticsaerch-head
解压后进入elasticsaerch-head-master,
npm install 安装结束出现npm ERR!不用管
npm run start 启动在9100
(3) 连接ES-head控制台
在elasticsearch.yml中加入下面的配置才能链接到控制台
http.cors.enabled: true http.cors.allow-origin: "*"
然后重启尝试连接
1.3 分布式部署
在另外两台机子上,拷贝es包、config下elasticsearch.yml与jvm.options,赋予权限,解决启动报错。
修改配置如下
##################### master配置 #######################
network.host: 192.168.114.100
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: master
node.master: true
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
##################### slave1配置 #######################
network.host: 192.168.114.110
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: slave1
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
##################### slave2配置 #######################
network.host: 192.168.114.120
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: slave2
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
若是伪分布式,则指定不同的http.port,discovery.zen.ping.unicast.hosts只配置一项IP即可。依次用子账户启动三台ES。
二、基本API操作
3.1 创建索引
|创建结构化索引
PUT http://192.168.114.100:9200/person
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"country": {
"type": "keyword"
}
}
}
}
}
注:
text:会分词,然后进行索引
支持模糊、精确查询
不支持聚合
keyword:不进行分词,直接索引
支持模糊、精确查询
支持聚合
常用的参数类型定义&赋值demo
类型 | 参数定义 | 赋值 |
---|---|---|
text | "name":{"type":"text"} | "name": "zhangsan" |
keyword | "tags":{"type":"keyword"} | "tags": "abc" |
date | "date":{"type": "date"} | "date":"2015-01-01T12:10:30Z" |
long | "age":{"type":"long"} | "age" :28 |
double | "score":{"type":"double"} | "score":98.8 |
boolean | "isgirl": { "type": "boolean" } | "isgirl" :true |
ip | "ip_addr":{"type":"ip"} | "ip_addr": "192.168.1.1" |
geo_point | "location": {"type":"geo_point"} | "location":{"lat":40.12,"lon":-71.34} |
3.2 文档操作
【插入文档】
PUT http://192.168.114.100:9200/person/person/1
{
"name" : "张飞",
"age" : 20,
"birthday" : "1998-02-02",
"country" : "大汉"
}
或者 POST http://192.168.114.100:9200/person/person
{
"name" : "刘备",
"age" : 24,
"birthday" : "1994-02-02",
"country" : "大汉"
}
POST不用指定文档id,会自行生成唯一字符串
【修改文档】
可以直接PUT指定对应的文档ID替换那条数据,
也可以
POST http://192.168.114.100:9200/person/person/1/_update
{
"doc":{
"name":"关二爷"
}
}
【删除文档】
DELETE http://192.168.114.100:9200/person/person/1
删除索引,同时也会把所有文档删除
DELETE http://192.168.114.100:9200/person
3.3 查询数据
【简单查询】
GET http://192.168.114.100:9200/person/person/1
{
"_index": "person",
"_type": "person",
"_id": "1",
"_version": 3, // ---该条文档修改的次数
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"name": "关二爷",
"age": 20,
"birthday": "1998-02-01",
"country": "大汉"
}
}
【条件查询】
POST http://192.168.114.100:9200/person/person/_search
{
"query":{
"match_all":{}
}
}
{
"took": 10, //--- 查询花费时间ms
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": { //--- hits命中的数据
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "person",
"_type": "person",
"_id": "5Tk8ImkB6d7OxUUbp0wf",
"_score": 1,
"_source": {
"name": "刘备",
"age": 24,
"birthday": "1994-02-02",
"country": "大汉"
}
},
{
"_index": "person",
"_type": "person",
"_id": "1",
"_score": 1,
"_source": {
"name": "关二爷",
"age": 20,
"birthday": "1998-02-01",
"country": "大汉"
}
}
]
}
}
【排序分页】
默认排序规则是按_score降序,若查询指定排序规则后,则返回的数据中_score变为null
POST http://192.168.114.100:9200/person/person/_search
{
"query": {
"match": {
"country": "东吴" // 包含关键词
}
},
"sort": [{
"birthday": {
"order": "desc" // 按生日降序
}
}],
"from": 0, // 从第一条开始
"size": 10 // 取10条
}
聚合查询会专列一篇博客深入讲解