【ElasticSearch】软件安装

=====================================================

1、许可证 xpack

2、本地 ElasticSearch

3、Docker 中安装 ElasticSearch

4、安装分词插件

     分词词典配置

5、安装拼音插件

=====================================================

1、许可证 xpack

查看有效期

curl -XGET http://localhost:9200/_xpack/license?pretty

注册许可证:https://register.elastic.co/

更新许可证

curl -XPUT -u elastic 'http://localhost:9200/_xpack/license?acknowledge=true' -H "Content-Type: application/json" -d @license.json

 

2、本地 ElasticSearch

下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-10-0

解压后修改config目录下elasticsearch.yml

# 集群名
cluster.name: "ycxCluster"
# 节点名称
node.name: ycxMaster
# 成为主节点
node.master: true
# 存储数据
node.data: true
# 外网访问
network.host: 0.0.0.0
# http端口
http.port: 9200
# 是否支持跨域,默认为false
http.cors.enabled: true
# 当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.allow-origin: "*"

 

 

 

内存分配

docker 指定参数

-e ES_JAVA_OPTS="-Xms4g -Xmx4g"

原则

a、物理机器内存一半和31G。
小于31G是分配一半的物理内存,剩下的预留给 OS 和 Lucene

b、Xmx和Xms的大小是相同的。
其目的是为了能够在java垃圾回收机制清理完堆区后不需要重新分隔计算堆区的大小而浪费资源,可以减轻伸缩堆大小带来的压力

不大于32G原因
这里有另外一个原因不分配大内存给Elasticsearch,事实上jvm在内存小于32G的时候会采用一个内存对象指针压缩技术。
在java中,所有的对象都分配在堆上,然后有一个指针引用它。指向这些对象的指针大小通常是CPU的字长的大小,不是32bit就是64bit,
这取决于你的处理器,指针指向了你的值的精确位置。

对于32位系统,你的内存最大可使用4G。对于64系统可以使用更大的内存。但是64位的指针意味着更大的浪费,因为你的指针本身大了。
浪费内存不算,更糟糕的是,更大的指针在主内存和缓存器(例如LLC, L1等)之间移动数据的时候,会占用更多的带宽。

Java 使用一个叫内存指针压缩的技术来解决这个问题。它的指针不再表示对象在内存中的精确位置,而是表示偏移量。
这意味着32位的指针可以引用40亿个对象,而不是40亿个字节。最终,也就是说堆内存长到32G的物理内存,也可以用32bit的指针表示。

一旦你越过那个神奇的30-32G的边界,指针就会切回普通对象的指针,每个对象的指针都变长了,就会使用更多的CPU内存带宽,
也就是说你实际上失去了更多的内存。事实上当内存到达40-50GB的时候,有效内存才相当于使用内存对象指针压缩技术时候的32G内存。

这段描述的意思就是说:即便你有足够的内存,也尽量不要超过32G,因为它浪费了内存,降低了CPU的性能,还要让GC应对大内存。

参考博文:

https://my.oschina.net/kittyMan/blog/387512?p=1 

 

3、Docker 中安装 ElasticSearch

镜像地址:https://hub.docker.com/_/elasticsearch

run 参数说明

-d 后台运行
--name es780 容器名
-p 9200:9200 端口
-e discovery.type="single-node" 单结点
-e ES_JAVA_OPTS="-Xms4g -Xmx4g" 配置堆大小,原则物理机内存一半和31G
-v /ycx/es/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
-v /ycx/es/data/:/usr/share/elasticsearch/data

容器操作

#停止
docker stop es
#启动
docker start es
#重启
docker restart es
#进入容器
docker exec -it es624 bash
#查看版本
cat /etc/redhat-release
#查找安装目录
which elasticsearch
/usr/share/elasticsearch/bin/elasticsearch
#查找配置文件
find / -name elasticsearch.yml
/usr/share/elasticsearch/config/elasticsearch.yml
/usr/share/elasticsearch

 

进入容器
docker exec -it es bash

安装目录
/usr/share/elasticsearch

启动脚本
/usr/share/elasticsearch/bin/elasticsearch

数据目录
/usr/share/elasticsearch/data

配置目录
/usr/share/elasticsearch/config
/usr/share/elasticsearch/config/analysis-ik/config/IKAnalyzer.cfg.xml

配置文件
/usr/share/elasticsearch/config/elasticsearch.yml

插件目录
/usr/share/elasticsearch/plugins
/usr/share/elasticsearch/plugins/analysis-ik/config/IKAnalyzer.cfg.xml

安装最新版

docker pull docker.elastic.co/elasticsearch/elasticsearch:latest
docker run -d --name esLatest -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:latest

安装指定版

7.10.0版

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.0
docker run -d --name es780 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.10.0
docker run -d --restart=always -m=2g --name es -p 9200:9200 -p 9300:9300 -v /ycx/data/elasticsearch/data:/usr/share/elasticsearch/data -v /etc/localtime:/etc/localtime -e "discovery.type=single-node" -e ES_MIN_MEM=1024m -e ES_MAX_MEM=2048m -e ES_HEAP_SIZE=2048m docker.elastic.co/elasticsearch/elasticsearch:7.10.0

 

docker run -d --restart=always -m=4g --name es -p 9200:9200 -p 9300:9300 --privileged=true 
-v /ycx/data/elasticsearch/data:/usr/share/elasticsearch/data 
-v /ycx/data/elk/IKAnalyzer.cfg.xml:/usr/share/elasticsearch/config/analysis-ik/config/IKAnalyzer.cfg.xml 
-v /etc/localtime:/etc/localtime 
-e "discovery.type=single-node" -e ES_MIN_MEM=2048m -e ES_MAX_MEM=4096m -e ES_HEAP_SIZE=4096m 
docker.elastic.co/elasticsearch/elasticsearch:7.10.0

 

amazon版最新版

镜像地址:https://hub.docker.com/r/amazon/opendistro-for-elasticsearch

博文地址:https://aws.amazon.com/cn/blogs/china/running-open-distro-for-elasticsearch/

docker pull amazon/opendistro-for-elasticsearch:latest
docker run -d --name openES -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" amazon/opendistro-for-elasticsearch:latest
curl -XGET https://localhost:9200 -u admin:admin --insecure

 

4、安装分词插件

下载地址:https://github.com/medcl/elasticsearch-analysis-ik

我使用ES版本7.10.0,所以ik下载7.10.0,版本要对应

手动下载安装

cd your-es-root/plugins
mkdir ik
cd ik
复制到ik目录 docker cp /ycx/elasticsearch-analysis-ik-7.10.0.zip es:/usr/share/elasticsearch/plugins/ik 或
下载到ik目录 https:
//github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.0/elasticsearch-analysis-ik-7.10.0.zip unzip elasticsearch-analysis-ik-7.10.0.zip

使用elasticsearch-plugin install自动安装

cd your-es-root/bin
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.0/elasticsearch-analysis-ik-7.10.0.zip

测试

curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }

}'

curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'

curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'

curl -XPOST http://localhost:9200/index/_create/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'

curl -XPOST http://localhost:9200/index/_create/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

分词词典配置

官方地址:https://github.com/medcl/elasticsearch-analysis-ik

IKAnalyzer.cfg.xml 可以存放的位置

{conf}/analysis-ik/config/IKAnalyzer.cfg.xml    或    {plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml

/usr/share/elasticsearch/config/analysis-ik/config/IKAnalyzer.cfg.xml    或    /usr/share/elasticsearch/plugins/analysis-ik/config/IKAnalyzer.cfg.xml

IKAnalyzer.cfg.xml 文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 扩展配置</comment>
    <!--用户可以在这里配置自己的扩展字典 -->
    <entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
     <!--用户可以在这里配置自己的扩展停止词字典-->
    <entry key="ext_stopwords">custom/ext_stopword.dic</entry>
     <!--用户可以在这里配置远程扩展字典 -->
    <entry key="remote_ext_dict">location</entry>
     <!--用户可以在这里配置远程扩展停止词字典-->
    <entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

热更新 IK 分词使用方法
目前该插件支持热更新 IK 分词,通过上文在 IK 配置文件中提到的如下配置

<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">location</entry>

其中 location 是指一个 url,比如 http://yoursite.com/getCustomDict,该请求只需满足以下两点即可完成分词热更新。

该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。

该 http 请求返回的内容格式是一行一个分词,换行符用 \n 即可。

满足上面两点要求就可以实现热更新分词了,不需要重启 ES 实例

可以将需自动更新的热词放在一个 UTF-8 编码的 .txt 文件里,放在 nginx 或其他简易 http server 下,当 .txt 文件修改时,http server 会在客户端请求该文件时自动返回相应的 Last-Modified 和 ETag。可以另外做一个工具来从业务系统提取相关词汇,并更新这个 .txt 文件。

示例使用 nginx 做 http server 提供 custom.dic 和 stop.dic 文件

<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://172.17.0.1/custom.dic</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">http://172.17.0.1/stop.dic</entry>

在 /conf/nginx.conf 修改默认 server 节点

window nginx

location / {
    root   html;
    index  index.html index.htm;
}

centos docker nginx

location / {
    root   /usr/share/nginx/html/;
    index  index.html index.htm;
}

 

5、安装拼音插件

下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin

我使用ES版本7.10.0,所以pinyin下载7.10.0,版本要对应

手动下载安装

cd your-es-root/plugins
mkdir pinyin
cd pinyin
复制到pinyin目录 docker cp /ycx/elasticsearch-analysis-pinyin-7.10.0.zip es:/usr/share/elasticsearch/plugins/pinyin
或 下载到pinyin目录 https:
//github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.10.0/elasticsearch-analysis-pinyin-7.10.0.zip unzip elasticsearch-analysis-pinyin-7.10.0.zip

使用elasticsearch-plugin install自动安装

cd your-es-root/bin
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.10.0/elasticsearch-analysis-pinyin-7.10.0.zip

测试

PUT /medcl/ 
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "pinyin_analyzer" : {
                    "tokenizer" : "my_pinyin"
                    }
            },
            "tokenizer" : {
                "my_pinyin" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : false,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true
                }
            }
        }
    }
}

GET /medcl/_analyze
{
  "text": ["刘德华"],
  "analyzer": "pinyin_analyzer"
}

 

相关文章:

  • 2021-11-02
  • 2021-05-28
  • 2021-06-05
  • 2021-09-28
  • 2021-12-26
  • 2022-01-03
  • 2021-09-14
猜你喜欢
  • 2021-12-25
  • 2021-10-07
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-12-19
相关资源
相似解决方案