[setting] elasticsearch 環境構築メモ


# 環境構築メモ

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

## brewを使用してinstall

macOS

```bash
% 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/
```

```bash
% elasticsearch --version
Version: 7.17.4, Build: default/tar/79878662c54c886ae89206c685d9f1051a9d6411/2022-05-18T18:04:20.964345128Z, JVM: 18.0.1.1
```

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

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

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


### Elasticsearch起動

```bash
% elasticsearch
```

```bash
% curl http://localhost:9200/
```

```bash
{
  "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`を付与
```bash
% curl --include -XPUT "http://localhost:9200/foo_index?pretty"
```

```bash
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`取得
```bash
% curl --include -XGET "http://localhost:9200/foo_index?pretty"
```

```bash
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形式の値を追加
```bash
% 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."
}'

```

```bash
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形式の二つ目の値を追加
```bash
% curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'
```

```bash
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
}
```

実験のため、同じドキュメントをもう一つ追加。
```bash
% curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'
```


```bash
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`を指定して値を取得
```bash
% curl --include -XGET "http://localhost:9200/foo_index/_doc/7VqccJMBqNjbFLONavLW?pretty"
```

```bash
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": "夜の空気"`として、条件に一致する値を取得。

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

```

追加された二つの値が取得されていることを確認する。
```bash
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`をダウンロードします。

```bash
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が「開発元が不明のため実行できません」的なエラーを出力するため、実行できないです。なので、ダウンロードしたフォルダに対して、以下を実行して許可します。

```bash
% 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
```

成功です。

https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html

### PATHを通す

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

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

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

適用
```bash
source ~/.zshrc
```

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

成功です。

<details><summary>追記 brewの方を削除</summary>

探索して見つけた順に表示されます。
```bash
% 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
```
　
</details>



## 参考

[elastic searchをローカルで使用する場合にマウントポイント変更する方法](https://kobitosan.hatenablog.com/entry/2014/07/30/071700)

https://qiita.com/rjkuro/items/95f71ad522226dc381c8

https://qiita.com/Kawanji01/items/70d959bd2f4ab04005d5