mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Merge pull request #347 from kaedroho/searchchanges/search-fields-refactor-2
Search Changes 6 - Create field mappings in ElasticSearch backend
This commit is contained in:
commit
15a0e93ff7
@ -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(): {
|
||||
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user