From ca1fa1d93341f21f60d0fdde943c062f5c4b343c Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 20 Jun 2014 17:32:06 +0100 Subject: [PATCH] Index partials together into '_partials' --- wagtail/wagtailsearch/backends/elasticsearch.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailsearch/backends/elasticsearch.py b/wagtail/wagtailsearch/backends/elasticsearch.py index 5e9e6e9846..8fc2950ddf 100644 --- a/wagtail/wagtailsearch/backends/elasticsearch.py +++ b/wagtail/wagtailsearch/backends/elasticsearch.py @@ -71,6 +71,7 @@ class ElasticSearchMapping(object): fields = { 'pk': dict(type='string', index='not_analyzed', store='yes', include_in_all=False), 'content_type': dict(type='string', index='not_analyzed', include_in_all=False), + '_partials': dict(type='string', analyzer='edgengram_analyzer', include_in_all=False), } fields.update(dict( @@ -89,8 +90,18 @@ class ElasticSearchMapping(object): def get_document(self, obj): # Build document doc = dict(pk=str(obj.pk), content_type=self.model.indexed_get_content_type()) + partials = [] for field in self.model.get_search_fields(): - doc[field.get_index_name(self.model)] = field.get_value(obj) + value = field.get_value(obj) + + doc[field.get_index_name(self.model)] = value + + # Check if this field should be added into _partials + if isinstance(field, SearchField) and field.partial_match: + partials.append(value) + + # Add partials to document + doc['_partials'] = partials return doc @@ -102,7 +113,7 @@ class ElasticSearchQuery(object): def __init__(self, model, query_string, fields=None, filters={}): self.model = model self.query_string = query_string - self.fields = fields or ['_all'] + self.fields = fields or ['_all', '_partials'] self.filters = filters def _get_filters(self):