Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
Elasticsearch是一个开源的分布式、RESTful 风格的搜索和数据分析引擎,它的底层是开源库Apache Lucene。
Lucene 可以说是当下最先进、高性能、全功能的搜索引擎库——无论是开源还是私有,但它也仅仅只是一个库。为了充分发挥其功能,你需要使用 Java 并将 Lucene 直接集成到应用程序中。 更糟糕的是,您可能需要获得信息检索学位才能了解其工作原理,因为Lucene 非常复杂。
为了解决Lucene使用时的繁复性,于是Elasticsearch便应运而生。它使用 Java 编写,内部采用 Lucene 做索引与搜索,但是它的目标是使全文检索变得更简单,简单来说,就是对Lucene 做了一层封装,它提供了一套简单一致的 RESTful API 来帮助我们实现存储和检索。
当然,Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎。 它可以被下面这样准确地形容:
- 一个分布式的实时文档存储,每个字段可以被索引与搜索;
- 一个分布式实时分析搜索引擎;
- 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据。
由于Elasticsearch的功能强大和使用简单,维基百科、卫报、Stack Overflow、GitHub等都纷纷采用它来做搜索。现在,Elasticsearch已成为全文搜索领域的主流软件之一。
elasticsearch+elasticsearch-head+kibana环境安装
elasticsearch:分布式搜索引擎
elasticsearch-head:es搜索引擎可视化工具
kibana:针对Elasticsearch的开源分析及可视化平台,用来搜索、查看交互存储在Elasticsearch索引中的数据。使用Kibana,可以通过各种图表进行高
elasticsearch下载地址:Download Elasticsearch | Elastic
声明:JDK1.8 ,最低要求!
elasticsearch安装
- 解压elasticsearch-7.6.1-linux-x86_64.tar.gz到/usr/local/目录:
tar -xvf elasticsearch-7.6.1-linux-x86_64.tar.gz
- 进入解压后的elasticsearch目录:
(2)新建data
mkdir data
(2)修改config/elasticsearch.yml:
vi config/elasticsearch.yml
取消或者修改下列项注释并修改:
cluster.name: my-application #集群名称
node.name: node-1 #节点名称
#数据和日志的存储目录
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
#设置绑定的ip,设置为0.0.0.0以后就可以让任何计算机节点访问到了
network.host: 0.0.0.0
http.port: 9200 #端口
#设置在集群中的所有节点名称,这个节点名称就是之前所修改的,当然你也可以采用默认的也行,目前 是单机,放入一个节点即可
cluster.initial_master_nodes: ["node-1"]
修改完毕后,:wq 保存退出vi
- 准备启动es
进入/bin目录执行命令:
./elasticsearch 或者是 ./elasticsearch -d
这里出现如下错误:
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12) ## There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory. # An error report file with more information is saved as: # logs/hs_err_pid22863.log [root@localhost bin]#
elasticsearch使用java的jvm默认是使用1G的内存的,这里我们修改一下内存,直接把内存改到256m cd 到es 目录修改 ./config/jvm.options:
vi ./config/jvm.options
修改该内容:
-Xms256m
-Xmx256m
:wq 保存并退出vi,再次启动es
再次启动出现如下错误:
[2019-06-21T16:20:03,039][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~ [elasticsearch-7.6.1.jar:7.1.1] at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~ [elasticsearch-7.6.1.jar:7.1.1] atorg.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.1.jar:7.1.1] at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~ [elasticsearch-cli-7.1.1.jar:7.1.1] at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli- 7.1.1.jar:7.1.1] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~ [elasticsearch-7.6.1.jar:7.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~ [elasticsearch-7.6.1.jar:7.1.1] Caused by: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:102) ~ [elasticsearch-7.6.1.jar:7.1.1] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:169) ~[elasticsearch- 7.6.1.jar:7.1.1] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:325) ~[elasticsearch- 7.6.1.jar:7.1.1] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~ [elasticsearch-7.6.1.jar:7.1.1] ... 6 more
[root@localhost elasticsearch-7.6.1]#
这是不能使用root用户操作,添加一个其他的用户再试试:
[root@localhost elasticsearch]# useradd es
[root@localhost elasticsearch]# passwd es更改用户 elasticsearch 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost elasticsearch]# chown -R es/usr/local/elasticsearch
vi 编辑 /etc/security/limits.conf,在末尾加上:
es soft nofile 65536
es hard nofile 65536
es soft nproc 4096
es hard nproc 4096
vi 编辑 vi /etc/security/limits.d/20-nproc.conf,将* 改为用户名(elasticsearch):
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
es soft nproc 4096
root soft nproc unlimited
vi 编辑 /etc/sysctl.conf,在末尾加上:
vm.max_map_count = 655360
在root用户下执行:
[root@localhost ~]# sysctl -p
vm.max_map_count = 655360
[root@localhost ~]#
登录刚才新建的elasticsearch用户,并启动elasticsearch,OK
[root@localhost elasticsearch]# su es
[elasticsearch@localhost elasticsearch]$ ./bin/elasticsearch
服务器:9200,出现以下结果
3.2.2 elasticsearch-head安装
- 安装node.js
wget https://nodejs.org/dist/v12.18.1/node-v12.18.1-linux-x64.tar.xz // 下载
tar xf node-v12.18.1-linux-x64.tar.xz // 解压
cd node-v12.18.1-linux-x64
// 进入解压目录 解压文件的 bin 目录底下包含了 node、npm 等命令,我们可以修改linux系统的环境变量 (profile)来设置直接运行命令:
export PATH=$PATH:/root/node-v12.18.1-linux-x64/bin
source /etc/profile
- elasticsearch-head
1. 在/usr/local目录解压elasticsearch-head-master.zip
unzip elasticsearch-head-master.zip
2. 修改elasticsearch-head-master目录
mv elasticsearch-head-master elasticsearch-head
3. 在elasticsearch-head目录下安装
npm install //安装后面可能会出现异常,但是不会影响操作
4. 启动elasticsearch-head
npm run start
[root@iZuf64b6tvq9ycjdreknleZ elasticsearch-head]# npm run start
> elasticsearch-head@0.0.0 start /usr/local/elasticsearch-head
> grunt server
>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
成功后会出现跨域问题,在elasticsearch.yml配置文件中配置跨域:
http.cors.enabled: true # elasticsearch中启用CORS
http.cors.allow-origin: "*" # 允许访问的IP地址段,* 为所有IP都可以访问
再次访问,IP:9100,出现如下信息则表示连接成功
3.2.3 kibana安装
1. 在/usr/local/解压kibana-7.6.1-linux-x86_64.tar.gz
tar -xvf kibana-7.6.1-linux-x86_64.tar.gz
2. 进入./kibana/bin目录启动
./kibana
Kibana should not be run as root. Use --allow-root to continue. 不能在root用户下启动
3. 创建kibana用户
useradd kibana
passwd kibana
chown -R kibana /usr/local/kibana
进入kibana用户启动kibana访问,启动成功后,再查看IP:9100出现以下则说明kibana配置成功
IK分词器安装
ik分词器安装 声明:安装完成后需要重新启动elasticsearch,elasticsearch-head,kibana 1. 在elasticsearch目录的plugins解压elasticsearch-analysis-ik-7.6.1.zip
unzip elasticsearch-analysis-ik elasticsearch-analysis-ik-7.6.1.zip
解压完成后并删除 elasticsearch-analysis-ik-7.6.1.zip,不然运行会报错
rm -rf elasticsearch-analysis-ik-7.6.1.zip
然后再启动elasticsearch
分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个词,比如:
"中国的花"会被分为"中","国","的","花",这显然是不符合要求的,所以我们需要安装中文分词器ik来解决这个问题
如果要使用中文,建议使用ik分词器!