0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ElasticsearchのRESTfull API

Last updated at Posted at 2021-07-10

RESTfull API

Elasticsearchは直接APIを叩くだけで操作できることは特徴の一つであり、公式には沢山APIを用意してくれてます。いくつ代表的なAPIを実際に操作してみたいと思います。

事前準備

Advanced REST clientの利用方法

Advanced REST clientをインストール後、アプリ→ARCのマークでAPIを使用することができる。
api.PNG
このツールを使わない場合にはターミナルにて以下のようにコマンド操作でもできます。
例:

& curl -XPUT http://localhost:9200/dic
 {"ok":true,"acknowledged":true}

API応用

インデックス作成・削除(Index APIs)

公式:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#indices
以下は公式のサンプルコードです。

リクエスト
PUT /my-index-000001
{
  "settings": {
    "index": {
      "number_of_shards": 3,  
      "number_of_replicas": 2 
    }
  }
}

今回 indexの名前はmail-indexとする

リクエスト
URL:
PUT /mail-index
Content type: application/jaso
body: 
{
  "settings": {
    "index": {
      "number_of_shards": 3,  
      "number_of_replicas": 2 
    }
  }
}

「SEND」をクリックする
index3.PNG
200帰ってきた。できたようです。
index5.PNG

もう一回elasticsearch-headにも確認します。
http://xxx.xxx.xxx.xxx:9100/ にアクセスする。mail-indexが作成されました。
index6.PNG

インデックス削除の場合

構文
DELETE /{インデックス名}/

データー挿入(Document APIs)

  • id指定式

構文
POST /{インデックス名}/{タイプ名}/{id}

実際やってみよ~

リクエスト
URL部分
POST /mail-index/user_info/1001

body部分
{
  "ユーザID":1001,
  "ユーザ名":"鈴木太郎",
  "ステータス":0,
  "メール":"suzukitaro@gmail.com"
}
レスポンス
{
"_index": "mail-index",
"_type": "user_info",
"_id": "1001",
"_version": 1,  → ここ注意
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}

結果確認
doc1.PNG

  • id指定なし式
構文
POST /{インデックス名}/{タイプ名}

実際やってみよ~

リクエスト
URL部分
POST /mail-index/user_info

body部分
{
  "ユーザID":1002,
  "ユーザ名":"田中博",
  "ステータス":0,
  "メール":"tanaka.hiroshi@gmail.com"
}
レスポンス
{
"_index": "mail-index",
"_type": "user_info",
"_id": "r8qdj3oB7A0XUeSyl8-h",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}

結果確認
キャプチャ.PNG

id指定しない場合には自動にidが生成されることが分かった。

データー更新(Document APIs)

  • 全部更新

idは1001のユーザーのステータスを1に変更する

構文
PUT /{インデックス名}/{タイプ名}/{id}
リクエスト
URL部分
PUT /mail-index/user_info/1001

body部分
{
  "ユーザID":1001,
  "ユーザ名":"鈴木太郎",
  "ステータス":1,
  "メール":"suzukitaro@gmail.com"
}
レスポンス
{
"_index": "mail-index",
"_type": "user_info",
"_id": "1001",
"_version": 2,    → ここ注目
"result": "updated",  → ここ注目
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}

結果確認
キャプチャ.PNG

  • 一部更新

idはr8qdj3oB7A0XUeSyl8-hのユーザーのメールアドレスをyahoo.co.jpにする

構文
POST /{インデックス名}/{タイプ名}/{id}/_update
リクエスト
URL部分
POST /mail-index/user_info/r8qdj3oB7A0XUeSyl8-h/_update

body部分
{
 "doc":{
  "メール": "tanaka.hiroshi@yahoo.co.jp"
 }
}

↑部分更新のため、属性すべて入れる必要がなく、必要な属性だけでよい。

レスポンス
{
  "_index": "mail-index",
  "_type": "user_info",
  "_id": "r8qdj3oB7A0XUeSyl8-h",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 3,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

結果確認
キャプチャ.PNG

データー削除・検索(Document APIs)

  • 削除
構文
DELETE /{インデックス名}/{タイプ名}/{id}
リクエスト
URL部分
DELETE /mail-index/user_info/1001

存在しないidを削除すると404が返ってくる。

  • 一部検索
構文
GET /{インデックス名}/{タイプ名}/{id}
リクエスト
URL部分
GET /mail-index/user_info/1001
レスポンス
{
"_index": "mail-index",
"_type": "user_info",
"_id": "1001",
"_version": 2,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"ユーザID": 1001,
"ユーザ名": "鈴木太郎",
"ステータス": 1,
"メール": "suzukitaro@gmail.com"
}
}
  • 全部検索
構文
GET /{インデックス名}/{タイプ名}/_search
リクエスト
URL部分
GET /mail-index/user_info/_search

全部検索しても、登録済みの10件しか返ってこない。全件取得したい場合にはまた別途紹介する。

  • キーワード検索
構文
GET /{インデックス名}/{タイプ名}/_search?q=XXX:X
リクエスト
URL部分
GET /mail-index/user_info/_search?q=ステータス:1
レスポンス
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "mail-index",
        "_type": "user_info",
        "_id": "1001",
        "_score": 1,
        "_source": {
          "ユーザID": 1001,
          "ユーザ名": "鈴木太郎",
          "ステータス": 1,
          "メール": "suzukitaro@gmail.com"
        }
      }
    ],
    
  }
}

DSL検索

Query DSLといい、複雑な検索に使われる。

構文
POST /{インデックス名}/{タイプ名}/_search
リクエスト
URL部分
GET /mail-index/user_info/_search

Body部分
{
 "query":{
   "match":{
  "ステータス": 1
   }
 }
}
レスポンス
{
"took": 3,
"timed_out": false,
"_shards": {
  "total": 3,
  "successful": 3,
  "skipped": 0,
  "failed": 0
},
"hits": {
  "total": {
    "value": 1,
    "relation": "eq"
  },
  "max_score": 1,
  "hits": [
    {
      "_index": "mail-index",
      "_type": "user_info",
      "_id": "1001",
      "_score": 1,
      "_source": {
        "ユーザID": 1001,
        "ユーザ名": "鈴木太郎",
        "ステータス": 1,
        "メール": "suzukitaro@gmail.com"
      }
    }
  ],
  
}
}
  • 複数条件の場合
リクエスト
URL部分
GET /mail-index/user_info/_search

Body部分 年齢30以上ステータス1となる
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gt": 30
          }
        }
      }
    },
    "must": {
      "match": {
        "ステータス": 1
      }
    }
  }
}
  • 全文検索
リクエスト
URL部分
GET /mail-index/user_info/_search

Body部分 鈴木 田中が含まれるデーター
{
 "query":{
   "match":{
     "ユーザ名": "鈴木 田中"
   }
 }
}

ドキュメント存在チェック

  • 方法①

レスポンスにある「"found": true,」を確認する、true→存在、false→存在なし

  • 方法②

レスポンスコードで判断する。

リクエスト
URL部分
HEAD /{インデックス名}/{タイプ名}/{id}
レスポンスコード
200 → 存在
404 →  存在しない

ハイライト

リクエスト
URL部分
GET /mail-index/user_info/_search

Body部分 鈴木 田中が含まれるデーター
{
  "query": {
    "match": {
      "ユーザ名": "鈴木 田中"
    }
  },
  "highlight": {
    "fields":{
      "ユーザ名": {
        
      }
    }
  }
}
レスポンス
{
  "took": 55,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.4723402,
    "hits": [
      {
        "_index": "mail-index",
        "_type": "user_info",
        "_id": "r8qdj3oB7A0XUeSyl8-h",
        "_score": 1.4723402,
        "_source": {
          "ユーザID": 1002,
          "ユーザ名": "田中博",
          "ステータス": 0,
          "メール": "tanaka.hiroshi@yahoo.co.jp"
        },
        "highlight": {
          "ユーザ名": [
            "<em>田</em><em>中</em>博"
          ],
          
        }
      },
      {
        "_index": "mail-index",
        "_type": "user_info",
        "_id": "1001",
        "_score": 1.3097506,
        "_source": {
          "ユーザID": 1001,
          "ユーザ名": "鈴木太郎",
          "ステータス": 1,
          "メール": "suzukitaro@gmail.com"
        },
        "highlight": {
          "ユーザ名": [
            "<em>鈴</em><em>木</em>太郎"
          ],
          
        }
      }
    ],
    
  }
}

余談

いくつコツをご紹介します。

  • ブラウザでJson式表示

ブラウザでそのままAPIを叩いても良いですが、形式がちょっと変です。
その時最後「?pretty」を入れると、Json式で表示される。

例:http://xxx.xxx.xxx.xxx:9200/mail-index/user_info/_search?pretty

  • 検索情報を選定したい
    検索した情報が_sourceに全部入ってますね。
    不要な情報も入ってるため、必要な情報だけ検索したい場合には以下のようなURLにすればよいです。

例:http://xxx.xxx.xxx.xxx:9200/mail-index/user_info/_search?_source=ステータス

そうすると_source内のステータス情報だけ取得する。

  • _sourceだけほしい

rawのデーターだけほしい場合は以下のように取得する

例:http://xxx.xxx.xxx.xxx:9200/mail-index/user_info/1001/_source?_source=ステータス

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?