Elasticsearch 搜索引擎


简介:

  Elasticsearch 是一个实时的分布式搜索和分析引擎。它可以帮助你用前所未有的速度去处理大规模数据、它可以用于全文搜索,结构化搜索以及分析。
 
  分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。

  实时分析的分布式搜索引擎。

  可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。

下载地址:https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.0/elasticsearch-2.4.0.tar.gz

一、安装 Elasticsearch

shell > yum -y install java                 # 需要 java 环境

shell > java -version
openjdk version "1.8.0_101"
OpenJDK Runtime Environment (build 1.8.0_101-b13)
OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)

shell > useradd elast                       # 用于启动 elasticsearch
shell > cd /usr/local/src
shell > wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.0/elasticsearch-2.4.0.tar.gz

shell > tar zxf elasticsearch-2.4.0.tar.gz
shell > mv elasticsearch-2.4.0 ../elasticsearch-2.4.0

shell > chown -R elast.elast /usr/local/elasticsearch

二、配置 Elasticsearch

shell > vim /usr/local/elasticsearch/conf/elasticsearch.yml

cluster.name: es                              # 集群名称

node.name: node-1                             # 节点名称,默认第一个启动的节点为集群 Master

path.data: /data/esearch/data                 # 索引存放路径
path.logs: /data/esearch/logs                 # 日志

network.host: 192.168.1.32                    # 监听地址
http.port: 9200                               # 端口

shell > mkdir /data/esearch/data
shell > mkdir /data/esearch/logs

shell > chown -R elast.elast /data/esearch/data
shell > chown -R elast.elast /data/esearch/logs

shell > vim /usr/local/elasticsearch/bin/elasticsearch.in.sh

if [ "x$ES_MIN_MEM" = "x" ]; then
    ES_MIN_MEM=256m
fi
if [ "x$ES_MAX_MEM" = "x" ]; then
    ES_MAX_MEM=2g
fi

# 设置一下允许 elasticsearch 使用的最大/最小内存

三、启动 Elasticsearch

shell > su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch -d"           # 以 elast 用户身份启动,否则报错

shell > echo 'su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch -d"' >> /etc/rc.local      # 加入开机启动

shell > killall java  # 因为我的服务器上只跑一个 java 程序,就是 elasticsearch ,所以我这样关闭

shell > netstat -lnpt | grep java
tcp        0      0 :::9200                     :::*                        LISTEN      2076/java           
tcp        0      0 :::9300                     :::*                        LISTEN      2076/java

shell > tree /data/esearch/  # 数据(索引)目录、日志目录
/data/esearch/
├── data
│   └── my-esearch
│       └── nodes
│           └── 0
│               ├── node.lock
│               └── _state
│                   └── global-0.st
└── logs
    ├── my-esearch_deprecation.log
    ├── my-esearch_index_indexing_slowlog.log
    ├── my-esearch_index_search_slowlog.log
    └── my-esearch.log

shell >ps aux | grep java
elast     2107  3.7  6.6 5757256 259152 ?      Sl   22:52   0:10 /usr/bin/java -Xms256m -Xmx2g -Djava.awt.headless=true
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly
-XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/local/elasticsearch-2.4.0
-cp /usr/local/elasticsearch-2.4.0/lib/elasticsearch-2.4.0.jar:/usr/local/elasticsearch-2.4.0/lib/* org.elasticsearch.bootstrap.Elasticsearch start -d

# 可以看到启动参数中有前面内存的设定 -Xms256m -Xmx2g

四、测试 Elasticsearch

shell > curl -X GET localhost:9200                     # 返回 elasticsearch 基本信息,节点名称、版本信息等
{
  "name" : "node-1",
  "cluster_name" : "es",
  "version" : {
    "number" : "2.4.0",
    "build_hash" : "ce9f0c7394dee074091dd1bc4e9469251181fc55",
    "build_timestamp" : "2016-08-29T09:14:17Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.2"
  },
  "tagline" : "You Know, for Search"
}

shell > curl -X GET localhost:9200/_cat                # 支持的各指令
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}

shell > curl -X GET 127.0.0.1:9200/_cat/master?v       # 查看 Master 信息,加 ?v 可以显示表头
id                     host         ip           node   
7EkYmW84S8aELlXvieV5uw 192.168.1.32 192.168.1.32 node-1 

shell > curl -X GET localhost:9200/_cat/nodes?v        # 集群节点信息
host         ip           heap.percent ram.percent load node.role master name   
192.168.1.32 192.168.1.32            1          20 0.12 d         *      node-1

五、Elasticsearch 插件安装

shell > /usr/local/elasticsearch/bin/plugin list       # 查看 Elasticsearch 安装了哪些插件,结果一个也没有
Installed plugins in /usr/local/elasticsearch/plugins:
    - No plugin detected

1、head 集群管理插件

shell > /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head          # 安装一个插件,用于集群管理

# 一些插件地址:
# https://github.com/mobz/elasticsearch-head              # 可直接安装
# https://github.com/medcl/elasticsearch-analysis-ik      # 需要自己打包 mvn package

shell > /usr/local/elasticsearch/bin/plugin list          # 再次查看
Installed plugins in /usr/local/elasticsearch/plugins:
    - head

## http://192.168.1.32:9200/_plugin/head  浏览器访问 head 集群管理插件

2、中文分词插件

# 这里先留空一个知识点:maven 的安装跟怎么给中文分词器编译打包。

shell > cd /usr/local/src/
shell > wget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip  # 下载中文分词插件
shell > unzip elasticsearch-analysis-ik-master.zip
shell > cd elasticsearch-analysis-ik-master
shell > mvn package                                       # 将分词器源码编译成 jar 包,过程有点长 ( 打包失败的,可以加我 QQ:25152069 ,我有打好的包 )
shell > cp target/releases/elasticsearch-analysis-ik-1.10.0.zip /usr/local/elasticsearch-2.4.0/plugins/ik/  # ik 目录不存在就创建一下
shell > cd /usr/local/elasticsearch-2.4.0/plugins/ik
shell > unzip elasticsearch-analysis-ik-1.10.0.zip
shell > killall java
shell > su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch"

shell > /usr/local/elasticsearch-2.4.0/bin/plugin list    # 再次查看插件已经安装成功
Installed plugins in /usr/local/elasticsearch-2.4.0/plugins:
    - head
    - ik

六、配置 Elasticsearch 集群

1、node1 ( 默认的 Master )

shell > grep -vP '^#|^$' /usr/local/elasticsearch-2.4.0/config/elasticsearch.yml 
cluster.name: es                                    # 集群名称必须一致,且同一局域网内可以自动组成集群 
node.name: node-1                                   # 节点名称唯一
path.data: /data/esearch/data
path.logs: /data/esearch/logs
network.host: 192.168.1.32
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.1.33"]  # 指定谁可以成为 Master

2、node2 ( 默认的 node )

shell > grep -vP '^#|^$' /usr/local/elasticsearch-2.4.0/config/elasticsearch.yml 
cluster.name: es
node.name: node-2                                   # 节点名称唯一
path.data: /data/esearch/data
path.logs: /data/esearch/logs
network.host: 192.168.1.33
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.1.32"]  # 指定谁可以成为 Master

## 注意:

> 如果不指定谁可以成为 Master ,那么默认两个节点都是 Master ,无法构成集群。新加入的节点不设置该参数,无法加入到现有集群。

> 如果被指定的节点设置了 node.master: false ,那么该节点无法成为 Master ,如果只有两个节点,当 Master 挂掉,那么集群失败。

> 防火墙:开启 TCP 9200 :为了使用web浏览集群状态等,TCP 9300 :用于节点直接通信。

3、查看集群状态

shell > curl -X GET 192.168.1.32:9200/_cat/master?v  # 查看谁是 Master
id                     host         ip           node   
9RVF5M9lRDuvhzKNF5VjXA 192.168.1.32 192.168.1.32 node-1 

shell > curl -X GET 192.168.1.32:9200/_cat/nodes?v   # 查看各节点状态,* 代表该节点是 Master ,m 代表该节点可以成为 Master ,- 代表该节点不可以成为 Master
host         ip           heap.percent ram.percent load node.role master name   
192.168.1.33 192.168.1.33            4          17 0.36 d         m      node-2 
192.168.1.32 192.168.1.32            4          25 0.49 d         *      node-1 

# End

原文地址:https://www.cnblogs.com/wangxiaoqiangs/p/5854159.html