什么是 Elasticsearch

想查数据就免不了搜索,搜索就离不开搜索引擎,百度、谷歌都是一个非常庞大复杂的搜索引擎,他们几乎索引了互联网上开放的所有网页和数据。然而对于我们自己的业务数据来说,肯定就没必要用这么复杂的技术了,如果我们想实现自己的搜索引擎,方便存储和检索,Elasticsearch 就是不二选择,它是一个全文搜索引擎,可以快速地储存、搜索和分析海量数据。

为什么要用 Elasticsearch

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。

那 Lucene 又是什么?Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库,但也仅仅只是一个库。要用上 Lucene,我们需要编写 Java 并引用 Lucene 包才可以,而且我们需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的,反正用起来没那么简单。

那么为了解决这个问题,Elasticsearch 就诞生了。Elasticsearch 也是使用 Java 编写的,它的内部使用 Lucene 做索引与搜索,但是它的目标是使全文检索变得简单,相当于 Lucene 的一层封装,它提供了一套简单一致的 RESTful API 来帮助我们实现存储和检索。

所以 Elasticsearch 仅仅就是一个简易版的 Lucene 封装吗?那就大错特错了,Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎。 它可以被下面这样准确的形容:

  • 一个分布式的实时文档存储,每个字段可以被索引与搜索
  • 一个分布式实时分析搜索引擎
  • 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据

总之,是一个相当牛逼的搜索引擎,维基百科、Stack Overflow、GitHub 都纷纷采用它来做搜索。

Docker 部署 ElasticSearch

1、从仓库中查找所有ElasticSearch的镜像

docker search elasticsearch
[root@iZwz99dhxbd6xwly17tb3bZ app]# docker search elasticsearch
INDEX       NAME                                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/elasticsearch                         Elasticsearch is a powerful open source se...   3060      [OK]      
docker.io   docker.io/kibana                                Kibana gives shape to any kind of data — s...   1239      [OK]      
docker.io   docker.io/nshou/elasticsearch-kibana            Elasticsearch-6.3.1 Kibana-6.3.1                81                   [OK]
docker.io   docker.io/itzg/elasticsearch                    Provides an easily configurable Elasticsea...   66                   [OK]
docker.io   docker.io/mobz/elasticsearch-head               elasticsearch-head front-end and standalon...   34                  
docker.io   docker.io/kubernetes/fluentd-elasticsearch      An image that ingests Docker container log...   25                  
docker.io   docker.io/lmenezes/elasticsearch-kopf           elasticsearch kopf                              17                   [OK]
docker.io   docker.io/tutum/elasticsearch                   Elasticsearch image - listens in port 9200.     16                   [OK]
docker.io   docker.io/monsantoco/elasticsearch              ElasticSearch Docker image                      11                   [OK]
docker.io   docker.io/bitnami/elasticsearch                 Bitnami Docker Image for Elasticsearch          10                   [OK]
docker.io   docker.io/mesoscloud/elasticsearch              [UNMAINTAINED] Elasticsearch                    9                    [OK]
docker.io   docker.io/justwatch/elasticsearch_exporter      Elasticsearch stats exporter for Prometheus     7                   
docker.io   docker.io/taskrabbit/elasticsearch-dump         Import and export tools for elasticsearch       7                    [OK]
docker.io   docker.io/blacktop/elasticsearch                Alpine Linux based Elasticsearch Docker Image   5                    [OK]
docker.io   docker.io/centerforopenscience/elasticsearch    Elasticsearch                                   3                    [OK]
docker.io   docker.io/frodenas/elasticsearch                A Docker Image for Elasticsearch                3                    [OK]
docker.io   docker.io/barchart/elasticsearch-aws            Elasticsearch AWS node                          2                   
docker.io   docker.io/jetstack/elasticsearch-pet            An elasticsearch image for kubernetes PetSets   1                    [OK]
docker.io   docker.io/phenompeople/elasticsearch            Elasticsearch is a powerful open source se...   1                    [OK]
docker.io   docker.io/thingswise/elasticsearch              Elasticsearch + etcd2 peer discovery            1                    [OK]
docker.io   docker.io/18fgsa/elasticsearch                  Built from https://github.com/docker-libra...   0                   
docker.io   docker.io/backplane/elasticsearch-curator       Elasticsearch Curator (https://github.com/...   0                   
docker.io   docker.io/driveclutch/infra-elasticsearch-aws   Elasticsearch Docker for use in AWS             0                    [OK]
docker.io   docker.io/forkdelta/fluentd-elasticsearch       fluent/fluentd with fluent-plugin-elastics...   0                    [OK]
docker.io   docker.io/wreulicke/elasticsearch               elasticsearch

2、选择一个版本镜像进行拉取

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.1.1
[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.1.1
Trying to pull repository docker.elastic.co/elasticsearch/elasticsearch ...
7.1.1: Pulling from docker.elastic.co/elasticsearch/elasticsearch
7dc0dca2b151: Pull complete
72d60ff53590: Pull complete
ca55c9f7cc1f: Pull complete
822d6592a660: Pull complete
22eceb1ece84: Pull complete
30e73cf19e42: Pull complete
f05e800ca884: Pull complete
3e6ee2f75301: Pull complete
Digest: sha256:8f06aecf7227dbc67ee62d8d05db680f8a29d0296ecd74c60d21f1fe665e04b0
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.1.1

3、 查看镜像

docker images
[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
docker.io/elasticsearch                         latest              5acf0e8da90b        3 days ago          486 MB
docker.io/hello-world                           latest              4ab4c602aa5e        2 weeks ago         1.84 kB
docker.elastic.co/elasticsearch/elasticsearch   7.1.1               96dd1575de0f        2 months ago

4、通过镜像,启动一个容器,并将9200和9300端口映射到本机(ElasticSearch的默认端口是9200,我们把宿主环境9200端口映射到Docker容器中的9200端口)

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker run -d --name es -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.1.1
c11ef1beb4f3e82d74b8709f9462e83636551b6307cd816563a4a5539f17ac6b

5、 配置跨域,进入容器内部,修改elasticsearch.yml

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker exec -it es /bin/bash
[root@c11ef1beb4f3 elasticsearch]# ls
LICENSE.txt  NOTICE.txt  README.textile  bin  config  data  lib  logs  modules  plugins
[root@c11ef1beb4f3 elasticsearch]# cd config/
[root@c11ef1beb4f3 config]# ls
elasticsearch.keystore  ingest-geoip  log4j2.properties  roles.yml  users_roles
elasticsearch.yml       jvm.options   role_mapping.yml   users
[root@c11ef1beb4f3 config]# vi elasticsearch.yml

新增跨域配置

cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
  
# minimum_master_nodes need to be explicitly set when bound on a public IP
# set to 1 to allow single node clusters
# Details: https://github.com/elastic/elasticsearch/pull/17288
discovery.zen.minimum_master_nodes: 1

6、重启ElasticSearch容器

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker restart es
es

7、 浏览器访问ip:9200

{
  "name" : "bfc29f5a8f8c",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "9dtJwBrNTwCHnbewPETxhw",
  "version" : {
    "number" : "7.1.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "7a013de",
    "build_date" : "2019-05-23T14:04:00.380842Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

二、Docker 部署 ElasticSearch-Head

ElasticSearch-Head是一个管理界面,可以查看ElasticSearch相关信息

1、拉取ElasticSearch-Head镜像

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker pull mobz/elasticsearch-head:5
Trying to pull repository docker.io/mobz/elasticsearch-head ...
5: Pulling from docker.io/mobz/elasticsearch-head
75a822cd7888: Pull complete
57de64c72267: Pull complete
4306be1e8943: Pull complete
871436ab7225: Pull complete
0110c26a367a: Pull complete
1f04fe713f1b: Pull complete
723bac39028e: Pull complete
7d8cb47f1c60: Pull complete
7328dcf65c42: Pull complete
b451f2ccfb9a: Pull complete
304d5c28a4cf: Pull complete
4cf804850db1: Pull complete
Digest: sha256:55a3c82dd4ba776e304b09308411edd85de0dc9719f9d97a2f33baa320223f34
Status: Downloaded newer image for docker.io/mobz/elasticsearch-head:5

2、 运行ElasticSearch-Head容器

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5
76b0903ac01bc642d4d3ad238134df7312473a44c5b9f603f97f6742a759e629

3、浏览器访问 ip:9100
!](https://img-blog.csdnimg.cn/20191220100346738.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3Mjc1NDA1,size_16,color_FFFFFF,t_70)

在这里插入图片描述

三、Docker部署ik中文分词插件

1、进入es容器内部,/plugins下新建ik文件夹

[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker exec -it es /bin/bash
[root@970f612c5cac elasticsearch]# ls
LICENSE.txt  NOTICE.txt  README.textile  bin  config  data  lib  logs  modules  plugins
[root@970f612c5cac elasticsearch]# cd plugins/
[root@970f612c5cac plugins]# mkdir ik
[root@970f612c5cac plugins]# ls
ik  ingest-geoip  ingest-user-agent

2、下载与es对应版本的ik压缩包,并解压

这一步有的人服务器不支持zip所以解压不了。我是从电脑上解压后弄成tar.gz文件上传到服务器然后cp到容器内部对应文件夹下,命令
docker cp /tmp/elasticsearch-analysis-ik-7.1.1.tar.gz 40aeef081297:/usr/share/elasticsearch/plugins/tk

[root@970f612c5cac plugins]# cd ik
[root@970f612c5cac ik]# wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.1.1/elasticsearch-analysis-ik-7.1.1.zip
[root@970f612c5cac ik]# unzip elasticsearch-analysis-ik-7.1.1.zip
[root@970f612c5cac ik]# ls
commons-codec-1.9.jar    elasticsearch-analysis-ik-6.3.2.jar  plugin-descriptor.properties
commons-logging-1.2.jar  httpclient-4.5.2.jar                 plugin-security.policy
config                   httpcore-4.4.4.jar

3、退出容器,重启es容器

[root@970f612c5cac ik]# exit
exit
[root@iZwz99dhxbd6xwly17tb3bZ ~]# docker restart es

至此Docker安装 Elasticsearch已基本完成

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐