搭建 Elasticsearch、Kibana、Sudachi 环境备忘录

首先

我将介绍从头开始构建Elasticsearch、Kibana和Sudachi的步骤。
此外,我也会提供修改Sudachi插件配置以改变分词行为的步骤。

环境

環境バージョンOSUbuntu 18.04.3 LTSJavaOpenJDK 1.8.0_232Elasticsearch7.5.1Kibana7.5.1

安装Elasticsearch

请在内核参数上进行更改。

增加Elasticsearch可用的内存映射。

$ sudo vi /etc/sysctl.d/99-sysctl.conf
vm.max_map_count = 262144

$ sudo sysctl --system

下载和解压Elasticsearch

可以从官方网站下载Elasticsearch,也可以直接使用wget获取。

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-linux-x86_64.tar.gz

下载完成后,将进行解压和配置。
配置位置已设置为/opt/elasticsearch。
似乎无法使用root权限启动,因此请将该目录的所有者设置为适当的用户。

$ tar xvzf elasticsearch-7.5.1-linux-x86_64.tar.gz
$ sudo mv elasticsearch-7.5.1 /opt/elasticsearch
$ sudo chown -R /opt/elasticsearch [GROUP:USER]

设置环境变量

将Elasticsearch的根目录设置为环境变量ES_HOME。

export ES_HOME=/opt/elasticsearch

修改Elasticsearch的配置

在这里,我们将设置为在单节点上运行,并且可以从本地以外的地方进行访问。

$ vi /opt/elasticsearc/config/elasticsearch.yml
network.host: 0.0.0.0
discovery.seed_hosts: ["127.0.0.1", "[::1]"]
discovery.type: single-node

启动Elasticsearch

以下为Elasticsearch的启动方式,使用-d选项可在后台运行。

$ cd /opt/elasticsearch
$ ./bin/elasticsearch -d -p pid

在Web浏览器中访问 http://[主机]:9200 时,会显示如下内容。

{
  "name" : [サーバ名],
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "dudQ9w_mR9i3_JbJOod7Zw",
  "version" : {
    "number" : "7.5.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96",
    "build_date" : "2019-12-16T22:57:37.835892Z",
    "build_snapshot" : false,
    "lucene_version" : "8.3.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

另外,您可以访问http://[主机]:9200/_cat/health?v来检查集群的状态。

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1577875702 10:48:22  elasticsearch yellow          1         1      4   4    0    0        1             0                  -                 80.0%

Kibana的安装

下载和安装Kibana

可以通过官方网站下载Kibana。
或者可以使用wget直接获取。

$ wget https://artifacts.elastic.co/downloads/kibana/kibana-7.5.1-linux-x86_64.tar.gz

完成下载后,请解压并安排好位置。
位置设定为/opt/kibana。

$ tar xvzf kibana-7.5.1-linux-x86_64.tar.gz
$ sudo mv kibana-7.5.1-linux-x86_64 /opt/kibana

修改 Kiabana 的设置

将Kibana与Elasticsearch建立连接,并设置为可以从远程访问。

$ sudo vi /opt/kibana/config/kibana.yml
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://[Elasticsearchのホスト]:9200"]

Kibana的启动

我打开Kibana并尝试在Web浏览器中访问。

$ /opt/kibana/bin/kibana

当访问http://[ホスト]:5601时,将显示Kibana界面。

当您访问左侧工具栏的“开发工具”,将显示可以执行查询的“控制台”。在控制台中输入并执行以下查询以进行操作确认。

get _cat/health?v

以下内容将以以下的方式在右边的面板中输出。

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1577881593 12:26:33  elasticsearch green           1         1      3   3    0    0        0             0                  -                100.0%

安装Sudachi

从Github获取用于Elasticsearch的插件。

$ git clone https://github.com/WorksApplications/elasticsearch-sudachi.git

使用Maven来打包插件。在这个过程中,需要在pom.xml中记录Elasticsearch的版本。
打包文件将会在target/release/目录下创建。

$ cd elasticsearch-sudachi
$ vi pom.xml
<elasticsearch.version>7.5.1</elasticsearch.version>

$ mvn package

成功時には「BUILD SUCCESS」と表示されます。作成されたプラグインは、Elasticsearchにインストールされます。インストールするファイルは、コンソールの最後に表示されるZIPファイルです。[INFO] るzip: /somewhere/elasticsearch-sudachi/target/releases/analysis-sudachi-elasticsearch7.5-1.3.3-SNAPSHOT.zip をビルドしています。

$ /opt/elasticsearch/bin/elasticsearch-plugin install file:///[プラグインのパッケージファイルパス(zip)]

[=================================================] 100%??
-> Installed analysis-sudachi

我会确认是否已正确安装。

$ /opt/elasticsearch/bin/elasticsearch-plugin list
analysis-sudachi

安装Sudachi词典

获取Sudachi词典并进行设置。可以从官方网站获取词典。
Sudachi词典有三种类型:small、core和full,但这里我们将使用full。

我們將把Sudachi的字典和設定存儲在以下文件夾中:/opt/elasticsearch/config/sudachi_tokenizer。

# 辞書をダウンロードして解凍する。
$ wget https://object-storage.tyo2.conoha.io/v1/nc_2520839e1f9641b08211a5c85243124a/sudachi/sudachi-dictionary-20191224-full.zip
$ unzip sudachi-dictionary-20191224-full.zip

# Sudachiの辞書や設定用のディレクトリを作成し、辞書ファイルをコピーする。
$ mkdir /opt/elasticsearch/config/sudachi_tokenizer
$ cp sudachi-dictionary-20191224/system_full.dic /opt/elasticsearch/config/sudachi_tokenizer

为了使用Sudachi设置,除了插件之外还需要Sudachi本身,您可以从Github获取。
然后,复制完整的词典文件的配置文件(json)。

$ git clone https://github.com/WorksApplications/Sudachi.git
$ cp Sudachi/src/main/resources/sudachi_fulldict.json /opt/elasticsearch/config/sudachi_tokenizer/

请在此处重新启动Elasticsearch和Kibana。
以下是停止和启动Elasticsearch的示例。
在此步骤中,假设Kibana在前端运行,请使用CTRL+C停止,然后执行/opt/kibana/bin/kibana。

$ cd /opt/elasticsearch
$ kill -9 `cat pid`
$ ./bin/elasticsearch -d -p pid

对Sudachi进行操作验证

我会使用Sudachi来确认它是否正常工作。在这里,我们将使用Kibana创建一个用于测试的索引。
在Web浏览器中打开Kibana(http://[Kibana主机]:5601),并显示Dev Tools控制台。

将Elasticsearch的索引进行注册

在 Elasticsearch 中,索引相当于关系数据库中的表。在这里,我们尝试注册 Sudachi 的测试索引。

在控制台上粘贴并执行以下查询,将创建索引。

PUT /test_sudachi
{
  "settings": {
    "number_of_shards": "1",
    "analysis": {
      "analyzer": {
        "sudachi_analyzer": {
          "filter": [],
          "type": "custom",
          "tokenizer": "sudachi_tokenizer"
        }
      },
      "tokenizer": {
        "sudachi_tokenizer": {
          "mode": "search",
          "type": "sudachi_tokenizer",
          "discard_punctuation": "true",
          "resources_path": "/opt/elasticsearch/config/sudachi_tokenizer/",
          "settings_path": "/opt/elasticsearch/config/sudachi_tokenizer/sudachi_fulldict.json"            
}}}}}

如果右侧窗格显示如下内容,则表示成功。

{ "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_sudachi" }

另外,执行以下查询将显示所选索引的配置信息。

GET test_sudachi

我尝试用Sudachi进行分词。

我將嘗試使用適當的方法將字符串進行分詞。請在控制台輸入以下查詢並執行。

GET /test_sudachi/_analyze
{ "analyzer":"sudachi_analyzer",
  "text":"私は青いペンを持っている。しかし、赤いペンは持っていない。" }

结果将以分词形式输出在右侧栏中。

{ "tokens" : [
  { "token" : "私", "start_offset" : 0, "end_offset" : 1, "type" : "word", "position" : 0  },
  { "token" : "は", "start_offset" : 1, "end_offset" : 2, "type" : "word", "position" : 1  },
  { "token" : "青い", "start_offset" : 2, "end_offset" : 4, "type" : "word", "position" : 2  },
  { "token" : "ペン", "start_offset" : 4, "end_offset" : 6, "type" : "word", "position" : 3  },
  { "token" : "を", "start_offset" : 6, "end_offset" : 7, "type" : "word", "position" : 4  },
  { "token" : "持っ", "start_offset" : 7, "end_offset" : 9, "type" : "word", "position" : 5  },
  { "token" : "て", "start_offset" : 9, "end_offset" : 10, "type" : "word", "position" : 6  },
  { "token" : "いる", "start_offset" : 10, "end_offset" : 12, "type" : "word", "position" : 7  },
  { "token" : "しかし", "start_offset" : 13, "end_offset" : 16, "type" : "word", "position" : 8  },
  { "token" : "赤い", "start_offset" : 17, "end_offset" : 19, "type" : "word", "position" : 9  },
  { "token" : "ペン", "start_offset" : 19, "end_offset" : 21, "type" : "word", "position" : 10  },
  { "token" : "は", "start_offset" : 21, "end_offset" : 22, "type" : "word", "position" : 11  },
  { "token" : "持っ", "start_offset" : 22, "end_offset" : 24, "type" : "word", "position" : 12  },
  { "token" : "て", "start_offset" : 24, "end_offset" : 25, "type" : "word", "position" : 13  },
  { "token" : "い", "start_offset" : 25, "end_offset" : 26, "type" : "word", "position" : 14  },
  { "token" : "ない", "start_offset" : 26, "end_offset" : 28, "type" : "word", "position" : 15  }
]}

Sudachi的设置更改

我会用以下两个插件来改变Sudachi的分词行为。

    • 除外する品詞の設定 (sudachi_part_of_speech)

 

    動詞と形容詞の終止形化 (sudachi_baseform)

要更改Sudachi的行为,需要通过REST-API更改相应索引的配置。

索引的变动流程

更改索引设置的步骤如下:

    • インデックスのクローズ

 

    • 設定変更

 

    インデックスのオープン

索引的关闭

使用POST方法执行/[索引]/_close请求。

在Kibana的控制台中执行以下操作将关闭。

POST /test_sudachi/_close

结果如下所示。

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "indices" : {
    "test_sudachi" : {
      "closed" : true
}}}

更改索引设置

当关闭特定索引时,将修改索引的设置。
通过PUT方法执行索引的更新操作,使用/[索引]/_settings路径。

以下的更改是将sudachi_baseform和sudachi_part_of_speech作为Sudachi的插件进行使用。
此外,在sudachi_part_of_speech中,我们指定了要排除的词性,包括”接続詞”、”助動詞”、”助詞”、”記号”、”補助記号”、”接続詞”。

请参考Elasticsearch插件中以下文件中可指定的词性。

elasticsearch-sudachi/src/main/resources/com/worksap/nlp/lucene/sudachi/ja/stoptags.txt 的中文释义

PUT /test_sudachi/_settings
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "sudachi_analyzer": {
            "filter": [
              "sudachi_baseform",
              "my_posfilter"
        ],
            "tokenizer": "sudachi_tokenizer",
            "type": "custom"
          }
        },
        "filter":{
         "my_posfilter":{
          "type":"sudachi_part_of_speech",
          "stoptags":[
            "接続詞","助動詞","助詞","記号","補助記号","接続詞"
            ]
}}}}}}

结果如下所示。

{ "acknowledged" : true }

索引的开启

结果如下。

{ "acknowledged" : true,
  "shards_acknowledged" : true }

我试着用拆句的方式写

我将使用与刚才相同的句子进行分词试验。

GET /test_sudachi/_analyze
{ "analyzer":"sudachi_analyzer",
  "text":"私は青いペンを持っている。しかし、赤いペンは持っていない。" }

结果如下所示。
动词以原形呈现,不包括连接词等被排除的单词未被输出。

{ "tokens" : [
  {"token" : "私", "start_offset" : 0, "end_offset" : 1, "type" : "word", "position" : 0 },
  {"token" : "青い", "start_offset" : 2, "end_offset" : 4, "type" : "word", "position" : 2 },
  {"token" : "ペン", "start_offset" : 4, "end_offset" : 6, "type" : "word", "position" : 3 },
  {"token" : "持つ", "start_offset" : 7, "end_offset" : 9, "type" : "word", "position" : 5 },
  {"token" : "いる", "start_offset" : 10, "end_offset" : 12, "type" : "word", "position" : 7 },
  {"token" : "赤い", "start_offset" : 17, "end_offset" : 19, "type" : "word", "position" : 9 },
  {"token" : "ペン", "start_offset" : 19, "end_offset" : 21, "type" : "word", "position" : 10 },
  {"token" : "持つ", "start_offset" : 22, "end_offset" : 24, "type" : "word", "position" : 12 },
  {"token" : "いる", "start_offset" : 25, "end_offset" : 26, "type" : "word", "position" : 14 }
]}

删除索引

删除最后创建的索引。
使用DELETE命令执行删除索引的操作,索引名称为/[索引]/?pretty。

DELETE /test_sudachi?pretty

结果如下:

{ "acknowledged" : true }

概括

我记录了从零开始构建Elasticsearch、Kibana和Sudachi的步骤。
我使用Elasticsearch的REST-API来更改索引的设置,并尝试使用Sudachi插件来改变分词的行为。

bannerAds