本文整理汇总了Python中elasticsearch.client.IndicesClient.update_aliases方法的典型用法代码示例。如果您正苦于以下问题:Python IndicesClient.update_aliases方法的具体用法?Python IndicesClient.update_aliases怎么用?Python IndicesClient.update_aliases使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch.client.IndicesClient
的用法示例。
在下文中一共展示了IndicesClient.update_aliases方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IndicesManager
# 需要导入模块: from elasticsearch.client import IndicesClient [as 别名]
# 或者: from elasticsearch.client.IndicesClient import update_aliases [as 别名]
class IndicesManager(object):
def __init__(self, options=None):
self.options = options or {}
self.es = get_elasticsearch(self.options)
self.esc = IndicesClient(self.es)
self.conf_dir = sys.path[0]
def __create__(self, name, config=None, type=None):
result = None
try:
if not config:
file_name = "{}/config/{}_index.json".format(
self.conf_dir, type)
with open(file_name) as fp:
config = fp.read()
# create the index with version number
result = self.esc.create(index=name, body=config)
except es_exceptions.TransportError:
print("unable to connect to Elasticsearch")
return result
def create(self, doc_type):
alias_name = 'frisc_{}'.format(doc_type)
index_name = '{}_v1'.format(alias_name)
try:
if self.esc.exists_alias(alias_name):
print('Index {} already existst, updating'.format(alias_name))
self.update(doc_type)
return
self.__create__(index_name, type=doc_type)
# set an alias to the index
self.esc.put_alias(index=index_name, name=alias_name)
except es_exceptions.TransportError:
print("unable to connect to Elasticsearch")
def update(self, doc_type):
alias_name = 'frisc_{}'.format(doc_type)
index_name = '{}_v1'.format(alias_name),
try:
if not self.esc.exists_alias(alias_name):
self.create(doc_type)
return
version_number = 0
old_index_name = ''
old_indexes = self.esc.get_alias(name=alias_name)
for index in old_indexes.keys():
match = re.search('^({})_v(\d+)$'.format(alias_name), index)
if match:
version = int(match.group(2))
if version > version_number:
version_number = version
old_index_name = match.group(0)
version_number += 1
index_name = '{}_v{}'.format(alias_name, version_number)
if self.esc.exists(index_name):
# raise soemthing
raise
self.__create__(index_name, type=doc_type)
reindex(self.es, old_index_name, index_name)
self.esc.update_aliases(
body={'actions': [
{'remove': {'alias': alias_name, 'index': old_index_name}},
{'add': {'alias': alias_name, 'index': index_name}}
]}
)
except es_exceptions.TransportError:
print("unable to connect to Elasticsearch")
示例2: ESIndexManager
# 需要导入模块: from elasticsearch.client import IndicesClient [as 别名]
# 或者: from elasticsearch.client.IndicesClient import update_aliases [as 别名]
class ESIndexManager(object):
def __init__(self, es_config=None):
if not es_config:
es_config = SMConfig.get_conf()['elasticsearch']
self._es = init_es_conn(es_config)
self._ind_client = IndicesClient(self._es)
def internal_index_name(self, alias):
yin, yang = '{}-yin'.format(alias), '{}-yang'.format(alias)
assert not (self.exists_index(yin) and self.exists_index(yang)), \
'Only one of {} and {} should exist'.format(yin, yang)
if self.exists_index(yin):
return yin
elif self.exists_index(yang):
return yang
else:
return yin
def create_index(self, index):
dynamic_templates = [{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword",
"normalizer": "default"}}
}]
body = {
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0,
"max_result_window": 2147483647,
"analysis": {
"normalizer": {
"default": {
"type": "custom",
"filter": ["lowercase", "asciifolding"]
}
}
}}},
"mappings": {
"dataset": {
"dynamic_templates": dynamic_templates,
"properties": {
"ds_id": {"type": "keyword"}
}
},
"annotation": {
"dynamic_templates": dynamic_templates,
"properties": {
"ds_id": {"type": "keyword"},
"chaos": {"type": "float"},
"image_corr": {"type": "float"},
"pattern_match": {"type": "float"},
"total_iso_ints": {"type": "float"},
"min_iso_ints": {"type": "float"},
"max_iso_ints": {"type": "float"},
"msm": {"type": "float"},
"fdr": {"type": "float"}}}}}
if not self._ind_client.exists(index):
out = self._ind_client.create(index=index, body=body)
logger.info('Index {} created\n{}'.format(index, out))
else:
logger.info('Index {} already exists'.format(index))
def delete_index(self, index):
if self._ind_client.exists(index):
out = self._ind_client.delete(index)
logger.info('Index {} deleted: {}'.format(index, out))
def exists_index(self, index):
return self._ind_client.exists(index)
def another_index_name(self, index):
assert index.endswith('yin') or index.endswith('yang')
if index.endswith('yin'):
return index.replace('yin', 'yang')
else:
return index.replace('yang', 'yin')
def remap_alias(self, new_index, alias='sm'):
old_index = self.another_index_name(new_index)
logger.info('Remapping {} alias: {} -> {}'.format(alias, old_index, new_index))
self._ind_client.update_aliases({
"actions": [{"add": {"index": new_index, "alias": alias}}]
})
if self._ind_client.exists_alias(old_index, alias):
self._ind_client.update_aliases({
"actions": [{"remove": {"index": old_index, "alias": alias}}]
})
out = self._ind_client.delete(index=old_index)
logger.info('Index {} deleted: {}'.format(old_index, out))