Elasticsearch 7 OSS Only 版を Ubuntu 20.04 LTS (Focal Fossa) にインストールする


概要

APT リポジトリの設定

apt から使用するリポジトリが信頼できるものであると設定するために Elasticsearch の PGP 認証鍵をインポートする。

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
OK

Elasticsearch 7 OSS Only 版の APT リポジトリを追加する。

$ echo "deb https://artifacts.elastic.co/packages/oss-7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
deb https://artifacts.elastic.co/packages/oss-7.x/apt stable main

elasticsearch-oss パッケージのインストール

$ sudo apt update && sudo apt install elasticsearch-oss
(中略)
以下のパッケージが新たにインストールされます:
  elasticsearch-oss
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
232 MB のアーカイブを取得する必要があります。
この操作後に追加で 419 MB のディスク容量が消費されます。
取得:1 https://artifacts.elastic.co/packages/oss-7.x/apt stable/main amd64 elasticsearch-oss amd64 7.9.0 [232 MB]
232 MB を 21秒 で取得しました (11.1 MB/s)                                                          

systemd による起動管理

設定内容を反映するために systemd の設定をリロードする。

$ sudo systemctl daemon-reload

systemctl enable で Elasticsearch サービスを有効にする。

$ sudo systemctl enable elasticsearch
Synchronizing state of elasticsearch.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable elasticsearch
Created symlink /etc/systemd/system/multi-user.target.wants/elasticsearch.service → /lib/systemd/system/elasticsearch.service.

Elasticsearch の起動

systemctl start で Elasticsearch サービスを起動する。

$ sudo systemctl start elasticsearch

systemctl status でサービスの状態を確認する。

$ sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-08-30 14:12:00 JST; 11s ago
       Docs: https://www.elastic.co
   Main PID: 38446 (java)
      Tasks: 52 (limit: 2318)
     Memory: 330.7M
     CGroup: /system.slice/elasticsearch.service
             └─38446 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.t>

 8月 30 14:11:45 foo-bar-aaaaa systemd[1]: Starting Elasticsearch...
 8月 30 14:12:00 foo-bar-aaaaa systemd[1]: Started Elasticsearch.

Elasticsearch の起動確認

curl でアクセスして Elasticsearch が起動していることを確認する。

$ curl http://localhost:9200/
{
  "name" : "foo-bar-aaaaa",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "XXXXXXXXXXXXXXXXXXXXXX",
  "version" : {
    "number" : "7.9.0",
    "build_flavor" : "oss",
    "build_type" : "deb",
    "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
    "build_date" : "2020-08-11T21:36:48.204330Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

基本的な動作の確認

インデックスを作成する。

$ curl --include -XPUT "http://localhost:9200/foo_index?pretty"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 85

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

インデックスにドキュメントを追加する。

$ curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
> -H 'Content-Type: application/json' \
> -d '{
>   "foo_user": "Alice",
>   "foo_message": "The night was young, and so was he. But the night was sweet, and he was sour."
> }'
HTTP/1.1 201 Created
Location: /foo_index/_doc/unbQPXQBOy7T_EM8fvEu
content-type: application/json; charset=UTF-8
content-length: 242

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "unbQPXQBOy7T_EM8fvEu",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

ドキュメントを検索。

$ curl --include -XGET "http://localhost:9200/foo_index/_search?pretty" \
> -H 'Content-Type: application/json' \
> -d '
> {
>   "query": {
>     "match": {
>       "foo_message": "night was sweet"
>     }
>   }
> }'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 586

{
  "took" : 177,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1700915,
    "hits" : [
      {
        "_index" : "foo_index",
        "_type" : "_doc",
        "_id" : "unbQPXQBOy7T_EM8fvEu",
        "_score" : 1.1700915,
        "_source" : {
          "foo_user" : "Alice",
          "foo_message" : "The night was young, and so was he. But the night was sweet, and he was sour."
        }
      }
    ]
  }
}

主な設定ファイルの場所

/etc/default/elasticsearch
/etc/elasticsearch/.elasticsearch.keystore.initial_md5sum
/etc/elasticsearch/elasticsearch.keystore
/etc/elasticsearch/elasticsearch.yml
/etc/elasticsearch/jvm.options
/etc/elasticsearch/log4j2.properties
/lib/systemd/system/elasticsearch.service

参考資料