弱いエンジニアの備忘録

自分的に気になった技術に関するメモや備忘録です。Elasticsearchに関する記事が多くなりそうです。

Elasticsearch の search template 一覧を取得する

Elsaticsearchでは、Search Templateという機能があります。
クエリの共通部分を事前に登録しておくことで、検索時のクエリ記述量を減らすことができるものです。
www.elastic.co

作成した Search Templateの一覧を取得するための API がなかったので、取得方法をメモ。

Search Templateの登録

POST _scripts/template_for_description_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "desctiption": "{{query_string}}"
        }
      }
    }
  }
}
POST _scripts/template_for_title_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "title": "{{query_string}}"
        }
      }
    }
  }
}

取得

スクリプトが Cluster State に保存されるのがポイントです。

GET _cluster/state?filter_path=metadata.stored_scripts
{
  "metadata" : {
    "stored_scripts" : {
      "template_for_description_search" : {
        "lang" : "mustache",
        "source" : """{"query":{"match":{"desctiption":"{{query_string}}"}}}""",
        "options" : {
          "content_type" : "application/json; charset=UTF-8"
        }
      },
      "template_for_title_search" : {
        "lang" : "mustache",
        "source" : """{"query":{"match":{"title":"{{query_string}}"}}}""",
        "options" : {
          "content_type" : "application/json; charset=UTF-8"
        }
      }
    }
  }
}

以上