elasticsearch.pyのexpand_wildcards指定について
背景
ES投入時にインデックスを削除する処理をしているが、そこで使っているメソッドにexpand_wildcards
という引数が用意されていた
これがデフォルトでopen
になっているらしく、このままではcloseしたインデックスは削除できないのでは?という疑問が生じた
例えば以下のような処理を想定している(以下のコードは動作確認していない)
# 準備 from elasticsearch import Elasticsearch es = Elasticsearch() # 以下の部分が es_client.indices.delete(index="target-index") # 本当はあるべきではないか? es_client.indices.delete(index="target-index", expand_wildcards="all")
ドキュメントにあたる
es-insertのインデックス削除に使っているのは上述の通りElasticsearch
クラスだが、このクラスのdelete
メソッドではexpand_wildcards
という引数は存在してなさそう
https://elasticsearch-py.readthedocs.io/en/7.x/api.html#elasticsearch.Elasticsearch.delete
一方IndicesClient
のdelete
にはexpand_wildcards
という引数が存在している
https://elasticsearch-py.readthedocs.io/en/7.x/api.html#elasticsearch.Elasticsearch.delete
この違いは一体何なのだ
そもそもElasticsearchとIndicesClientどちらのクラスを使うべきか?
Elasticsearch
クラスに関する記述を読んでみると、以下のようなことが書いてある(リンク)
The instance has attributes cat, cluster, indices, ingest, nodes, snapshot and tasks that provide access to instances of CatClient, ClusterClient, IndicesClient, IngestClient, NodesClient, SnapshotClient and TasksClient respectively. This is the preferred (and only supported) way to get access to those classes and their methods.
つまるところ、Elasticsearch
クラスのindices
というattributeはIndicesClient
にアクセスするためのものらしく、そちらを使ってほしいとのこと
ならなぜdelete
メソッドに使える引数が異なるのか引っかかるところだが、一旦その気持ちは抑えて、Elasticsearchクラスをそのまま使い次に進もうと思う
expand_wildcardsとはなにか
とはいえ結局IndicesClient
は使っているので、問題のこのクラス(メソッド)について見てみる
IndicesClient
クラスのdelete
メソッドに用意されているexpand_wildcards
の説明をみると以下のようなことが記載されている
Whether wildcard expressions should get expanded to open or closed indices (default: open) Valid choices: open, closed, hidden, none, all Default: open
Whether wildcard expressions should get expanded to open or closed indices
とあることから、あくまでこの引数は削除対象のインデックス名を指定する際にワイルドカードを用いた場合につかうものであるようだ
当初の疑問に対して答えると
- index引数にhoge-index
のようなインデックス名を指定している場合、そのインデックスがopenでもcloseでも削除できる
- index引数にhoge-*
のようなワイルドカードを用いた指定をしている場合、そのパターンにマッチするインデックスであってもcloseされているものは削除の対象にならない
ということになる
確証のない話
ElasticsearchにはResolve APIなるものがあるらしく、そちらが元なのか?とも感じている https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-resolve-index-api.html