Elastic SearchのAPIを試す

# TL;DR

Elastic SearchのAPIの中から
実務で良く使いそうなAPIを叩いていました。

https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html

この記事では、ローカルPCでセットアップしたElastic SearchのREST APIを呼び出しています。
セットアップ方法は以下の記事に記載しています。

https://qiita.com/tobita_yoshiki/items/9de9cfc4beed26b792c1

# Index API

## Create Index API

### `PUT /<index>`

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#indices-create-index

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X PUT "https://localhost:9200/my-index-000001?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my-index-000001"
}
```

## Delete index API

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html#indices-delete-index

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X DELETE "https://localhost:9200/my-index-000001/"
{"acknowledged":true}%
```

# Document API

## Index API

### `POST /<target>/_doc/`

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X POST "https://localhost:9200/my-index-000001/_doc" -H 'Content-Type: application/json' -d '{"k": "v"}'
{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}%
```

## Search API

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

### `GET /<target>/_search`

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X GET "https://localhost:9200/my-index-000001/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'
{"took":132,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_score":1.0,"_source":{"k": "v"}}]}}%
```

## UPDATE API

### `POST /<index>/_update/<_id>`

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X POST "https://localhost:9200/my-index-000001/_update/pck94o8BH2JNu7nWA_Fi" -H 'Content-Type: application/json' -d '{"doc": {"u": "updated"}}'
{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
```

```txt:データの更新チェック
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X GET "https://localhost:9200/my-index-000001/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'
{"took":15,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_score":1.0,"_source":{"k":"v","u":"updated"}}]}}
```

## Delete API

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html

### `DELETE /<index>/_doc/<_id>`

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X DELETE "https://localhost:9200/my-index-000001/_doc/pck94o8BH2JNu7nWA_Fi"
{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}%
```

## Delete by query API

```
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X POST "https://localhost:9200/my-index-000001/_delete_by_query" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'
{"took":70,"timed_out":false,"total":0,"deleted":0,"batches":0,"version_conflicts":0,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[]}%
```