0
0

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 のREST APIs その②

0
Posted at

概要

前回Elasticsearch REST APIsの使用方法の続きとなります。

一括検索(Multi-document APIs) mget

↑公式にはGETメソッド使ってましたが、間違いだったかな?

例:
idは"1001","1002","r8qdj3oB7A0XUeSyl8-h"であるデータ全部検索
以下の例にはid1002が存在してないものを前提とする。
idが存在するにもかかわらずエラーせずにレスポンスする。

リクエスト
URL
POST /{インデックス名}/{タイプ名}/_mget
Body
{
"ids":["1001","1002","r8qdj3oB7A0XUeSyl8-h"]
}
レスポンス
{
  "docs": [
    {
      "_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"
      }
    },
    {
      "_index": "mail-index",
      "_type": "user_info",
      "_id": "1002",
      "found": false
    },
    {
      "_index": "mail-index",
      "_type": "user_info",
      "_id": "r8qdj3oB7A0XUeSyl8-h",
      "_version": 2,
      "_seq_no": 3,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "ユーザID": 1002,
        "ユーザ名": "田中博",
        "ステータス": 0,
        "メール": "tanaka.hiroshi@yahoo.co.jp"
      }
    }
  ],
  
}

データ一括挿入(Multi-document APIs) bulk

リクエスト
URL部
POST /{インデックス名}/{タイプ名}/_bulk
BODY部
{ "create" : { "_index" : "mail-index", "_type": "user_info"} }
{"ユーザID": 1003,"ユーザ名": "前川理恵","ステータス": 0,"メール": "maekawa.rie@yahoo.co.jp"}
{ "create" : { "_index" : "mail-index", "_type": "user_info"} }
{"ユーザID": 1004,"ユーザ名": "木下美緒","ステータス": 0,"メール": "kinoshita.mio@yahoo.co.jp"}

↑リクエストBODY部にはエラーが表示されるかもしれないが、無視で構わない。

検索ページング

前回Elasticsearchを使って検索した際に最大で登録済みの10件しか戻ってこない話があった。
ヒットした全部のデーター(ドキュメント)を取得した場合はページングが必要となる。

size:一ページに何件にする
form:何件後から取得する、ディフォルトは0となる
例:
GET /_search?size=5  → 最初の5件ください。
GET /_search?size=5&from=10  → 5件ほしい。11件目から取得する。

一般的な検索エンジンは最大で1000件しか検索結果が戻らないようです。それはページネーションによって捨てるデーターが多すぎるため、性能が落ちるからそうだ。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?