def test_index_manager_perform_create_index(self): """ Perform all side-effects through the ES client and return the index name (incl. timestamp) """ # Create an indexable from scratch that mimicks the expected shape of the dynamic # import in es_index class IndexableClass: """Indexable stub""" index_name = "richie_courses" mapping = { "properties": { "code": { "type": "keyword" }, "name": { "type": "text" } } } # pylint: disable=no-self-use def get_es_documents(self, index, action="index"): """Stub method""" for i in range(0, 10): yield { "_id": i, "_index": index, "_op_type": action, "code": f"course-{i:d}", "name": f"Course Number {i:d}", } indexable = IndexableClass() # Set a fake time to check the name of the index now = datetime(2016, 5, 4, 3, 12, 33, 123456, tzinfo=pytz.utc) # Make sure our index is empty before we call the function self.assertEqual(ES_INDICES_CLIENT.get_alias("*"), {}) mock_logger = mock.Mock(spec=["info"]) with mock.patch.object(timezone, "now", return_value=now): new_index = perform_create_index(indexable, mock_logger) ES_INDICES_CLIENT.refresh() self.assertEqual(new_index, "richie_courses_2016-05-04-03h12m33.123456s") self.assertEqual(ES_CLIENT.count()["count"], 10) self.assertEqual( ES_INDICES_CLIENT.get_mapping(), { "richie_courses_2016-05-04-03h12m33.123456s": { "mappings": { "properties": { "code": { "type": "keyword" }, "name": { "type": "text" }, } } } }, ) mock_logger.info.assert_called()
def test_partial_mappings_multilingual_text(self): """ Make sure our multilingual_text dynamic mapping results in the proper mappings being generated when objects with the expected format are indexed """ index_name = "stub_index" mapping = {"dynamic_templates": MULTILINGUAL_TEXT} # Create the index and set a mapping that includes the pattern we want to test ES_INDICES_CLIENT.create(index=index_name) ES_INDICES_CLIENT.put_mapping(index=index_name, body=mapping) # The index needs to be closed before we set an analyzer ES_INDICES_CLIENT.close(index=index_name) ES_INDICES_CLIENT.put_settings(body=ANALYSIS_SETTINGS, index=index_name) ES_INDICES_CLIENT.open(index=index_name) # The stub mapping only contains our dynamic template mapping = ES_INDICES_CLIENT.get_mapping(index=index_name) self.assertEqual( mapping[index_name]["mappings"], {"dynamic_templates": MULTILINGUAL_TEXT}, ) # Index an object that should trigger a match for our dynamic template ES_CLIENT.index( index=index_name, doc_type="_doc", body={"title": { "fr": "Un titre en français à titre d'exemple" }}, ) # The stub mapping has been extended with a matching property for 'fr' mapping = ES_INDICES_CLIENT.get_mapping(index=index_name) self.assertEqual( mapping[index_name]["mappings"], { "dynamic_templates": MULTILINGUAL_TEXT, "properties": { "title": { "properties": { "fr": { "type": "text", "fields": { "language": { "type": "text", "analyzer": "french" }, "trigram": { "type": "text", "analyzer": "french_trigram", "search_analyzer": "french", }, }, } } } }, }, ) # Index an object that should trigger a different match for our dynamic template ES_CLIENT.index( index=index_name, doc_type="_doc", body={"title": { "en": "An English title as an example" }}, ) # The sub mapping has been extended with a matching property for 'en' mapping = ES_INDICES_CLIENT.get_mapping(index=index_name) self.assertEqual( mapping[index_name]["mappings"], { "dynamic_templates": MULTILINGUAL_TEXT, "properties": { "title": { "properties": { "en": { "type": "text", "fields": { "language": { "type": "text", "analyzer": "english" }, "trigram": { "type": "text", "analyzer": "english_trigram", "search_analyzer": "english", }, }, }, "fr": { "type": "text", "fields": { "language": { "type": "text", "analyzer": "french" }, "trigram": { "type": "text", "analyzer": "french_trigram", "search_analyzer": "french", }, }, }, } } }, }, )