受欢迎的博客标签

Elasticsearch5.2.1 搜索引擎框架 在LINUX上安装配置及analysis-ik中文分词插件安装

Published

http://blog.csdn.net/fenglailea/article/details/55506775 

 

 

目录

http://blog.csdn.net/fenglailea/article/details/52486471

老版本

http://blog.csdn.net/fenglailea/article/details/52471254

介绍

Elasticsearch一个开源的搜索引擎框架(支持群集架构方式)

JAVA版本说明

必须是 java 1.8.0_73 以上
安装 Java 版本 ,根据教程下载相应的版本
http://blog.csdn.net/fenglailea/article/details/26006647

0.Elasticsearch 简介或其他

http://kibana.logstash.es/content/elasticsearch/
https://es.xiaoleilu.com/

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html
风来了.fox

服务器说明

如果在服务器上面安装
最低 4G内存,
如果更低的内存 请自行更改 config/jvm.xx 文件中的配置

1.下载和安装

下载地址

https://www.elastic.co/downloads/elasticsearch
  • 1

目前最新版本 5.2.1
这里选择 LINUX 即方式一
方式一:源码

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.1.tar.gz
tar -zxvf elasticsearch-5.2.1.tar.gz
  • 1
  • 2

不要用root用户,这里使用hadoop用户

这里是 方式一后续步骤:

不要用root用户,不要用root用户,不要用root用户

不要用root用户,如果已使用root用户解压缩了,先改文件夹所属用户组和用户
root用户是无法启动elasticsearch

chown hadoop:hadoop -R elasticsearch-2.4.0
  • 1

2.配置elasticsearch

2.1配置

编辑config/elasticsearch.yml

vim config/elasticsearch.yml
  • 1

修改为

network.host: 0.0.0.0
cluster.name: es
  • 1
  • 2

…其他部分没有改动,不需要修改

2.2 环境变量设置

vim /etc/profile.d/elasticsearch.sh
  • 1

加入

export ES_HOME=/home/hadoop/elasticsearch-5.2.1
export PATH=$ES_HOME/bin:$PATH
  • 1
  • 2

应用生效

. /etc/profile
. /etc/bashrc
  • 1
  • 2

3.启动

命令

cd elasticsearch目录
bin/elasticsearch     #前台运行
bin/elasticsearch -d  #后台运行
  • 1
  • 2
  • 3

访问

http://10.1.5.66:9200/
  • 1

10.1.5.66 安装elasticsearch的服务器IP
如果在局域网中访问
修改

vim config/elasticsearch.yml
  • 1

增加

network.host: 0.0.0.0
  • 1

3.3停止

查找进程 ID

ps -ef |grep elasticsearch
  • 1

KILL他

kill -9  id
  • 1

3.X Elasticsearch设置

4. 插件

执行命令 自动下载安装

4.1 x-pack

bin/elasticsearch-plugin install x-pack
  • 1

此插件下载速度相当的慢,可以happy几小时回来再看。(如果你有代理那另当别论)

X-Pack是一个Elastic Stack的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中。
在Elasticsearch 5.0.0之前,您必须安装单独的Shield,Watcher和Marvel插件才能获得在X-Pack中所有的功能。
注意,安装之后,所有对ES的访问,增加了security机制,即需要用户名和密码,默认分别为: elastic 和changeme ,使用sense插件访问的时候可以输入,如果你是使用curl等方式访问,则需要在http 的header中增加 Authentication 参数。
验证X-Pack
在浏览器上输入: http://localhost:5601/

4.1 head

此插件已独立运行。新的安装方式

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
#先要安装 npm   http://blog.csdn.net/fenglailea/article/details/56484144
npm install
grunt server
  • 1
  • 2
  • 3
  • 4
  • 5

访问地址

http://localhost:9100/
  • 1

4.2 bigdesk

bin/plugin install lukas-vlcek/bigdesk
  • 1

访问地址

http://localhost:9200/_plugin/bigdesk/
  • 1

中文分词插件analysis-ik

https://github.com/medcl/elasticsearch-analysis-ik/releases
  • 1

下载,当前版本 v5.2.1,elasticsearch-analysis-ik-5.2.1.zip

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.2.1/elasticsearch-analysis-ik-5.2.1.zip
unzip elasticsearch-analysis-ik-5.2.1.zip -d elasticsearch-analysis-ik-5.2.1
  • 1
  • 2

复制到插件目录

mv elasticsearch-analysis-ik-5.2.1 elasticsearch安装目录/plugins/analysis-ik
  • 1

这个时候需要重启 elasticsearch 插件才能生效(这个可以等设置好词库再重启也可以)

分词词库设置

进入 elasticsearch安装目录
编辑词库配置文件

vim plugins/analysis-ik/config/IKAnalyzer.cfg.xml 
  • 1

ext_dict 这一行修改为

<entry key="ext_dict">custom/sougou.dic;custom/mydict.dic;custom/single_word_low_freq.dic;custom/product.dic</entry> 
  • 1

custom/product.dic 是我的词库,这里不方便透露

这个时候需要重启 elasticsearch 插件才能生效

热更新 IK 分词使用方法 来自官方

https://github.com/medcl/elasticsearch-analysis-ik
目前该插件支持热更新 IK 分词,通过上文在 IK 配置文件中提到的如下配置

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

其中 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 文件。

分词测试

curl -XPUT "http://localhost:9200/index"
  • 1

测试分词效果:

http://10.1.5.101:9200/index/_analyze?analyzer=ik_max_word&text=中华人民共和国
  • 1

结果

{
    "tokens": [
        {
            "token": "中华人民共和国",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中华人民",
            "start_offset": 0,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "中华",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "华人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "人民共和国",
            "start_offset": 2,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "人民",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "共和国",
            "start_offset": 4,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "共和",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "国",
            "start_offset": 6,
            "end_offset": 7,
            "type": "CN_CHAR",
            "position": 8
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

Mysql数据库同步 logstash-input-jdbc

6.高级配置说明

http://rockelixir.iteye.com/blog/1883373

http://kibana.logstash.es/content/elasticsearch/

x.其他说明

x.1 bin/elasticsearch命令运行后,出现java.lang.RuntimeException: don’t run elasticsearch as root异常

解决方法:不要使用root用户,使用其他用户,并且要把 elasticsearch 文件夹的所属用户和用户组更改为该用户

每次修改elasticsearch配置,重启elasticsearch才能生效

X.2 max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

[2017-02-17T00:20:16,210][INFO ][o.e.b.BootstrapChecks    ] [l4x7kR2] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 
  • 1
  • 2
  • 3
  • 4

修改

sudo vim /etc/sysctl.conf
  • 1

加入

vm.max_map_count=655360
  • 1

修改

sudo vim /etc/security/limits.conf
  • 1

加入

hadoop hard nofile 65536  
hadoop soft nofile 65536 
  • 1
  • 2

hadoop这里为用户名
然后重启启动elasticsearch即可

如果以上设置好了,还是报错,请重启服务器即可解决

x.3 Exception in thread “main” ElasticsearchParseException[malformed, expected settings to start with ‘object’, instead was [VALUE_STRING]]

Exception in thread "main" ElasticsearchParseException[malformed, expected settings to start with 'object', instead was [VALUE_STRING]]
        at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:73)
        at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:52)
        at org.elasticsearch.common.settings.loader.YamlSettingsLoader.load(YamlSettingsLoader.java:50)
        at org.elasticsearch.common.settings.Settings$Builder.loadFromStream(Settings.java:925)
        at org.elasticsearch.common.settings.Settings$Builder.loadFromPath(Settings.java:914)
        at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:102)
        at org.elasticsearch.bootstrap.Bootstrap.initialEnvironment(Bootstrap.java:257)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:297)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112)
        at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122)
        at org.elasticsearch.cli.Command.main(Command.java:88)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:89)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:82)
Refer to the log for complete error details. 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

原因
**参数的冒号前后没有加空格**

X.4 Exception in thread “main” java.lang.UnsupportedClassVersionError: org/elasticsearch/bootstrap/Elasticsearch : Unsupported major.minor version 52.0

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/elasticsearch/bootstrap/Elasticsearch : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
        ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

java版本低了,换高级版本

部分来源:
http://kibana.logstash.es/content/elasticsearch/
http://www.cnblogs.com/jiu0821/p/5624908.html

http://blog.csdn.net/wuxiao5570/article/details/54407618
http://blog.csdn.net/laoyang360/article/details/51694519
http://blog.csdn.net/xifeijian/article/details/49617775