第一章 初识Elasticsearch

一、生活中的数据

1.数据的分类

1)结构化数据

行数据,以二维表的形式存储

2)非结构化数据

没有具体的结构,视频,文本,音乐文件

3)半结构化数据

xml表格,word文档,PPT,html文件

2.搜索的种类

1)结构化数据搜索

数据之间我们会对他建立一定的关系,生成二维表进行查询

2)非结构化数据搜索

1.顺序扫描
2.全文检索

二、ES介绍

1.什么是ES

是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如Nginx、Tomcat、系统日志等功能。

ElasticSearch是一个基于Lucene的搜索服务器
    1).ES是全文检索的框架,专门做搜索,支持分布式,集群
    2).ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式
    3).高可用的分布式集群 ,处理PB级别数据

2.ES和数据结构对比

mysql ES
库(database) 索引(index)
表(table) 类型(type)
列(字段)
真实数据行 文档(doc)

3.ES原理

1.存储数据是进行全文检索
2.全文检索后建立倒排索引

1)全文检索

将存储的数据进行操作
1.分词
2.找出关键词
3.搜索索引
4.匹配,命中,计算命中率
5.根据命中进行排序

2)倒排索引

java 是世界上最好的语言
php 是世界上最好的语言
python 是世界上最好的语言

https://www.cnblogs.com/ajianbeyourself/p/11280247.html
分词后的词条 第一句话 第二句话 第三句话
java 命中
php 命中
python 命中
命中 命中 命中
世界上 命中 命中 命中
最好的 命中 命中 命中
语言 命中 命中 命中

4.倒排索引的术语

1.词条:索引最小的存储单位,拆分一组词之后每一个字或者词
2.词典:词条存储的位置,一般放在内存

3.倒排表:记录字或者词命中的次数或者顺序
4.倒排文件:存放倒排表的位置,一般放在磁盘中

5.ES功能

1.分布式存储
2.全文检索:		select * from table;
3.结构化检索:	select * from table where id >1;
4.数据分析:		select count(*) from table;

6.ES的使用场景

1.大量数据存储
2.搜索数据
3.分析数据(ELK)
4.搜索高亮显示

7.ES的特点

1.可以部署单点或者集群
2.高性能
3.支持分布式
4.不需要会java
5.部署简单
6.功能丰富

三、部署ES

1.服务器时间同步

[root@es01 ~]# ntpdate time1.aliyun.com

2.安装java环境

1.上传java环境包
[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r--  1 root root 170023183 Aug 14  2018 jdk-8u181-linux-x64.rpm

2.安装
[root@es01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm

3.验证
[root@es01 ~]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

3.安装es

1)上传包

[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r--  1 root root 114059630 Jan 29  2019 elasticsearch-6.6.0.rpm

2)安装

[root@es01 ~]# rpm -ivh elasticsearch-6.6.0.rpm

#根据提示继续操作
[root@es01 ~]# systemctl daemon-reload
[root@es01 ~]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@es01 ~]# systemctl start elasticsearch.service

3)验证

[root@es01 ~]# netstat -lntp | grep java
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      8257/java           
tcp6       0      0 ::1:9200                :::*                    LISTEN      8257/java           
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      8257/java           
tcp6       0      0 ::1:9300                :::*                    LISTEN      8257/java

[root@es01 ~]# curl 127.0.0.1:9200
{
  "name" : "wUdyBOT",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "okeoXx9-QWefW6jCVhU1Xw",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

4.ES相关的配置文件

[root@es01 ~]# rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml			#主配置文件
/etc/elasticsearch/jvm.options					#虚拟内存设置文件
/etc/elasticsearch/log4j2.properties			#日志配置
/etc/elasticsearch/role_mapping.yml				#索引的规则文件
/etc/elasticsearch/roles.yml				    #规则文件
/etc/init.d/elasticsearch						#启动脚本
/etc/sysconfig/elasticsearch					#es的系统配置
/usr/lib/sysctl.d/elasticsearch.conf			#库文件的配置
/usr/lib/systemd/system/elasticsearch.service	 #systemd管理启动程序

5.配置ES

[root@es01 ~]# vim /etc/elasticsearch/elasticsearch.yml
#集群名称(单节点不需要配置)
#cluster.name: my-application

#节点名字
node.name: es-1
#ES的数据目录
path.data: /data/es/data
#指定ES的日志目录
path.logs: /data/es/log
#开启内存锁(在CentOS7中支持内存锁并且要修改启动脚本)
bootstrap.memory_lock: true
#ES监听地址
network.host: 10.0.0.71 127.0.0.1 172.16.1.71
#ES监听端口
http.port: 9200

#配置集群地址
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#集群投票节点数,一般为集群节点的半数以上
#discovery.zen.minimum_master_nodes:

#最终配置
[root@es01 ~]# grep '^[a-z]' /etc/elasticsearch/elasticsearch.yml
node.name: es-1
path.data: /data/es/data
path.logs: /data/es/log
bootstrap.memory_lock: true
network.host: 10.0.0.71 127.0.0.1 172.16.1.71
http.port: 9200

6.创建数据和日志目录

[root@es01 ~]# mkdir /data/es/{data,log} -p

#授权
[root@es01 ~]# chown -R elasticsearch.elasticsearch /data/

7.重启ES

1.重启
[root@es01 ~]# systemctl restart elasticsearch

2.重启失败

3.查看原因
[2020-11-30T16:36:12,351][ERROR][o.e.b.Bootstrap          ] [es-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
#报错说内存没有锁定

4.解决
#修改启动脚本
[root@es01 ~]# vim /usr/lib/systemd/system/elasticsearch.service
... ...
[Service]
... ...
#修改内存限制(去掉此行注释)
LimitMEMLOCK=infinity

#重新加载启动脚本
[root@es01 ~]# systemctl daemon-reload

8.验证ES

#浏览器访问  http://10.0.0.71:9200/
{
  "name" : "es-1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "KoSAwa9ZQwC4gji_qCJCWQ",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

四、与ES进行交互

1.使用curl命令与ES进行交互

1)特点

1.使用不方便,容易出错,命令复杂
2.不需要安装任何服务和插件,只需要curl命令

2)使用方式

1>创建索引(建库)
[root@es01 ~]# curl -XPUT '10.0.0.71:9200/student?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "student"
}
2>插入数据
[root@es01 ~]# curl -XPUT '10.0.0.71:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'
{
  "_index" : "student",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
3>查看数据
[root@es01 ~]# curl -GET '10.0.0.71:9200/student/user/1?pretty'
{
  "_index" : "student",
  "_type" : "user",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "lhd",
    "sex" : "man",
    "age" : "18",
    "about" : "good good study",
    "interests" : [
      "chinese",
      "english"
    ]
  }
}

2.使用插件的方式进行交互

插件是为了完成不同的功能,官方提供了一些插件但大部分是收费的,另外也有一些开发爱好者提供的插件,可以实现对elasticsearch集群的状态监控与管理配置等功能,我们现在要安装的是Elasticsearch的head插件,此插件提供elasticsearch的web界面功能。

安装Elasticsearch的head插件时,要安装npm,npm的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载、安装、上传以及管理已经安装的包。

在Elasticsearch 5.x版本以后不再支持直接安装head插件,而是需要通过启动一个服务方式。
Github地址:https://github.com/mobz/elasticsearch-head

1)特点

1.查看数据简单,操作简单
2.需要安装nodejs,安装复杂费时

2)安装插件方式一

#安装npm(只需要在一个节点安装即可,如果前端还有nginx做反向代理可以每个节点都装)
[root@elkstack01 ~]# yum install -y npm
#进入下载head插件代码目录
[root@elkstack01 src]# cd /usr/local/
#从GitHub上克隆代码到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
#克隆完成后,进入elasticsearch插件目录
[root@elkstack01 local]# cd elasticsearch-head/
#清除缓存
[root@elkstack01 elasticsearch-head]# npm cache clean -f
#使用npm安装n模块(不同的项目js脚本所需的node版本可能不同,所以就需要node版本管理工具)
[root@elkstack01 elasticsearch-head]# npm install -g n
#安装最新版本n模块
[root@elkstack01 elasticsearch-head]# n stable
#生成grunt
[root@elkstack01 elasticsearch-head]# npm install grunt -save
#确认生成grunt文件
[root@elkstack01 elasticsearch-head]# ll node_modules/grunt
#执行安装grunt
[root@elkstack01 elasticsearch-head]# npm install
#后台启动head插件(切记,必须在插件目录下执行启动命令)
[root@elkstack01 elasticsearch-head]# npm run start &
#验证端口是否启动成功
[root@elkstack01 elasticsearch-head]# netstat -lntup
tcp        0      0 0.0.0.0:9100                0.0.0.0:*                   LISTEN      11293/grunt
#启动成功后,修改elasticsearch配置文件
[root@elkstack01 elasticsearch-head]# vim /etc/elasticsearch/elasticsearch.yml
#添加如下两行,开启跨域访问支持(添加在配置文件最后即可)
http.cors.enabled: true
http.cors.allow-origin: "*"
#重启elasticsearch
[root@elkstack01 elasticsearch-head]# /etc/init.d/elasticsearch restart

3)插件安装方式二

1.windows下创建一个目录
2.将插件解压到目录下
3.谷歌浏览器,右上角三个点或者是哪个竖线或者箭头
4.更多工具 -> 扩展程序
5.打开开发者模式
6.加载已解压的扩展程序,选中解压包的目录
7.使用时,直接点击搜索框后面的放大镜

3.使用kibana进行交互

1)安装kibana

1.上传包
[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r--  1 root root 185123116 Jan 29  2019 kibana-6.6.0-x86_64.rpm

2.安装
[root@es01 ~]# rpm -ivh kibana-6.6.0-x86_64.rpm

2)配置

[root@es01 ~]# grep '^[a-z]' /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.71"
elasticsearch.hosts: ["http://172.16.1.71:9200"]

3)启动

#启动
[root@es01 ~]# systemctl start kibana

#验证
[root@es01 ~]# netstat -lntp       
tcp        0      0 10.0.0.71:5601

4)访问测试

http://10.0.0.71:5601
原文地址:https://www.cnblogs.com/jhno1/p/14095592.html