From 2ea14994ffb8ac4298168538d1d9f6790b98dadd Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 29 Jul 2016 11:16:26 +0100 Subject: [PATCH] Fixed bug causing nested objects to be mapped with parents mapping class (#2572) Nested objects that are defined using RelatedFields were being mapped into Elasticsearch using the parent model's mapping. For example, if we index a page with tags, the tags were being mapped with the Page mapping instead of the Tag mapping. This doesn't make any difference for Elasticsearch 1, but Elasticsearch 2 needs to prefix fields if there are defined on a child model and it was doing this across the related fields as well. This fix is covered by the tests coming in the Elasticsearch 2 backend. --- wagtail/wagtailsearch/backends/elasticsearch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailsearch/backends/elasticsearch.py b/wagtail/wagtailsearch/backends/elasticsearch.py index 9b7a530d53..5381ec4ce9 100644 --- a/wagtail/wagtailsearch/backends/elasticsearch.py +++ b/wagtail/wagtailsearch/backends/elasticsearch.py @@ -59,9 +59,11 @@ class ElasticsearchMapping(object): def get_field_mapping(self, field): if isinstance(field, RelatedFields): mapping = {'type': 'nested', 'properties': {}} + nested_model = field.get_field(self.model).related_model + nested_mapping = type(self)(nested_model) for sub_field in field.fields: - sub_field_name, sub_field_mapping = self.get_field_mapping(sub_field) + sub_field_name, sub_field_mapping = nested_mapping.get_field_mapping(sub_field) mapping['properties'][sub_field_name] = sub_field_mapping return self.get_field_column_name(field), mapping