Elasticsearch の使い方

Arch Linux へのインストール方法

```bash
sudo pacman -S elasticsearch
```

サーバーの状態の確認

```bash
sudo systemctl status elasticsearch
```

サーバーの起動

```bash
sudo systemctl start elasticsearch
```

クライアントから、サーバーの確認

```text
$ curl http://127.0.0.1:9200
{
  "name" : "iwata",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "Gvg6GQ6LSJqkqZpaXSNyGA",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "unknown",
    "build_date" : "2021-08-25T17:55:24.048595Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
```

データの投入

```bash:go_insert.sh
curl -H "Content-Type: application/json" \
	-X PUT "http://127.0.0.1:9200/test/_doc/1" \
	-d '{
	"user" : "uchida",
	"counter" : 1,
	"tags" : ["red"]
}' | jq .
```

実行結果

```text
$ ./go_insert.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   209  100   152  100    57     73     27  0:00:02  0:00:02 --:--:--   101
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
```

データの値を見る

```bash:go_get.sh
curl http://127.0.0.1:9200/test/_doc/1 | jq .
```

実行結果

```text
$ ./go_get.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   136  100   136    0     0  27200      0 --:--:-- --:--:-- --:--:-- 27200
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "user": "uchida",
    "counter": 1,
    "tags": [
      "red"
    ]
  }
}
```

データの更新
>counter に 4 を加える

```bash:go_update.sh
curl -H "Content-Type: application/json" \
	-X POST "http://127.0.0.1:9200/test/_doc/1/_update" \
	-d '{
"script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}' | jq .
```

実行結果

```text
$ ./go_update.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   314  100   152  100   162   1490   1588 --:--:-- --:--:-- --:--:--  3078
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}
```

値の確認

```text
$ ./go_get.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   123  100   123    0     0  30750      0 --:--:-- --:--:-- --:--:-- 30750
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "user": "uchida",
    "counter": 5,
    "tags": [
      "red"
    ]
  }
}
```

もう一度、go_update.sh を実行すると counter は 9 になります。

データの削除

```bash:go_del.sh
curl -XDELETE 'http://localhost:9200/test'
```

実行結果

```text
$ ./go_del.sh 
{"acknowledged":true}
```

ヒープメモリを小さくする方法
>1G を 512M に変更
設定を変更してから、Elasticsearch を再起動

```text://etc/elasticsearch/jvm.options
#-Xms1g
#-Xmx1g
-Xms512m
-Xmx512m
```