Python源码示例:elasticsearch.RequestError()
示例1
def get_es_client(self):
self._es = Elasticsearch(hosts=self.es_hosts)
self._es_version = [int(i) for i in self._es.info()["version"]["number"].split(".")]
if self._es_version < [7]:
raise ValueError("Inventory exporter {} not compatible with ES < 7.0")
# lifecycle
_esilm = IlmClient(self._es)
_esilm.put_lifecycle(ES_LIFECYCLE_POLICY_NAME, ES_LIFECYCLE_POLICY)
# template
self._es.indices.put_template(ES_TEMPLATE_NAME, ES_TEMPLATE)
# create index
for i in range(10):
existing_indices = self._es.indices.get(ES_INDEX_PATTERN).keys()
if not len(existing_indices):
current_index_name = ES_INDEX_PATTERN.replace("*", "000001")
try:
self._es.indices.create(current_index_name, {"aliases": {ES_ALIAS: {"is_write_index": True}}})
except RequestError:
# probably race
pass
else:
break
return ES_ALIAS
示例2
def get_es_client(self):
self._es = Elasticsearch(hosts=self.es_hosts)
self._es_version = [int(i) for i in self._es.info()["version"]["number"].split(".")]
# template
template_body = ES_TEMPLATE
if self._es_version < [7]:
template_body["mappings"] = {"_doc": template_body.pop("mappings")}
self._es.indices.put_template(ES_TEMPLATE_NAME, template_body)
# create index
for i in range(10):
existing_indices = self._es.indices.get(ES_INDEX_PATTERN).keys()
if not len(existing_indices):
next_id = 0
else:
next_id = max(int(index.rsplit("-", 1)[-1]) for index in existing_indices) + 1
index_name = ES_INDEX_PATTERN.replace("*", "{:08d}".format(next_id))
try:
self._es.indices.create(index_name)
except RequestError:
# probably race
pass
else:
# move alias
update_aliases_body = {
"actions": [
{"add": {"index": index_name, "alias": ES_ALIAS}}
]
}
try:
old_indices = self._es.indices.get_alias(ES_ALIAS)
except NotFoundError:
old_indices = []
for old_index in old_indices:
if old_index != index_name:
update_aliases_body["actions"].append(
{"remove": {"index": old_index, "alias": ES_ALIAS}}
)
self._es.indices.update_aliases(update_aliases_body)
return index_name
示例3
def create_index(self):
"""Tell the Elasticsearch client to create the index as configured."""
self.logger.debug("creating index %s", self.index_name)
body = {
"settings": self.settings,
"mappings": self.mappings
}
try:
self.client.indices.create(index=self.index_name, body=body)
except RequestError as e:
if u'resource_already_exists_exception' == e.error:
self.logger.debug("swallowing index exists exception")
else:
# if it wasn't this error, raise it again
raise e
示例4
def put_object(self, obj):
# TODO consider putting into a ES class
self.pr_dbg('put_obj: %s' % self.json_dumps(obj))
"""
Wrapper for es.index, determines metadata needed to index from obj.
If you have a raw object json string you can hard code these:
index is .kibana (as of kibana4);
id can be A-Za-z0-9\- and must be unique;
doc_type is either visualization, dashboard, search
or for settings docs: config, or index-pattern.
"""
if obj['_index'] is None or obj['_index'] == "":
raise Exception("Invalid Object, no index")
if obj['_id'] is None or obj['_id'] == "":
raise Exception("Invalid Object, no _id")
if obj['_type'] is None or obj['_type'] == "":
raise Exception("Invalid Object, no _type")
if obj['_source'] is None or obj['_source'] == "":
raise Exception("Invalid Object, no _source")
self.connect_es()
self.es.indices.create(index=obj['_index'], ignore=400, timeout="2m")
try:
resp = self.es.index(index=obj['_index'],
id=obj['_id'],
doc_type=obj['_type'],
body=obj['_source'], timeout="2m")
except RequestError as e:
self.pr_err('RequestError: %s, info: %s' % (e.error, e.info))
raise
return resp
示例5
def _run_query(self, es_query, result_type):
try:
results = config.es.search(
index='data_explorer',
doc_type='flywheel',
body=es_query
)
except RequestError:
self.abort(400, 'Unable to parse filters - invalid format.')
return self._process_results(results, result_type)