Elasticsearch(一)

# ElasticSearch课堂笔记

Search and analyze your data in real time.

## 1. 全文检索技术简介

### 什么是搜索?

搜索,就是在任何场景下,找寻你想要的信息,这个时候,会输入一段你要搜索的关键字,然后就期望找到这个关键字相关的有些信息。

 

### 如何实现搜索?

OA系统,比如:通过名字搜索员工等等。

mysql :

select * from employee e where e.name like "%李雷%";

select * from employee e where e.`comment` like "%好%";

问题:

1. 性能
2. 比如搜索“优秀工”,mysql 无法支持。

 

### 全文检索

全文数据库是全文检索系统的主要构成部分。所谓全文数据库是将一个完整的信息源的全部内容转化为计算机可以识别、处理的信息单元而形成的数据集合。

 

全文数据库不仅存储了信息,而且还有对全文数据进行词、字、段落等更深层次的编辑、加工的功能。

 

所有全文数据库无一不是海量信息数据库。

 

#### 倒排索引

传统数据库存储:

| id | 描述 |
| :--: | :----------: |
| 1 | 优秀员工 |
| 2 | 销售冠军 |
| 3 | 优秀团队领导 |
| 4 | 优秀项目 |

 

倒排索引处理步骤:

1、切词:

优秀员工 —— 优秀 员工

销售冠军 —— 销售 冠军

优秀团队领导 —— 优秀 团队 领导

优秀项目 —— 优秀 项目

 

2、建立倒排索引:

关键词 id

| 关键词 | id |
| :----: | :----: |
| 优秀 | 1,3,4 |
| 员工 | 1 |
| 销售 | 2 |
| 团队 | 3 |
| 。。。 | 。。。 |

 

### Lucene

全文检索引擎

Lucene 能够为文本类型的数据建立索引,所以你只要能把你要索引的数据格式转化的文本的,Lucene 就能对你的文档进行索引和搜索。比如你要对一些 HTML 文档,PDF 文档进行索引的话你就首先需要把 HTML 文档和 PDF 文档转化成文本格式的,然后将转化后的内容交给 Lucene 进行索引,然后把创建好的索引文件保存到磁盘或者内存中,最后根据用户输入的查询条件在索引文件上进行查询。不指定要索引的文档的格式也使 Lucene 能够几乎适用于所有的搜索应用程序。

 

换句话说,使用 Lucene 可以轻松完成上述步骤。

 

### Elasticsearch

Elasticsearch 是一个高度可伸缩的开源全文搜索和分析引擎。它允许你以近实时的方式快速存储、搜索和分析大量的数据。它通常被用作基础的技术来赋予应用程序复杂的搜索特性和需求。

Elasticsearch ,是基于 lucene 开发的,隐藏复杂性,提供简单易用的 restful api 接口、java api 接口(还有其他语言的 api 接口)。

 

### Elasticsearch 特点

1. 可以作为一个大型分布式集群(数百台服务器)技术,处理 PB 级数据,服务大公司;也可以运行在单机上,服务小公司。
2. Elasticsearch 不是什么新技术,主要是将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的 ES 。
3. 对用户而言,是开箱即用的,非常简单,作为中小型的应用,直接3分钟部署一下 ES ,就可以作为生产环境的系统来使用了,数据量不大,操作不是太复杂。
4. 数据库的功能面对很多领域是不够用的(事务,还有各种联机事务型的操作);特殊的功能,比如全文检索,同义词处理,相关度排名,复杂数据分析,海量数据的近实时处理; Elasticsearch 作为传统数据库的一个补充,提供了数据库所不能提供的很多功能。

 

## 2. 安装

### 单节点安装教程

java8

下载es安装包

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.tar.gz

启动

bin/elasticsearch

注意:can not run elasticsearch as root

 

验证:

REST API

curl -XGET 'localhost:9200/_cat/health?v&pretty'

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1550960314 06:18:34 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%

看到status是 green,证明启动成功。

- Green - 一切运行正常(集群功能齐全)
- Yellow - 所有数据是可以获取的,但是一些复制品还没有被分配(集群功能齐全)
- Red - 一些数据因为一些原因获取不到(集群部分功能不可用)

 

在 elasticsearch-6.1.1路径下创建 data 和 logs 文件夹:

mkdir data
mkdir logs

 

修改配置文件 elasticsearch-6.1.1/config/elasticsearch.yml

cluster.name: my-application

node.name: node3

path.data: /usr/local/elasticsearch-6.1.1/data

path.logs: /usr/local/elasticsearch-6.1.1/logs

network.host: 192.168.109.133

discovery.zen.ping.unicast.hosts: ["node3"]

bootstrap.memory_lock: false

bootstrap.system_call_filter: false

 

说明:

- cluster.name :如果要配置集群需要两个节点上的 elasticsearch 配置的 cluster.name 相同,都启动可以自动组成集群,这里如果不改 cluster.name 则默认是 cluster.name=my-application。
- nodename 随意取但是集群内的各节点不能相同

 

 

启动es,报错:

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [hduser] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: system call filters failed to install; check the logs and fix your configuration or disable system

 

配置 linux 系统环境:

 

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

vi /etc/security/limits.conf

添加如下内容:

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

 

进入 limits.d 目录下修改配置文件。

vi /etc/security/limits.d/90-nproc.conf

把 * soft nproc 1024 改成4096

 

修改配置 sysctl.conf

vi /etc/sysctl.conf

添加:

vm.max_map_count=655360

执行:

sysctl -p

 

重新登录hduser用户,重新启动es

如果还有报错,则需重启虚拟机

查看集群状态命令:curl -XGET 'your ip:9200/_cat/health?v&pretty'

查看所有数据命令:

curl -XGET 'ip地址:9200/index名称/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} }
}'

 

### Elasticsearch head插件安装

node js

 

下载插件:<https://github.com/mobz/elasticsearch-head>

 

nodejs官网下载安装包:<https://nodejs.org/dist/>

node-v6.9.2-linux-x64.tar.xz

拷贝

 

安装nodejs:

解压

 

配置环境变量:

export NODE_HOME=/usr/local/node-v6.9.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin

 

查看node和npm版本:

node -v

npm -v

 

解压head插件到/opt/module目录下:

unzip elasticsearch-head-master.zip

 

查看当前head插件目录下有无node_modules/grunt目录:

没有:执行命令创建:npm install grunt --save --registry=https://registry.npm.taobao.org

 

安装head插件:

npm install -g cnpm --registry=https://registry.npm.taobao.org

 

安装grunt:

npm install -g grunt-cli --registry=https://registry.npm.taobao.org

 

编辑Gruntfile.js

vim Gruntfile.js

文件93行添加

hostname:'0.0.0.0'

 

检查head根目录下是否存在base文件夹

没有:将 _site下的base文件夹及其内容复制到head根目录下

mkdir base

cp base/* ../base/

 

启动grunt server:

grunt server -d

 

如果提示grunt的模块没有安装:

Local Npm module “grunt-contrib-clean” not found. Is it installed?

Local Npm module “grunt-contrib-concat” not found. Is it installed?

Local Npm module “grunt-contrib-watch” not found. Is it installed?

Local Npm module “grunt-contrib-connect” not found. Is it installed?

Local Npm module “grunt-contrib-copy” not found. Is it installed?

Local Npm module “grunt-contrib-jasmine” not found. Is it installed?

 

执行以下命令:

npm install grunt-contrib-clean -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

npm install grunt-contrib-concat -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

npm install grunt-contrib-watch -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

npm install grunt-contrib-connect -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

npm install grunt-contrib-copy -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

npm install grunt-contrib-jasmine -registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)

最后一个模块可能安装不成功,但是不影响使用。

 

浏览器访问head插件:

http://192.168.109.133:9100

 

posted @ 2019-05-01 13:51  jareny  阅读(112)  评论(0)    收藏  举报