mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-05 14:04:46 +00:00
Switch string type for text/keyword
Using "text" type in search fields and "keyword" type in filter fields https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking_50_mapping_changes.html#_literal_string_literal_fields_replaced_by_literal_text_literal_literal_keyword_literal_fields https://www.elastic.co/guide/en/elasticsearch/reference/5.x/text.html https://www.elastic.co/guide/en/elasticsearch/reference/5.x/keyword.html
This commit is contained in:
parent
93cc470d4b
commit
a5e7fbbb0f
3 changed files with 47 additions and 27 deletions
|
|
@ -41,6 +41,9 @@ class ElasticsearchMapping(object):
|
|||
'TimeField': 'date',
|
||||
}
|
||||
|
||||
keyword_type = 'string'
|
||||
text_type = 'string'
|
||||
|
||||
# Contains the configuration required to use the edgengram_analyzer
|
||||
# on a field. It's different in Elasticsearch 2 so it's been put in
|
||||
# an attribute here to make it easier to override in a subclass.
|
||||
|
|
@ -82,6 +85,9 @@ class ElasticsearchMapping(object):
|
|||
mapping = {'type': self.type_map.get(field.get_type(self.model), 'string')}
|
||||
|
||||
if isinstance(field, SearchField):
|
||||
if mapping['type'] == 'string':
|
||||
mapping['type'] = self.text_type
|
||||
|
||||
if field.boost:
|
||||
mapping['boost'] = field.boost
|
||||
|
||||
|
|
@ -91,6 +97,9 @@ class ElasticsearchMapping(object):
|
|||
mapping['include_in_all'] = True
|
||||
|
||||
elif isinstance(field, FilterField):
|
||||
if mapping['type'] == 'string':
|
||||
mapping['type'] = self.keyword_type
|
||||
|
||||
mapping['index'] = 'not_analyzed'
|
||||
mapping['include_in_all'] = False
|
||||
|
||||
|
|
@ -103,9 +112,9 @@ class ElasticsearchMapping(object):
|
|||
def get_mapping(self):
|
||||
# Make field list
|
||||
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', include_in_all=False),
|
||||
'pk': dict(type=self.keyword_type, index='not_analyzed', store='yes', include_in_all=False),
|
||||
'content_type': dict(type=self.keyword_type, index='not_analyzed', include_in_all=False),
|
||||
'_partials': dict(type=self.text_type, include_in_all=False),
|
||||
}
|
||||
fields['_partials'].update(self.edgengram_analyzer_config)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,21 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from .elasticsearch2 import Elasticsearch2SearchBackend
|
||||
from .elasticsearch2 import (
|
||||
Elasticsearch2Mapping, Elasticsearch2SearchBackend, Elasticsearch2SearchQuery)
|
||||
|
||||
|
||||
class Elasticsearch5Mapping(Elasticsearch2Mapping):
|
||||
keyword_type = 'keyword'
|
||||
text_type = 'text'
|
||||
|
||||
|
||||
class Elasticsearch5SearchQuery(Elasticsearch2SearchQuery):
|
||||
mapping_class = Elasticsearch5Mapping
|
||||
|
||||
|
||||
class Elasticsearch5SearchBackend(Elasticsearch2SearchBackend):
|
||||
pass
|
||||
mapping_class = Elasticsearch5Mapping
|
||||
query_class = Elasticsearch5SearchQuery
|
||||
|
||||
|
||||
SearchBackend = Elasticsearch5SearchBackend
|
||||
|
|
|
|||
|
|
@ -753,20 +753,20 @@ class TestElasticsearch5Mapping(TestCase):
|
|||
expected_result = {
|
||||
'searchtests_searchtest': {
|
||||
'properties': {
|
||||
'pk': {'index': 'not_analyzed', 'type': 'string', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard', 'include_in_all': False, 'type': 'string'},
|
||||
'pk': {'index': 'not_analyzed', 'type': 'keyword', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard', 'include_in_all': False, 'type': 'text'},
|
||||
'live_filter': {'index': 'not_analyzed', 'type': 'boolean', 'include_in_all': False},
|
||||
'published_date_filter': {'index': 'not_analyzed', 'type': 'date', 'include_in_all': False},
|
||||
'title': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'content': {'type': 'string', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'string', 'include_in_all': True},
|
||||
'title': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
'content': {'type': 'text', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'text', 'include_in_all': True},
|
||||
'tags': {
|
||||
'type': 'nested',
|
||||
'properties': {
|
||||
'name': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'slug_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'name': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'slug_filter': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
@ -836,32 +836,32 @@ class TestElasticsearch5MappingInheritance(TestCase):
|
|||
'searchtests_searchtest_searchtests_searchtestchild': {
|
||||
'properties': {
|
||||
# New
|
||||
'searchtests_searchtestchild__extra_content': {'type': 'string', 'include_in_all': True},
|
||||
'searchtests_searchtestchild__subtitle': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'searchtests_searchtestchild__extra_content': {'type': 'text', 'include_in_all': True},
|
||||
'searchtests_searchtestchild__subtitle': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'searchtests_searchtestchild__page': {
|
||||
'type': 'nested',
|
||||
'properties': {
|
||||
'title': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'search_description': {'type': 'string', 'include_in_all': True},
|
||||
'title': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'search_description': {'type': 'text', 'include_in_all': True},
|
||||
'live_filter': {'index': 'not_analyzed', 'type': 'boolean', 'include_in_all': False},
|
||||
}
|
||||
},
|
||||
|
||||
# Inherited
|
||||
'pk': {'index': 'not_analyzed', 'type': 'string', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard', 'include_in_all': False, 'type': 'string'},
|
||||
'pk': {'index': 'not_analyzed', 'type': 'keyword', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard', 'include_in_all': False, 'type': 'text'},
|
||||
'live_filter': {'index': 'not_analyzed', 'type': 'boolean', 'include_in_all': False},
|
||||
'published_date_filter': {'index': 'not_analyzed', 'type': 'date', 'include_in_all': False},
|
||||
'title': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'content': {'type': 'string', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'string', 'include_in_all': True},
|
||||
'title': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
'content': {'type': 'text', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'text', 'include_in_all': True},
|
||||
'tags': {
|
||||
'type': 'nested',
|
||||
'properties': {
|
||||
'name': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'slug_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'name': {'type': 'text', 'include_in_all': True, 'analyzer': 'edgengram_analyzer', 'search_analyzer': 'standard'},
|
||||
'slug_filter': {'index': 'not_analyzed', 'type': 'keyword', 'include_in_all': False},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue