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 elasticsearch.helpers import bulk
|
||||||
|
|
||||||
from wagtail.wagtailsearch.backends.base import BaseSearch
|
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
|
from wagtail.wagtailsearch.utils import normalise_query_string
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +19,24 @@ class ElasticSearchMapping(object):
|
|||||||
def get_document_type(self):
|
def get_document_type(self):
|
||||||
return self.model.indexed_get_content_type()
|
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):
|
def get_mapping(self):
|
||||||
# Make field list
|
# Make field list
|
||||||
fields = {
|
fields = {
|
||||||
@ -26,8 +44,9 @@ class ElasticSearchMapping(object):
|
|||||||
'content_type': dict(type='string'),
|
'content_type': dict(type='string'),
|
||||||
}
|
}
|
||||||
|
|
||||||
for field in self.model.get_search_fields():
|
fields.update(dict(
|
||||||
fields[field.get_attname(self.model)] = field.to_dict(self.model)
|
self.get_field_mapping(field) for field in self.model.get_search_fields()
|
||||||
|
))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
self.get_document_type(): {
|
self.get_document_type(): {
|
||||||
|
@ -112,6 +112,7 @@ class Indexed(object):
|
|||||||
|
|
||||||
class BaseField(object):
|
class BaseField(object):
|
||||||
searchable = False
|
searchable = False
|
||||||
|
suffix = ''
|
||||||
|
|
||||||
def __init__(self, field_name, **kwargs):
|
def __init__(self, field_name, **kwargs):
|
||||||
self.field_name = field_name
|
self.field_name = field_name
|
||||||
@ -127,16 +128,8 @@ class BaseField(object):
|
|||||||
except models.fields.FieldDoesNotExist:
|
except models.fields.FieldDoesNotExist:
|
||||||
return self.field_name
|
return self.field_name
|
||||||
|
|
||||||
def to_dict(self, cls):
|
def get_index_name(self, cls):
|
||||||
dic = {
|
return self.get_attname(cls) + self.suffix
|
||||||
'type': 'string'
|
|
||||||
}
|
|
||||||
|
|
||||||
if 'es_extra' in self.kwargs:
|
|
||||||
for key, value in self.kwargs['es_extra'].items():
|
|
||||||
dic[key] = value
|
|
||||||
|
|
||||||
return dic
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s: %s>" % (self.__class__.__name__, self.field_name)
|
return "<%s: %s>" % (self.__class__.__name__, self.field_name)
|
||||||
@ -150,23 +143,6 @@ class SearchField(BaseField):
|
|||||||
self.boost = boost
|
self.boost = boost
|
||||||
self.partial_match = partial_match
|
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):
|
class FilterField(BaseField):
|
||||||
def to_dict(self, cls):
|
suffix = '_filter'
|
||||||
dic = super(FilterField, self).to_dict(cls)
|
|
||||||
|
|
||||||
if 'index' not in dic:
|
|
||||||
dic['index'] = 'not_analyzed'
|
|
||||||
|
|
||||||
return dic
|
|
||||||
|
Loading…
Reference in New Issue
Block a user