我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用elasticsearch.exceptions.RequestError()。
def test_es(): """ Before running other tests, ensure connection to ES is established """ es = Elasticsearch() try: es.indices.create(INDEX) es.indices.delete(INDEX) return True except RequestError: print('Index already exists: skipping tests.') return False except ConnectionError: print('The ElasticSearch backend is not running: skipping tests.') return False except Exception as e: print('An unknown error occured connecting to ElasticSearch: %s' % e) return False
def get_last_doc(self): """Get the most recently modified document from Elasticsearch. This method is used to help define a time window within which documents may be in conflict after a MongoDB rollback. """ try: result = self.elastic.search( index=self.meta_index_name, body={ "query": {"match_all": {}}, "sort": [{"_ts": "desc"}], }, size=1 )["hits"]["hits"] for r in result: r['_source']['_id'] = r['_id'] return r['_source'] except es_exceptions.RequestError: # no documents so ES returns 400 because of undefined _ts mapping return None
def copyToEs(self, p_esTo, p_indexTo, p_force=False): if not self.m_json: print("*** Can not get '%s' object from '%s'" % (self, self.m_index), file=sys.stderr) return try: if p_force: l_response = p_esTo.update(index=p_indexTo, doc_type=self.m_type, id=self.m_id, body={ "doc": self.m_json["_source"], "doc_as_upsert" : True }) else: l_response = p_esTo.create(index = p_indexTo, doc_type=self.m_type, id=self.m_id, body=self.m_json["_source"]) except exceptions.ConflictError as e: print("*** Can not create '%s' in index '%s'" % (self.m_idUtf8, p_indexTo), file=sys.stderr) except exceptions.RequestError as e: print("*** Can't write to unknown index", p_indexTo, file=sys.stderr) sys.exit(1)
def create_indexes(mapping_dir): """ Create all indexes for which a mapping- and settings file is available. It is assumed that mappings in the specified directory follow the following naming convention: "owa_mapping_{SOURCE_NAME}.json". """ click.echo('Creating indexes for ES mappings in %s' % (mapping_dir)) for mapping_file_path in glob('%s/owa_mapping_*.json' % mapping_dir): # Extract the index name from the filename index_name = DEFAULT_INDEX_PREFIX mapping_file = os.path.split(mapping_file_path)[-1].split('.')[0] index_name = '%s_%s' % (DEFAULT_INDEX_PREFIX, '_'.join(mapping_file.rsplit('_')[2:])) click.echo('Creating ES index %s' % index_name) mapping_file = open(mapping_file_path, 'rb') mapping = json.load(mapping_file) mapping_file.close() try: es.indices.create(index=index_name, body=mapping) except RequestError as e: error_msg = click.style('Failed to create index %s due to ES ' 'error: %s' % (index_name, e.error), fg='red') click.echo(error_msg)
def index_fingerprint(sender, instance, **kwargs): """Process facts using engine and convert to fingerprints. :param sender: Class that was saved :param instance: SystemFingerprint that was saved :param kwargs: Other args :returns: None """ # pylint: disable=unused-argument timestamp = time.time() timestamp = int(round(timestamp * 1000)) es_fingerprint = FingerPrintIndex( fact_collection=instance.id, connection_host=instance.connection_host, connection_port=instance.connection_port, connection_uuid=instance.connection_uuid, cpu_count=instance.cpu_count, cpu_core_per_socket=instance.cpu_core_per_socket, cpu_siblings=instance.cpu_siblings, cpu_hyperthreading=instance.cpu_hyperthreading, cpu_socket_count=instance.cpu_socket_count, cpu_core_count=instance.cpu_core_count, system_creation_date=instance.system_creation_date, infrastructure_type=instance.infrastructure_type, os_name=instance.os_name, os_version=instance.os_version, os_release=instance.os_release, virtualized_is_guest=instance.virtualized_is_guest, virtualized_type=instance.virtualized_type, virtualized_num_guests=instance.virtualized_num_guests, virtualized_num_running_guests=instance.virtualized_num_running_guests, timestamp=timestamp) try: es_fingerprint.save() except RequestError as error: logger.error('%s failed to persist fingerprint: %s\n%s', __name__, es_fingerprint, error)