Ubuntu 安装 Elasticsearch 和 elasticsearch-analysis-ik 分词

假设你已经安装了 JDK ,如没有请查看这篇文章进行安装JDK

安装 Elasticsearch

官方文档

1、下载 Elasticsearch

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz

2、 解压

tar -xzf elasticsearch-6.4.0.tar.gz

3.、启动

cd elasticsearch-6.4.0/
./bin/elasticsearch  

或者 作为守护进程运行

./bin/elasticsearch -d -p pid

Elasticsearch 会在默认的9200端口运行

curl localhost:9200

可能遇到的问题:

1、使用非 root用户启动ES,而该用户的文件权限不足而被拒绝执行

Java HotSpot(TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2018-09-01T08:41:24,515][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.0.jar:6.4.0]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.4.0.jar:6.4.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.4.0.jar:6.4.0]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.4.0.jar:6.4.0]
        ... 6 more

解决方式:

sudo chown -R vagrant:vagrant /var/elasticsearch-6.4.0

重新启动

./bin/elasticsearch -d

查看是否启动

curl localhost:9200

如返回如下格式,即表示安装启动成功

{
  "name" : "iK2w1Zv",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "u8XYlB1ZQACw0b1W0MA0Pw",
  "version" : {
    "number" : "6.4.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "595516e",
    "build_date" : "2018-08-17T23:18:47.308994Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
  1. Text file busy
    Could not rename log file 'logs/gc.log' to 'logs/gc.log.03' (Text file busy)

    解决方式:

找出占用该文件的进程:

sudo fuser 文件名

杀死占用该文件的进程

sudo kill -9 进程id

安装 elasticsearch-analysis-ik 分词

官方文档

我们安装的 Elasticsearch 版本为 6.4.0,该版本匹配的版本号也是6.4.0 如果复制文档地址可能会报如下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Plugin [analysis-ik] was built for Elasticsearch version 6.3.0 but version 6.4.0 is running
        at org.elasticsearch.plugins.PluginsService.verifyCompatibility(PluginsService.java:339)
        at org.elasticsearch.plugins.InstallPluginCommand.loadPluginInfo(InstallPluginCommand.java:717)
        at org.elasticsearch.plugins.InstallPluginCommand.installPlugin(InstallPluginCommand.java:792)
        at org.elasticsearch.plugins.InstallPluginCommand.install(InstallPluginCommand.java:775)
        at org.elasticsearch.plugins.InstallPluginCommand.execute(InstallPluginCommand.java:231)
        at org.elasticsearch.plugins.InstallPluginCommand.execute(InstallPluginCommand.java:216)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124)
        at org.elasticsearch.cli.MultiCommand.execute(MultiCommand.java:77)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.plugins.PluginCli.main(PluginCli.java:47)

所以到 这里 复制6.4.0 的链接地址

安装

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip

重新启动 elasticsearch

./bin/elasticsearch -d

查看效果:

curl -H 'Content-Type: application/json'  -XGET 'localhost:9200/_analyze?
pretty' -d '{"analyzer":"ik_max_word","text":"贺德强"}'

正常的话 会返回如下内容:

{
  "tokens" : [
    {
      "token" : "贺",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "德",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "强",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    }
  ]
}
1个回复
1

hedeqiang hedeqiang

该内容已过时、请安装最新版、内置jdk

0 / 180
网站公告

LaravelCode 正式上线啦

--->>>体验新站

今日诗词
正在加载今日诗词....
标签云
PHP Laravel API 随想 Node.js Linux Yii2.0 Redis 队列 JavaScript Vue Java MySQL 微信小程序 Python Hyperf 转载