目录
一、ES介绍
官方:开源搜索:Elasticsearch、ELK Stack 和 Kibana 的开发者 | Elastic
Github:https://github.com/elastic/elasticsearch
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
Elasticsearch是与名为Logstash的数据收集和日志解析引擎以及名为Kibana的分析和可视化平台一起开发的。这三个产品被设计成一个集成解决方案,称为“Elastic Stack”(以前称为“ELK stack”)。
二、ES部署
注:es5以后为了安全都不允许使用root用户启动,否则启动报错can not run elasticsearch as root。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.13.0-linux-x86_64.tar.gz
tar -zxvf elasticsearch-7.13.0-linux-x86_64.tar.gz
adduser es
chown -R es elasticsearch-7.13.0
cd elasticsearch-7.13.0
su es
./bin/elasticsearch -d
这里参数-d是后台启动,部署后验证:curl localhost:9200
[es@yang elasticsearch-7.13.0]$ curl localhost:9200
{
"name" : "yang",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "mYzYN4ocTcOOuHkKH3uBZw",
"version" : {
"number" : "7.13.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "5ca8591c6fcdb1260ce95b08a8e023559635c6f3",
"build_date" : "2021-05-19T22:22:26.081971330Z",
"build_snapshot" : false,
"lucene_version" : "8.8.2",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
如果需要外网浏览器访问,就需要修改配置,vi config/elasticsearch.yml修改以下两个参数
# ---------------------------------- Network -----------------------------------
network.host: 0.0.0.0
http.port: 9200
但是启动时异常错误,日志路径:tail -f logs/elasticsearch.log
bootstrap check failure [1] of [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
bootstrap check failure [2] of [2]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
解决方法:
第一个问题修改vi /etc/sysctl.conf,增加一行vm.max_map_count = 262144,然后执行sysctl -p。
第二个问题修改vi config/elasticsearch.yml,cluster.initial_master_nodes: ["node-1"]
然后重启elasticsearch即可,通过curl XXX.XXX.XXX.XXX:9200验证
补充:9200端口和9300端口的区别
9200:用于外部通讯,基于http协议,程序与es的通信使用9200端口。
9300:jar之间就是通过tcp协议通信,遵循tcp协议,es集群中的节点之间也通过9300端口进行通信。
三、浏览器访问
elasticsearch-head是一个界面化的集群操作和管理工具,可以对集群进行傻瓜式操作。你可以通过插件把它集成到es(首选方式),也可以安装成一个独立webapp。
es-head主要有三个方面的操作:
a、显示集群的拓扑,并且能够执行索引和节点级别操作
b、搜索接口能够查询集群中原始json或表格格式的检索数据
c、能够快速访问并显示集群的状态
方式一:chrome浏览器可以通过浏览器插件访问:设置->扩展程序->搜索“elasticsearch-head”后安装,访问http://IP:9200
方式二:插件访问
从ElasticSearch5.x开始 不在支持 插件方式启动 必须作为独立服务器运行
对于Elasticsearch 2.x: sudo elasticsearch/bin/plugin install mobz/elasticsearch-head
对于Elasticsearch 1.x: sudo elasticsearch/bin/plugin -install mobz/elasticsearch-head/1.x
对于Elasticsearch 0.x: sudo elasticsearch/bin/plugin -install mobz/elasticsearch-head/0.9
方式三:作为独立服务访问
四、ES Restful API基本使用
Curl工具是一种可以在命令行访问url的工具,支持get和post请求方式。-X指定http请求的方法,-d指定要传输的数据。
curl <-Xaction> url -d 'body' # 这里的action表示HTTP协议中的各种动作,包括GET、POST、PUT、DELETE等。
1、简单查询
(1)查看集群中的节点信息
[root@yang elasticsearch-7.13.0]# curl -XGET "localhost:9200/_cat/nodes?v"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
xx.x.xxx.xxx 13 72 0 0.00 0.01 0.05 cdfhilmrstw * yang.novalocal
(2)查看集群中的索引信息
[root@yang elasticsearch-7.13.0]# curl -XGET "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open library TMgdUfmyQwKWgRBnsze7cQ 3 1 0 0 624b 624b
2、索引
(1)创建一个新的索引
[root@yang elasticsearch-7.13.0]# curl -XPUT "localhost:9200/test"
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}
返回acknowledged字段说明创建成功
详细参数说明:
curl -XPUT "localhost:9200/test" -d '
{
"settings": {
"index": {
"number_of_replicas": "1", # 设置复制数
"number_of_shards": "5" # 设置主分片数
}
},
"mappings": { # 创建mapping
"test_type": { # 在index中创建一个新的type(相当于table)
"properties": {
"name": { # 创建一个字段(string类型数据,使用普通索引)
"type": "string",
"index": "not_analyzed"
},
"age": {
"type": "integer"
}
}
}
}
}'
(2)删除一个索引
[root@yang elasticsearch-7.13.0]# curl -XDELETE "localhost:9200/test"
{"acknowledged":true}
五、ik分词器
github:https://github.com/medcl/elasticsearch-analysis-ik
注意对应不同版本的es安装ik的版本也不一样,可以看github首页
1、安装ik
两种安装方式:
(1)下载最新的版本:https://github.com/medcl/elasticsearch-analysis-ik/releases
mkdir -p plugins/ik
cd plugins/ik
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.0/elasticsearch-analysis-ik-7.13.0.zip
unzip elasticsearch-analysis-ik-7.13.0.zip //yum install -y unzip zip
或者
(2)./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.0/elasticsearch-analysis-ik-7.13.0.zip
两种方式安装完成后都需要重启es
2、验证安装
[es@yang elasticsearch-7.13.0]$ curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
> {
> "properties": {
> "content": {
> "type": "text",
> "analyzer": "ik_max_word",
> "search_analyzer": "ik_smart"
> }
> }
>
> }'^C
[es@yang elasticsearch-7.13.0]$ curl -XPUT http://localhost:9200/index
{"acknowledged":true,"shards_acknowledged":true,"index":"index"}[es@yang elasticsearch-7.13.0]$ curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
> {
> "properties": {
> "content": {
> "type": "text",
> "analyzer": "ik_max_word",
> "search_analyzer": "ik_smart"
> }
> }
>
> }'
{"acknowledged":true}
[es@yang elasticsearch-7.13.0]$ curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d'
> {"content":"美国留给伊拉克的是个烂摊子吗"}
> '
{"_index":"index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}