Skip to content
This repository was archived by the owner on Aug 2, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions daybed/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from elasticsearch.exceptions import RequestError, ElasticsearchException

from daybed import logger
from daybed.schemas import registry


class SearchError(Exception):
Expand Down Expand Up @@ -178,9 +179,11 @@ def _record_as_mapping(self, definition, record):
field_types[field_name] = field_type

mapping = record.copy()
for key, value in mapping.items():
for key, value in record.items():
field_type = field_types.get(key)
if field_type in ('line', 'polygon'):
if not registry.indexable(field_type):
del mapping[key]
elif field_type in ('line', 'polygon'):
geojson = {
'line': 'Linestring',
'polygon': 'Polygon'
Expand All @@ -189,8 +192,8 @@ def _record_as_mapping(self, definition, record):
'type': geojson[field_type],
'coordinates': value
}
if field_type == 'point':
elif field_type == 'point':
mapping[key] = {'lon': value[0], 'lat': value[1]}
if field_type == 'list':
elif field_type == 'list':
mapping[key] = json.dumps(value)
return mapping
8 changes: 8 additions & 0 deletions daybed/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def definition(self, typename, **options):
raise UnknownFieldTypeError('Type "%s" is unknown' % typename)
return nodetype.definition(**options)

def indexable(self, typename):
try:
nodetype = self._registry[typename]
except KeyError:
return True
return nodetype.indexable

@property
def names(self):
return self._registry.keys()
Expand All @@ -78,6 +85,7 @@ class TypeField(object):
required = True
default_value = null
hint = u''
indexable = True

@classmethod
def definition(cls, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions daybed/schemas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class TextField(TypeField):
hint = _('A text')


@registry.add('blob')
class BlobField(TypeField):
node = String
hint = _('A binary blob that will be stored as-is')
indexable = False


@registry.add('annotation')
class AnnotationField(TypeField):
required = False
Expand Down
5 changes: 5 additions & 0 deletions daybed/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def test_existing_records_unindexed_on_model_put(self, delete_mock):
{'name': 'u', 'type': 'polygon'},
{'name': 'v', 'type': 'anyof', 'model': 'simple'},
{'name': 'w', 'type': 'oneof', 'model': 'simple'},
{'name': 'x', 'type': 'blob'},
]
}
}
Expand Down Expand Up @@ -342,6 +343,7 @@ def setUp(self):
'u': [[[0.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]],
'v': ["simple-rec"],
'w': 'simple-rec',
'x': 'binary-blob',
}

self.mapping = None
Expand Down Expand Up @@ -379,6 +381,9 @@ def test_line_and_polygon_are_converted_to_geojson(self):
{"type": "Polygon",
"coordinates": self.record['u']})

def test_blob_is_not_indexed(self):
self.assertNotIn('x', self.mapping)


class SpatialSearchTest(BaseWebTest):

Expand Down