Elasticsearchの基本操作

# Elasticsearchの基本操作 RESTFUL APIツールを使う

- curlコマンド
- GUI(POstman, Kibana, Dev Tools)


基本的なリクエスト例

## 1.動作確認

起動
```bash
elasticsearch
```

動作確認。elasticsearchのバージョンやクラスタ情報が返ります。
```bash
curl http://localhost:9200/
```

## 2.インデックス作成

```bash
curl -X PUT "http://localhost:9200/my_index"
```

## 3.データの追加

作成済みのindexを指定して、json形式の値を送信します。

```bash 
curl -X POST "http://localhost:9200/my_index/_doc/1" -H "Content-Type: application/json" -d' { "name": "John Doe", "age": 30, "city": "New York" }'
``` 

response
```
{"_index":"my_index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}% 
```

## 4.データの検索

```bash 
curl -X GET "http://localhost:9200/my_index/_search?q=name:John"
```

response
```
{"took":509,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.2876821,"hits":[{"_index":"my_index","_type":"_doc","_id":"1","_score":0.2876821,"_source": { "name": "John Doe", "age": 30, "city": "New York" }}]}}% 
```

## 5. データの更新

```bash 
curl -X POST "http://localhost:9200/my_index/_update/1" -H "Content-Type: application/json" -d' { "doc": { "age": 31 } }' 
```

response
```bash
{"_index":"my_index","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}% 
```

# Kibana

## 1. kibana起動
Kibanaを利用すると、Elasticsearchに対する操作や可視化が簡単になります。

kibanaを起動
```bash
kibana
```

kibanaにアクセスする。
http://localhost:5601

## 2. Dev Toolsでクエリ送信

Kibanaの「Dev Tools」で先ほど同様にElasticsearch APIを実行してみます。


```bash
GET /my_index/_search
{
  "query": {
    "match": { "name": "John" }
  }
}
```

kibanaのDev ToolsでAPI実行します。右上の緑ボタンをポチッと押します。`200-OK`出たら成功。

![スクリーンショット 2024-12-09 11.08.56.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/443312/e766ff59-3fc2-99a8-00c1-34c100e6328d.png)

## 複数インデックスを取得

### 値の追加

値の追加
```bash
POST /my_index2/_doc/
{
  "properties":{
    "channel_id": 4,
    "content": "トルトル"
  }
}
```

値の追加
```bash
POST /my_index3/_doc/
{
  "properties":{
    "channel_id": 1,
    "title": "赤毛のあん"
  }
}
```

値の追加
```bash
POST /my_index3/_doc/
{
  "properties":{
    "channel_id": 1,
    "title": "Tete"
  }
}
```

### 複数インデックスを指定して取得

```bash
GET /my_index2,my_index3/_search
{
  "query":{
    "bool":{
      "should": [
        { "wildcard": { "properties.title": "*" } },
        { "wildcard": { "properties.content": "*" } }
      ]
    }
  }
}
```

output
```bash
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index2",
        "_type" : "_doc",
        "_id" : "fZVhsJMBYYz9d7HPfLZH",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 4,
            "content" : "トルトル"
          }
        }
      },
      {
        "_index" : "my_index3",
        "_type" : "_doc",
        "_id" : "fpVjsJMBYYz9d7HPs7aQ",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "赤毛のあん"
          }
        }
      },
      {
        "_index" : "my_index3",
        "_type" : "_doc",
        "_id" : "f5VksJMBYYz9d7HPl7br",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "Tete"
          }
        }
      }
    ]
  }
}
```

## その他

全てを取り出す

```bash
GET /_search
{
    "query": {
        "match_all": {}
    }
}
```

インデックスで絞り込み
```bash
GET /my_index/_search
{
    "query": {
        "match_all": {}
    }
}
```

# 参考
https://qiita.com/sakashin10291029/items/9a6ba5738a45577db481#%E3%81%93%E3%81%AE%E8%A8%98%E4%BA%8B%E3%81%A7%E8%A7%A3%E8%AA%AC%E3%81%97%E3%81%AA%E3%81%84%E3%81%93%E3%81%A8