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 1 year has passed since last update.

Elasticsearchで覚えておくと便利なAPI

Posted at

はじめに

本稿ではElasticsearchで利用するクエリ・小技を備忘のためにまとめます。
主にインデックス・ドキュメントの検索関連のクエリがメインです。

インデックス一覧の確認

GET _cat/indices

ドキュメントの全件検索

{indexName} は検索対象のインデックスを指定してください。

GET {indexName}/_search
{
 "query": {"match_all": {}} 
}

上記ではデフォルトの件数(10000件)までしか検索結果にヒットしません。
正確な全件件数を把握したい場合は、以下のように"track_total_hits": true, を指定します。
このオプションは全件検索以外においても利用できるため、正確な件数を把握したい場合に役立ちます。
※検索にヒットした件数は、検索結果のhits.total配下のvalueの値から確認できます。

GET {indexName}/_search
{
  "track_total_hits": true, 
  "query": {"match_all": {}}
}

ドキュメントのOR/AND/NOT検索

OR検索をしたい場合は以下のように、query.bool.shouldを利用します。
以下のクエリの例では、
XXXフィールドの値がYYYであるドキュメント又はZZZフィールドの値が存在するドキュメント
を検索します。

GET {indexName}/_search
{
  "track_total_hits": true,
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "XXX": "YYY" # XXXフィールドの値がYYYのドキュメント
          }
        },
        {
          "exists": {
            "field": "ZZZ" # ZZZフィールドが存在する
          }
        }
      ]
    }
  }
}

AND検索では、query.bool.mustを指定します。
以下のクエリの例では、
XXXフィールドの値がYYYである、かつZZZフィールドに値が存在するドキュメント
を検索します。

GET {indexName}/_search
{
  "track_total_hits": true,
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "XXX": "YYY" # XXXフィールドの値がYYYのドキュメント
          }
        },
        {
          "exists": {
            "field": "ZZZ" # ZZZフィールドが存在する
          }
        }
      ]
    }
  }
}

NOT検索では、query.bool.must_notを指定します。
以下の例では、
(XXXフィールドの値がYYYである、又はZZZフィールドに値が存在する)かつ(AAAフィールドに値が存在しない)
に合致するドキュメントを検索します。

GET {indexName}/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "XXX": "YYY"
          }
        },
        {
          "exists": {
            "field": "ZZZ"
          }
        }
      ],
      "must_not": [
        {
          "exists": {
            "field": "AAA"
          }
        }
      ]
    }
  }
}

条件を入れ子にした検索

以下のように、入れ子構造にすることでより複雑な条件の検索を行うことが出来ます。

GET {indexName}/_search
{
  "query": {
    "bool" : {
      "should" : [
         { 
           "bool" : {
             "must": [
               { "match": { "XXX" : "YYY" } },
               { "exists": {"field": "ZZZ" } }
             ]
           } 
         },
         {
           "bool": {
             "must_not" : {
               "exists" : { "field": "AAA" } 
             }
           }
         } 
       ]
    }
  }
}

値の範囲指定

値の範囲指定はrangeで指定します。

gt:Grater than の意で、オプションで下限を指定できます。
→ gt:10は日本語だと、"10より大きい" となります。
gte:Grater than or equal to の意で、オプションで下限を指定できます。
→ gte:10は日本語だと、"10以上" となります。
lt:Less than の意で、オプションとして上限を指定できます。
→ より小さい
lte:Less than or equal to の意で、オプションとして上限を指定できます。
→ 以下

以下のクエリは、ageフィールドの値が10以上20以下のドキュメントを検索します。

GET {indexName}/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

複数インデックスの検索

GET {indexNameA},{indexNameB},{indexNameC}/_search

インデックスの検索では、ワイルドカードも利用できます。

GET index*/_search
{
  "query": {
    "match": {
      ...
    }
  }
}

インデックスの削除

DELETE /{indexNameA},{indexNameB},{indexNameC}

特定の条件に合致したドキュメントの削除

POST /{indexNameA}/_delete_by_query
{
  "query": {
    "match": {
      "user.id": "XXXX"
    }
  }
}

wildcardを利用したドキュメントの検索

GET /{indexName}/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "id": "AAA*"
          }
        },
        {
          "wildcard": {
            "id": "BBB*"
          }
        }
      ]
    }
  }
}

まとめ

目的 クエリ
インデックス確認 GET _cat/indices
全件検索 query.match_all
件数確認オプション "track_total_hits": true
OR検索 query.bool.should
AND検索 query.bool.must
NOT検索 query.bool.must_NOT
値の範囲指定 query.range
特定の条件に合致したドキュメントの削除 POST /{indexNameA}/_delete_by_query
wildcardを利用したドキュメントの検索 query.wildcard

参考

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

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?