elasticsearch安装
1. 介绍
Elasticsearch 是一个分布式的免费开源搜索和分析引擎,适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。
Elasticsearch 是一个实时的分布式搜索分析引擎,
它能让你以一个之前从未有过的速度和规模,去探索你的数据。
它被用作全文检索、结构化搜索、分析以及这三个功能的组合
`
应用的场景:
商城的商品搜索
所有产品的评论
高亮显示搜索内容
收集展示各种日志
2. 下载安装
官方的网址
https://www.elastic.co/cn/downloads/elasticsearch
我们下载rpm的安装包
上传到虚拟机,安装
cd /opt
rz -E
3. 配置启动
基于java环境的,所以要安装java
yum install -y java-1.8.0-openjdk.x86_64
安装elasticsearch
rpm -ivh elasticsearch-6.6.0.rpm
启动的配置
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
检查是否启动成功
ps -ef|grep elastic
lsof -i:9200
配置文件的说明
rpm -ql elasticsearch 查看elasticsearch软件安装了哪些目录
rpm -qc elasticsearch 查看elasticsearch的所有配置文件
/etc/elasticsearch/elasticsearch.yml 配置文件
/etc/elasticsearch/jvm.options. jvm虚拟机配置文件
/etc/init.d/elasticsearch init启动文件
/etc/sysconfig/elasticsearch 环境变量配置文件
/usr/lib/sysctl.d/elasticsearch.conf sysctl变量文件,修改最大描述符
/usr/lib/systemd/system/elasticsearch.service systemd启动文件
/var/lib/elasticsearch 数据目录
/var/log/elasticsearch 日志目录
/var/run/elasticsearch pid目录
基础的配置
egrep -v "^#" /etc/elasticsearch/elasticsearch.yml
vim /etc/elasticsearch/elasticsearch.yml
cluster.name: dba5 集群名称
node.name: node-1 节点名称
path.data: /data/elasticsearch 数据目录
path.logs: /var/log/elasticsearch 日志目录
bootstrap.memory_lock: true 锁定内存
network.host: localhost 绑定IP地址
http.port: 9200 端口号
discovery.zen.ping.unicast.hosts: [“localhost”] 集群发现的通讯节点
discovery.zen.minimum_master_nodes: 2
egrep -v "^#" /etc/elasticsearch/elasticsearch.yml
我们单机的节点的配置只需要修改这几项就行了
[root@sjk1 elasticsearch]# egrep -v "^#" /etc/elasticsearch/elasticsearch.yml
node.name: node-1
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: localhost,192.168.80.90
http.port: 9200
内存的配置:
vim jvm.options
-Xms512m
-Xmx512m
默认是1g根据自己的内存大小进行更改
创建目录并授权
mkdir /data/elasticsearch
chown -R elasticsearch:elasticsearch /data/elasticsearch/
重启:
systemctl restart elasticsearch
systemctl status elasticsearch
这个时候可能会启动失败,查看日志可能会发现是锁定内存失败
官方解决方案
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/setup-configuration-memory.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/setting-system-settings.html#sysconfig
方法1: systemctl edit elasticsearch
方法2: vim /usr/lib/systemd/system/elasticsearch.service
### 增加如下参数
[Service]
LimitMEMLOCK=infinity
### 重新启动
systemctl daemon-reload
systemctl restart elasticsearch
4. es-head谷歌版本的安装
谷歌的扩展程序
下载地址:
https://github.com/liufengji/es-head
应用:
点击就会出现这个页面:
我们来连接单机的es试一试
4.2 颜色的介绍
集群状态颜色:
绿色:所有条件都满足,数据完整,副本满足
黄色:数据完整,副本不满足
红色:有索引里的数据出现不完整了
紫色:有分片正在同步中
5. 交互方式的介绍
curl命令:
最繁琐
最复杂
最容易出错
不需要安装任何软件,只需要有curl命令
es-head插件
查看数据方便
操作相对容易
需要node环境
kibana
查看数据以及报表格式丰富
操作很简单
需要java环境和安装配置kibana
5.1 curl的方式
-XPUT | 创建 |
---|---|
-XGET | 查询 |
-XPOST | 修改 |
-XDELETE | 删除 |
创建索引,相当于数据库里边的库
curl -XPUT 'localhost:9200/vipinfo?pretty'
vipinfo | 索引的名称 |
---|---|
?pretty | 显示的更加美观 |
[root@sjk1 ~]# curl -XPUT 'localhost:9200/vipinfo?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "vipinfo"
}
创建成功
插入数据
curl -XPUT 'localhost:9200/vipinfo/user/1?pretty' -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name": "Smith",
"age" : 25,
"about" : "I love to go rock climbing", "interests": [ "sports", "music" ]
}
'
curl -XPUT 'localhost:9200/vipinfo/user/2?pretty' -H 'Content-Type: application/json' -d' {
"first_name": "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums", "interests": [ "music" ]
}'
curl -XPUT 'localhost:9200/vipinfo/user/3?pretty' -H 'Content-Type: application/json' -d' {
"first_name": "Douglas", "last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets", "interests": [ "forestry" ]
}'
vipinfo | 索引,相当于数据库里边的库 |
---|---|
user | 类型,相当于数据库里边的表 |
3 | id,不写系统会自动的分配一个id,会更加的高效,我可一个让系统自动分配,然后自己在定义一个字段作为自己的id查询,大量的数据会更加快 |
-H ‘Content-Type: application/json’ -d’ | 固定的格式 |
first_name age about | 字段 |
我们也可以使用es-head页面进行操作
查询操作
查询索引中所有的
curl -XGET localhost:9200/vipinfo/user/_search?pretty
查询指定文档数据
curl -XGET 'localhost:9200/vipinfo/user/1?pretty'
curl -XGET 'localhost:9200/vipinfo/user/2?pretty’
按条件查询文档数据
查询索引中符合条件的数据:搜索姓氏为Smith的雇员
curl -XGET 'localhost:9200/vipinfo/user/_search?q=last_name:Smith&pretty’
使用Query-string查询
curl -XGET 'localhost:9200/vipinfo/user/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
'
过滤器查询
使用过滤器查询
搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的。
查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询
curl -XGET 'localhost:9200/vipinfo/user/_search?pretty' -H 'Content-Type: application/json' -d'{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {"age" : { "gt" : 30 }
}
}
}
}
}'
数据的更新
#PUT更新,需要填写完整的信息
curl -XPUT 'localhost:9200/vipinfo/user/1?pretty' -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name": "Smith",
"age" : 27,
"about" : "I love to go rock climbing", "interests": [ "sports", "music" ]
}
#POST更新,只需要填写需要更改的信息
curl -XPOST 'localhost:9200/vipinfo/user/2?pretty' -H 'Content-Type: application/json' -d'
{
"age" : 29
}'
删除
curl -XDELETE 'localhost:9200/vipinfo/user/1?pretty'
删除索引
curl -XDELETE 'localhost:9200/vipinfo?pretty'