ElasticSeacrch的安装

来自CloudWiki
跳转至: 导航搜索

全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选。

它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。

Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开箱即用。

本文从零开始,讲解如何使用 Elastic 搭建自己的全文搜索引擎。每一步都有详细的说明,大家跟着做就能学会。

新建用户

groupadd esg

useradd esu -g esg

环境准备

Elastic 需要 Java 8 环境。如果你的机器还没安装 Java,可以参考这篇文章:Linux系统安装Java环境,注意要保证环境变量JAVA_HOME正确设置。

或参考这篇,使用yum进行安装:

安装Elastic

安装完 Java,就可以跟着官方文档安装 Elastic。直接下载压缩包比较简单。

cd /home/esu/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.1-linux-x86_64.tar.gz
tar -zxvf  elasticsearch-7.17.1-linux-x86_64.tar.gz

chown -R esu:esg /home/esu/elasticsearch-7.17.1


接着,进入解压后的目录,运行下面的命令,启动 Elastic。

su esu
cd /home/esu/elasticsearch-7.17.1

 ./bin/elasticsearch

处理报错一

如果这时报错"max virtual memory areas vm.maxmapcount [65530] is too low",要运行下面的命令。

sudo sysctl -w vm.max_map_count=262144

处理启动报错二

2017-01-12T16:12:22,404][INFO ][o.e.b.BootstrapCheck     ] [SfD5sIh] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
ERROR: bootstrap checks failed
initial heap size [536870912] not equal to maximum heap size [1073741824]; this can cause resize pauses and prevents mlockall from locking the entire heap

vi config/jvm.options

-Xms1g
-Xmx1g

官方建议内存设置低于系统内存的一半,不要超过 32G(为什么不要超过 32G 可以翻阅一下资料,这里就不展开了),假如物理机器有 128G,可以在机器上面运行两个 ES 实例。

处理启动报错三

max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]

解决:切换到root用户,编辑limits.conf 添加类似如下内容

vi /etc/security/limits.conf

添加如下内容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

如果一切正常,Elastic 就会在默认的9200端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息。

[root@controller ~]# curl localhost:9200

{
  "name" : "VEgiqYy",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "X-HMAArURPaa6fjIaz4zPA",
  "version" : {
    "number" : "5.5.1",
    "build_hash" : "19c13d0",
    "build_date" : "2017-07-18T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

上面代码中,请求9200端口,Elastic 返回一个 JSON 对象,包含当前节点、集群、版本等信息。

按下 Ctrl + C,Elastic 就会停止运行。

默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elastic。


参考文档:

[1] http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html [2] https://gitbook.cn/books/5cad51b2aea01a433bdfe2e7/index.html