What is the Elasticsearch-py equivalent to alias actions?

人盡茶涼 提交于 2020-05-25 13:27:28

问题


I am trying to implement multiples indices approach using elasticsearch-dsl. There are basically two steps:

1. Create aliases:

PUT /tweets_1/_alias/tweets_search 
PUT /tweets_1/_alias/tweets_index 

2. Change alias when necessary:

POST /_aliases
{
  "actions": [
    { "add":    { "index": "tweets_2", "alias": "tweets_search" }}, 
    { "remove": { "index": "tweets_1", "alias": "tweets_index"  }}, 
    { "add":    { "index": "tweets_2", "alias": "tweets_index"  }}  
  ]
}

I could only implement the step 1 using elasticsearch-py (not the dsl):

from elasticsearch.client import IndicesClient
IndicesClient(client).("tweets_1", "tweets_search")
IndicesClient(client).("tweets_1", "tweets_index")

I have no clue how to do that for step 2. So, what would be the equivalent in elasticsearch-dsl (or at least in elasticsearch-py)?


回答1:


To implement that you need to use elasticsearch-py:

from elasticsearch import Elasticsearch
es = Elasticsearch()

# use es.indices instead of instantiating IndicesClient
es.indices.put_alias(index='tweets_1', name='tweets_search')
es.indices.put_alias(index='tweets_1', name='tweets_index')

es.indices.update_aliases({
  "actions": [
    { "add":    { "index": "tweets_2", "alias": "tweets_search" }}, 
    { "remove": { "index": "tweets_1", "alias": "tweets_index"  }}, 
    { "add":    { "index": "tweets_2", "alias": "tweets_index"  }}  
  ]
})



回答2:


index_list_for_realias = [...]
aliases_list_to_realias = [...]

for i in index_list_for_realias:
    print(i)
    for j in aliases_list_to_realias:
        es.indices.put_alias(index=i, name="logstash5-uni-" + j, body={
                "filter": {
                    "term": {
                        "uni": j
                    }
                }
            }
        )


来源:https://stackoverflow.com/questions/42719265/what-is-the-elasticsearch-py-equivalent-to-alias-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!