def test_mapping(self, mapping): """Verify that a mapping is correct. This function does so by first creating a new, temporary index in elasticsearch using the mapping. It then takes some recent crash reports that are in elasticsearch and tries to insert them in the temporary index. Any failure in any of those steps will raise an exception. If any is raised, that means the mapping is incorrect in some way (either it doesn't validate against elasticsearch's rules, or is not compatible with the data we currently store). If no exception is raised, the mapping is likely correct. This function is to be used in any place that can change the `storage_mapping` field in any Super Search Field. Methods `create_field` and `update_field` use it, see above. """ temp_index = 'socorro_mapping_test' es_connection = self.get_connection() # Import at runtime to avoid dependency circle. from socorro.external.es.index_creator import IndexCreator index_creator = IndexCreator(self.config) try: index_creator.create_index( temp_index, mapping, ) now = datetimeutil.utc_now() last_week = now - datetime.timedelta(days=7) current_indices = self.generate_list_of_indexes(last_week, now) crashes_sample = es_connection.search( index=current_indices, doc_type=self.config.elasticsearch.elasticsearch_doctype, size=self.config.elasticsearch.mapping_test_crash_number, ) crashes = [x['_source'] for x in crashes_sample['hits']['hits']] for crash in crashes: es_connection.index( index=temp_index, doc_type=self.config.elasticsearch.elasticsearch_doctype, body=crash, ) except elasticsearch.exceptions.ElasticsearchException as e: raise BadArgumentError( 'storage_mapping', msg='Indexing existing data in Elasticsearch failed with the ' 'new mapping. Error is: %s' % str(e), ) finally: try: index_creator.get_index_client().delete(temp_index) except elasticsearch.exceptions.NotFoundError: # If the index does not exist (if the index creation failed # for example), we don't need to do anything. pass
def test_mapping(self, mapping): """Verify that a mapping is correct. This function does so by first creating a new, temporary index in elasticsearch using the mapping. It then takes some recent crash reports that are in elasticsearch and tries to insert them in the temporary index. Any failure in any of those steps will raise an exception. If any is raised, that means the mapping is incorrect in some way (either it doesn't validate against elasticsearch's rules, or is not compatible with the data we currently store). If no exception is raised, the mapping is likely correct. This function is to be used in any place that can change the `storage_mapping` field in any Super Search Field. Methods `create_field` and `update_field` use it, see above. """ temp_index = 'socorro_mapping_test' es_connection = self.get_connection() # Import at runtime to avoid dependency circle. from socorro.external.es.index_creator import IndexCreator index_creator = IndexCreator(self.config) try: index_creator.create_index( temp_index, mapping, ) now = datetimeutil.utc_now() last_week = now - datetime.timedelta(days=7) current_indices = self.generate_list_of_indexes(last_week, now) crashes_sample = es_connection.search( index=current_indices, doc_type=self.config.elasticsearch.elasticsearch_doctype, size=self.config.elasticsearch.mapping_test_crash_number, ) crashes = [x['_source'] for x in crashes_sample['hits']['hits']] for crash in crashes: es_connection.index( index=temp_index, doc_type=self.config.elasticsearch.elasticsearch_doctype, body=crash, ) except elasticsearch.exceptions.ElasticsearchException as e: raise BadArgumentError( 'storage_mapping', msg='Indexing existing data in Elasticsearch failed with the ' 'new mapping. Error is: %s' % str(e), ) finally: try: index_creator.get_index_client().delete(temp_index) except elasticsearch.exceptions.NotFoundError: # If the index does not exist (if the index creation failed # for example), we don't need to do anything. pass
def test_create_index(self): index_creator = IndexCreator(config=self.config) index_creator.create_index( self.config.elasticsearch.elasticsearch_index, {'foo': 'bar'}) assert self.index_client.exists( self.config.elasticsearch.elasticsearch_index)
def test_create_index(self): index_creator = IndexCreator(config=self.config) index_creator.create_index( self.config.elasticsearch.elasticsearch_index, {'foo': 'bar'} ) assert self.index_client.exists( self.config.elasticsearch.elasticsearch_index )
def test_create_index(self): index_creator = IndexCreator(config=self.config) index_creator.create_index( self.config.elasticsearch.elasticsearch_index, {} ) ok_( self.index_client.exists( self.config.elasticsearch.elasticsearch_index ) )
def test_create_index(self): index_creator = IndexCreator(config=self.config) index_creator.create_index( self.config.elasticsearch.elasticsearch_index, {} ) ok_( index_creator.get_index_client().exists( self.config.elasticsearch.elasticsearch_index ) )
def test_create_index(self): index_creator = IndexCreator(config=self.config) index_creator.create_index(self.config.elasticsearch.elasticsearch_index, {"foo": "bar"}) ok_(self.index_client.exists(self.config.elasticsearch.elasticsearch_index))