Merge pull request #349 from kaedroho/searchchanges/elasticsearchtypes

Search Changes 8 - Index fields with correct type in ElasticSearch
pull/435/head
Matt Westcott 2014-07-07 19:48:43 +01:00
commit bf9c21df71
2 zmienionych plików z 49 dodań i 9 usunięć

Wyświetl plik

@ -13,6 +13,32 @@ from wagtail.wagtailsearch.utils import normalise_query_string
class ElasticSearchMapping(object): class ElasticSearchMapping(object):
TYPE_MAP = {
'AutoField': 'integer',
'BinaryField': 'binary',
'BooleanField': 'boolean',
'CharField': 'string',
'CommaSeparatedIntegerField': 'string',
'DateField': 'date',
'DateTimeField': 'date',
'DecimalField': 'double',
'FileField': 'string',
'FilePathField': 'string',
'FloatField': 'double',
'IntegerField': 'integer',
'BigIntegerField': 'long',
'IPAddressField': 'string',
'GenericIPAddressField': 'string',
'NullBooleanField': 'boolean',
'OneToOneField': 'integer',
'PositiveIntegerField': 'integer',
'PositiveSmallIntegerField': 'integer',
'SlugField': 'string',
'SmallIntegerField': 'integer',
'TextField': 'string',
'TimeField': 'date',
}
def __init__(self, model): def __init__(self, model):
self.model = model self.model = model
@ -20,7 +46,7 @@ class ElasticSearchMapping(object):
return self.model.indexed_get_content_type() return self.model.indexed_get_content_type()
def get_field_mapping(self, field): def get_field_mapping(self, field):
mapping = {'type': 'string'} mapping = {'type': self.TYPE_MAP.get(field.get_type(self.model), 'string')}
if isinstance(field, SearchField): if isinstance(field, SearchField):
if field.boost: if field.boost:
@ -60,14 +86,8 @@ class ElasticSearchMapping(object):
def get_document(self, obj): def get_document(self, obj):
# Build document # Build document
doc = dict(pk=str(obj.pk), content_type=self.model.indexed_get_content_type()) doc = dict(pk=str(obj.pk), content_type=self.model.indexed_get_content_type())
for field in [field.get_attname(self.model) for field in self.model.get_search_fields()]: for field in self.model.get_search_fields():
if hasattr(obj, field): doc[field.get_index_name(self.model)] = field.get_value(obj)
doc[field] = getattr(obj, field)
# Check if this field is callable
if hasattr(doc[field], '__call__'):
# Call it
doc[field] = doc[field]()
return doc return doc

Wyświetl plik

@ -138,6 +138,26 @@ class BaseField(object):
def get_index_name(self, cls): def get_index_name(self, cls):
return self.get_attname(cls) + self.suffix return self.get_attname(cls) + self.suffix
def get_type(self, cls):
if 'type' in self.kwargs:
return self.kwargs['type']
try:
field = self.get_field(cls)
return field.get_internal_type()
except models.fields.FieldDoesNotExist:
return 'CharField'
def get_value(self, obj):
try:
field = self.get_field(obj.__class__)
return field._get_val_from_obj(obj)
except models.fields.FieldDoesNotExist:
value = getattr(obj, self.field_name, None)
if hasattr(value, '__call__'):
value = value()
return value
def __repr__(self): def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.field_name) return '<%s: %s>' % (self.__class__.__name__, self.field_name)