前言
ElasticSearch:是用Java开发并且当前最流行的开源企业级搜索引擎,能够达到实时搜索、稳定、可靠、快速,安装使用方便。客户端支持Java、.NET(C#)、PHP、Python、Ruby等多种语言。
应用场景:百度搜索、商品搜索等众多场景
官方网站: https://www.elastic.co/
ElasticSearch其实也可以理解为一个数据库,index索引库(DataBase数据库)、Type类型(Table表)、Document文档(Row行)、Filed字段(Column列),其中7.x版本之后Type废弃了。
一、安装
ElasticSearch不能使用root用户来启动,必须使用普通用户来安装启动
- 坏境准备
//创建用户组elasticgroup
[root@xq /]# groupadd elasticgroup
//创建用户xqelastic并设置密码
[root@xq /]# useradd xqelastic
[root@xq /]# passwd xqelastic
//创建用户xqelastic并设置密码
[root@xq /]# mkdir -p /usr/local/es
//将用户xqelastic添加到elasticgroup
[root@xq /]# usermod -G elasticgroup xqelastic
//将es目录的拥有者改为xqelastic
[root@xq /]# chown -R xqelastic /usr/local/es
//使用visudo为用户xqelastic添加权限
[root@xq /]# visudo
//#在root ALL=(ALL) ALL 的下面一行加上
//#xqelastic ALL=(ALL) ALL
//#添加成功后保存,切换至xqelastic用户
[root@xq /]# su xqelastic
//#上传elasticsearch压缩包并解压
[xqelastic@xq es]$ cd /usr/local/es/elk/
[xqelastic@xq elk]$ tar -zvxf elasticsearch-7.6.1-linux-x86_64.tar.gz -C /usr/local/es/
//创建log目录和data目录
[xqelastic@xq elasticsearch-7.6.1]$ mkdir -p /usr/local/es/elasticsearch-7.6.1/log
[xqelastic@xq elasticsearch-7.6.1]$ mkdir -p /usr/local/es/elasticsearch-7.6.1/data
[xqelastic@xq elasticsearch-7.6.1]$ ls
bin config data jdk lib LICENSE.txt log logs modules NOTICE.txt plugins README.asciidoc
- 修改配置文件
//1、修改elasticsearch.yml文件
[xqelastic@xq elasticsearch-7.6.1]$ cd config/
[xqelastic@xq config]$ rm -rf elasticsearch.yml
[xqelastic@xq config]$ vim elasticsearch.yml
//#文件内容如下
cluster.name: xq-es
node.name: node1
path.data: /usr/local/es/elasticsearch-7.6.1/data
path.logs: /usr/local/es/elasticsearch-7.6.1/log
network.host: 0.0.0.0
http.port: 9200
#不配置集群的话这个不要配置,否则会报错
#discovery.send_host: ["192.168.0.103","127.0.0.1"]
#单机需要保留一个节点
cluster.initial_master_nodes: ["node1"]
bootstrap.system_call_filter: false
bootstrap.memory_lock: false
http.cors.enabled: true
http.cors.allow-origin: "*"
//根据机器调整jvm堆内存大小(吃内存尽可能大一点)
[xqelastic@xq config]$ vim jvm.options
//#文件内容
-Xms2g
-Xmx2g
直接启动可能会报如下报错:
[xqelastic@xq bin]$ ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[3]: 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
- 修改系统配置
// 错误信息max file descriptors.....
// 原因:普通用户打开文件的最大数限制
// ES需要大量创建索引文件,同时需要打开大量的系统文件,因此需要解除Linux系统中打开文件最大数目的限制
[xqelastic@xq bin]$ sudo vi /etc/security/limits.conf
//# 文件内容如下,*号不能省略
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
//#错误信息-max virtual memory areas...需要切换到root用户(否则没权限修改) “:quit!”表示强制退出文件
// 原因:最大虚拟内存太小
// 调大系统的虚拟内存(每次启动机器的时候都手动执行一下)
[xqelastic@xq bin]$ su xiaqing
[root@xq ~]# vi /etc/sysctl.conf
//#文件添加内容
vm.max_map_count=262144
[root@xq ~]# sysctl -p
//#错误信息:max number of threads.......
//#错误原因:普通用户启动线程数受限制,无法创建本地线程,用户最大可创建线程数太小
// Centos6--> 90-nproc.conf Centos7--> 20-nproc.conf
[xqelastic@xq bin]$ sudo vi /etc/security/limits.d/20-nproc.conf
//#找到“* soft nproc 1024”修改为一下内容
* soft nproc 4096
错误信息:[3]: 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
单机需要注释掉discovery.seed_hosts,同时配置cluster.initial_master_nodes并指定当前节点
错误原因:cluster.initial_master_nodes没有配置节点
- 启动
二、ElasticSearch与Lucene
1.Lucene
Lucene可以被认为是迄今为止最先进、性能最好、功能最全的搜索引擎框架,但是必须用Java作为开发语言并且集成到应用中,配置和使用复杂。
Lucene缺点:
- 只能在Java项目中运行,并且以jar包的形式集成到项目中。
- 创建索引和搜索索引代码复杂
- 不支持集群
- 索引库和应用在同一个服务器,共同占用硬盘,共用空间少从而数据不能太多。
ES基于Lucene,以上不足在ES中均已解决
2.Lucene全文检索框架
- 全文索引:指通过一个程序扫描文本中的每一个单词,针对单词建立索引并保存该单词在文本中的位置、以及出现的次数,查询的时候通过建立好的索引来查询。
- 分词:将数据内容通过指定的方式进行拆分成字、词或者短语。比如:hello world --> hello 、world 两个单词
- 倒排索引:类似于目录,通俗的讲就是数据对应主键(关键词与主键对应关系表),通过分词查询主键然后找到完整数据。
3.Elasticsearch中的核心概念
- 索引index:一个索引就是一个拥有几分相似特征的文档集合。
- 映射mapping:用来定义一个文档,是处理数据的方式和规则方面的限制,如类型、默认值、分词器等。
- 字段Filed:类似于表的字段(列)
- 字段类型Type:每个字段都有对应的类型
- 文档document:可被索引的基础信息单元
- 分片:ElasticSearch提供了将索引划分成多份的能力,这些份就叫分片。创建索引的时候就可以指定分片数量,每个分片本身是完善并独立的“索引”
- 副本:分片的一份或多份拷贝,这些拷贝叫做副本分片。
分片原因:一个索引可以存储超出单个结点硬件限制的大量数据。比如,一个具有10亿文档的索引占据1TB的磁盘空间,而任一节点都没有这样大的磁盘空间;或者单个节点处理搜索请求,响应太慢。
副本原因:在一个网络/云的环境里,失败随时都可能发生,在某个分片/节点不知怎么的就处于离线状态,或者由于任何原因消失了,这种情况下,有一个故障转移机制是非常有用并且是强烈推荐的。
注意:在索引创建之后,可以在任何时候动态地改变副本的数量,但是不能改变分片的数量。分片是在同一台服务划分成多份独立的索引,而副本是不同服务器之间的备份。
更多内容请关注个人微信公众号