0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[setting] elasticsearch 環境構築メモ

Last updated at Posted at 2024-11-28

環境構築メモ

二つの場合のインストール手順をメモります。brewでインストールだとelastic search 8系がインストールされませんでした。近似近傍探索には8系が良いとのことで8系を二つ目の手順でインストールしてます。

brewを使用してinstall

macOS

% brew install elastic/tap/elasticsearch-full

logは次のようになる。[user name]はwhoamiの値です。

Data:    /opt/homebrew/var/lib/elasticsearch/elasticsearch_[user name]/
Logs:    /opt/homebrew/var/log/elasticsearch/elasticsearch_[user name].log
Plugins: /opt/homebrew/var/elasticsearch/plugins/
Config:  /opt/homebrew/etc/elasticsearch/
% elasticsearch --version
Version: 7.17.4, Build: default/tar/79878662c54c886ae89206c685d9f1051a9d6411/2022-05-18T18:04:20.964345128Z, JVM: 18.0.1.1

defaultのまま使用する場合は下記不要

home直下にフォルダを作成してその中にファイルをコピー

% cp /opt/homebrew/etc/elasticsearch/elasticsearch.yml .
% cp /opt/homebrew/etc/elasticsearch/jvm.options .
% cp /opt/homebrew/etc/elasticsearch/log4j2.properties .

Elasticsearch起動

% elasticsearch
% curl http://localhost:9200/
{
  "name" : "mXXMHJR7K4T",
  "cluster_name" : "elasticsearch_mariko.shozawa",
  "cluster_uuid" : "xtAgjMmqTCCjqEMCrKLNDg",
  "version" : {
    "number" : "7.17.4",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
    "build_date" : "2022-05-18T18:04:20.964345128Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

foo_indexを付与

% curl --include -XPUT "http://localhost:9200/foo_index?pretty"
HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 85

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

foo_index取得

% curl --include -XGET "http://localhost:9200/foo_index?pretty"
HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 541

{
  "foo_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "foo_index",
        "creation_date" : "1732761030945",
        "number_of_replicas" : "1",
        "uuid" : "QN8Oc8-LQie9diS2sST2Vg",
        "version" : {
          "created" : "7170499"
        }
      }
    }
  }
}

foo_indexにjson形式の値を追加

% 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/61qbcJMBqNjbFLONnPJ0
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 242

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

foo_indexにjson形式の二つ目の値を追加

% curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'
HTTP/1.1 201 Created
Location: /foo_index/_doc/7FqbcJMBqNjbFLON-_LQ
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 242

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

実験のため、同じドキュメントをもう一つ追加。

% curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'
HTTP/1.1 201 Created
Location: /foo_index/_doc/7VqccJMBqNjbFLONavLW
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 242

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

foo_indexを指定して値を取得

% curl --include -XGET "http://localhost:9200/foo_index/_doc/7VqccJMBqNjbFLONavLW?pretty"
HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 332

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "7VqccJMBqNjbFLONavLW",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "foo_user" : "ボブ",
    "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
  }
}

検索条件"foo_message": "夜の空気"として、条件に一致する値を取得。

% curl --include -XGET "http://localhost:9200/foo_index/_search?pretty" \
-H 'Content-Type: application/json' \
-d '
{
  "query": {
    "match": {
      "foo_message": "夜の空気"
    }
  }
}'

追加された二つの値が取得されていることを確認する。

HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.17.4-79878662c54c886ae89206c685d9f1051a9d6411 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 952

{
  "took" : 48,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.3920753,
    "hits" : [
      {
        "_index" : "foo_index",
        "_type" : "_doc",
        "_id" : "7FqbcJMBqNjbFLON-_LQ",
        "_score" : 2.3920753,
        "_source" : {
          "foo_user" : "ボブ",
          "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
        }
      },
      {
        "_index" : "foo_index",
        "_type" : "_doc",
        "_id" : "7VqccJMBqNjbFLONavLW",
        "_score" : 2.3920753,
        "_source" : {
          "foo_user" : "ボブ",
          "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
        }
      }
    ]
  }
}

version指定でダウンロードする方法

homeで下記を実行。今回はelasticsearch-8.16.1をダウンロードします。

curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.16.1-darwin-x86_64.tar.gz
curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.16.1-darwin-x86_64.tar.gz.sha512 | shasum -a 512 -c - 
tar -xzf elasticsearch-8.16.1-darwin-x86_64.tar.gz

!!重要!!

バイナリを実行する前に、

mac userはGatekeeperが「開発元が不明のため実行できません」的なエラーを出力するため、実行できないです。なので、ダウンロードしたフォルダに対して、以下を実行して許可します。

% xattr -d -r com.apple.quarantine elasticsearch-8.16.1

実行確認

cd elasticsearch-8.16.1
./bin/elasticsearch --version
Version: 8.16.1, Build: tar/ffe992aa682c1968b5df375b5095b3a21f122bf3/2024-11-19T16:00:31.793213192Z, JVM: 23

成功です。

PATHを通す

.zshrcファイルでバイナリに対してパスを通します。brew installしたものよりも先にヒットしてほしいので、左にパスを追加します。(brew installしたものはuninstallしてしまいましょ)

sudo vi ~/.zshrc
export ELASTICSEARCH="/Users/[user name]/elasticsearch-8.16.1/bin"
export PATH="$ELASTICSEARCH:$PATH"

パスが追加されているか確認します。

echo $PATH

適用

source ~/.zshrc

version確認

% elasticsearch --version
Version: 8.16.1, Build: tar/ffe992aa682c1968b5df375b5095b3a21f122bf3/2024-11-19T16:00:31.793213192Z, JVM: 23

成功です。

追記 brewの方を削除

探索して見つけた順に表示されます。

% where elasticsearch 
/Users/[user name]/elasticsearch-8.16.1/bin/elasticsearch
/opt/homebrew/bin/elasticsearch

brewの方を削除します

% brew uninstall elasticsearch-full
% where elasticsearch    
/Users/mariko/elasticsearch-8.16.1/bin/elasticsearch

 

参考

elastic searchをローカルで使用する場合にマウントポイント変更する方法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?