def index_elastic(index, action="create", config_file=None): """Create index or force i.e. delete if exist then create it. action: str [create or force] config_file: str """ es = get_es_connection() if not config_file: config_file = index file_name = 'schema/%s/index_%s.json' % (index, config_file) # create if not exist if action == "create": try: es.request(method="get", myindex=index, mysuffix="_settings") except: es.request(method="post", myindex=index, jsonnize=False, mydata=_read_schema(file_name)) elif action == "force": # force creation i.e delete and then create try: es.request(method="delete", myindex=index) except: pass es.request(method="post", myindex=index, jsonnize=False, mydata=_read_schema(file_name))
def create_repository(name): """create repository.""" es = get_es_connection() repository = {"type": "fs", "settings": { "location": "/tmp/%s" % name, "compress": True } } es.request(method="put", mysuffix="_snapshot/%s" % (name), mydata=repository)
def elastic_backup(name=None): """Launch a snapshot with generated name directory if not provided. name: str """ es = get_es_connection() import datetime if not name: name = "backup" _check_repository(es, name) snapshot_name = datetime.datetime.now().strftime("%Y%m%d%I%M%S") print es.request(method="put", mysuffix="_snapshot/%s/%s?wait_for_completion=true" % (name, snapshot_name))
def type_elastic(index, type, action=None): """Create type with mapping or merge the mapping. type: str index: str action: str """ es = get_es_connection() # delete the if exist if action == "force": try: es.request(method="delete", myindex=index, mytype=type) except: pass file_name = 'schema/%s/mapping_%s.json' % (index, type) # create if not exist or merge es.request(method="post", myindex=index, mytype=type, mysuffix="_mapping", jsonnize=False, mydata=_read_schema(file_name))
def type_merge(name): """Merge type, experimental only use if you are sure that you can get your data back (i.e reindex). It's to avoid re-indexing when you have a merge conflict. name: str Name of the model you want to merge """ # import dynamically the mod = __import__("model", fromlist=[name]) type_es = getattr(mod, name) es = get_es_connection() tmp_type = "%s2" % type_es.type # copy type in type2 force creation file_name = 'schema/%s/mapping_%s.json' % (type_es.index, type_es.type) es.request(method="post", myindex=type_es.index, mytype=tmp_type, mysuffix="_mapping", jsonnize=False, mydata=_read_schema(file_name)) _copy_type(type_es, type_es.type, tmp_type) type_elastic(type_es.index, type_es.type, "force") _copy_type(type_es, tmp_type, type_es.type) es.request(method="delete", myindex=type_es.index, mytype=tmp_type)
def delete_type(index, type): """delete a type from index.""" es = get_es_connection() es.request(method="delete", myindex=index, mytype=type)