diff --git a/wagtail/wagtailsearch/backends/elasticsearch.py b/wagtail/wagtailsearch/backends/elasticsearch.py index 28127fe831..88542f6248 100644 --- a/wagtail/wagtailsearch/backends/elasticsearch.py +++ b/wagtail/wagtailsearch/backends/elasticsearch.py @@ -8,7 +8,7 @@ from elasticsearch import Elasticsearch, NotFoundError, RequestError from elasticsearch.helpers import bulk from wagtail.wagtailsearch.backends.base import BaseSearch -from wagtail.wagtailsearch.indexed import Indexed +from wagtail.wagtailsearch.indexed import Indexed, SearchField, FilterField from wagtail.wagtailsearch.utils import normalise_query_string @@ -19,6 +19,24 @@ class ElasticSearchMapping(object): def get_document_type(self): return self.model.indexed_get_content_type() + def get_field_mapping(self, field): + mapping = {'type': 'string'} + + if isinstance(field, SearchField): + if field.boost: + mapping['boost'] = field.boost + + if field.partial_match: + mapping['analyzer'] = 'edgengram_analyzer' + elif isinstance(field, FilterField): + mapping['index'] = 'not_analyzed' + + if 'es_extra' in field.kwargs: + for key, value in field.kwargs['es_extra'].items(): + mapping[key] = value + + return field.get_index_name(self.model), mapping + def get_mapping(self): # Make field list fields = { @@ -26,8 +44,9 @@ class ElasticSearchMapping(object): 'content_type': dict(type='string'), } - for field in self.model.get_search_fields(): - fields[field.get_attname(self.model)] = field.to_dict(self.model) + fields.update(dict( + self.get_field_mapping(field) for field in self.model.get_search_fields() + )) return { self.get_document_type(): { diff --git a/wagtail/wagtailsearch/indexed.py b/wagtail/wagtailsearch/indexed.py index b4e67f4c1c..347e41c0cc 100644 --- a/wagtail/wagtailsearch/indexed.py +++ b/wagtail/wagtailsearch/indexed.py @@ -112,6 +112,7 @@ class Indexed(object): class BaseField(object): searchable = False + suffix = '' def __init__(self, field_name, **kwargs): self.field_name = field_name @@ -127,16 +128,8 @@ class BaseField(object): except models.fields.FieldDoesNotExist: return self.field_name - def to_dict(self, cls): - dic = { - 'type': 'string' - } - - if 'es_extra' in self.kwargs: - for key, value in self.kwargs['es_extra'].items(): - dic[key] = value - - return dic + def get_index_name(self, cls): + return self.get_attname(cls) + self.suffix def __repr__(self): return "<%s: %s>" % (self.__class__.__name__, self.field_name) @@ -150,23 +143,6 @@ class SearchField(BaseField): self.boost = boost self.partial_match = partial_match - def to_dict(self, cls): - dic = super(SearchField, self).to_dict(cls) - - if self.boost and 'boost' not in dic: - dic['boost'] = self.boost - - if self.partial_match and 'analyzer' not in dic: - dic['analyzer'] = 'edgengram_analyzer' - - return dic - class FilterField(BaseField): - def to_dict(self, cls): - dic = super(FilterField, self).to_dict(cls) - - if 'index' not in dic: - dic['index'] = 'not_analyzed' - - return dic + suffix = '_filter'