Skip to content

Commit 87257e6

Browse files
author
Rob Hudson
committed
Moved to open-ended kwargs for fields for future proofing
1 parent 614cc09 commit 87257e6

File tree

1 file changed

+12
-46
lines changed

1 file changed

+12
-46
lines changed

elasticutils/fields.py

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
class SearchField(object):
1616

1717
field_type = None
18-
19-
attrs = ('boost', 'include_in_all', 'index', 'null_value', 'store')
18+
attrs = []
2019

2120
# Used to maintain the order of fields as defined in the class.
2221
_creation_order = 0
2322

2423
def __init__(self, *args, **kwargs):
25-
self.index_fieldname = kwargs.pop('index_fieldname', None)
26-
self.is_multivalued = kwargs.pop('is_multivalued', None)
24+
# These are special.
25+
for attr in ('index_fieldname', 'is_multivalue'):
26+
setattr(self, attr, kwargs.pop(attr, None))
27+
28+
# Set all kwargs on self for later access.
29+
for attr in kwargs.keys():
30+
self.attrs.append(attr)
31+
setattr(self, attr, kwargs.pop(attr, None))
2732

2833
# Store this fields order.
2934
self._creation_order = SearchField._creation_order
@@ -64,16 +69,6 @@ def get_definition(self):
6469

6570
class StringField(SearchField):
6671
field_type = 'string'
67-
attrs = SearchField.attrs + (
68-
'analyzer', 'ignore_above',
69-
'index_analyzer', 'index_options', 'omit_norms',
70-
'position_offset_gap', 'search_analyzer', 'term_vector')
71-
72-
def __init__(self, *args, **kwargs):
73-
for attr in self.attrs:
74-
setattr(self, attr, kwargs.pop(attr, None))
75-
76-
super(StringField, self).__init__(*args, **kwargs)
7772

7873
def prepare(self, value):
7974
return self.convert(super(StringField, self).prepare(value))
@@ -85,18 +80,12 @@ def convert(self, value):
8580
return unicode(value)
8681

8782

88-
class NumberFieldBase(SearchField):
89-
attrs = SearchField.attrs + ('ignore_malformed', 'precision_step')
90-
91-
92-
class IntegerField(NumberFieldBase):
83+
class IntegerField(SearchField):
9384
field_type = 'integer'
9485

9586
def __init__(self, type='integer', *args, **kwargs):
9687
if type in ('byte', 'short', 'integer', 'long'):
9788
self.field_type = type
98-
for attr in self.attrs:
99-
setattr(self, attr, kwargs.pop(attr, None))
10089
super(IntegerField, self).__init__(*args, **kwargs)
10190

10291
def prepare(self, value):
@@ -109,14 +98,12 @@ def convert(self, value):
10998
return int(value)
11099

111100

112-
class FloatField(NumberFieldBase):
101+
class FloatField(SearchField):
113102
field_type = 'float'
114103

115104
def __init__(self, type='float', *args, **kwargs):
116105
if type in ('float', 'double'):
117106
self.field_type = type
118-
for attr in self.attrs:
119-
setattr(self, attr, kwargs.pop(attr, None))
120107
super(FloatField, self).__init__(*args, **kwargs)
121108

122109
def prepare(self, value):
@@ -131,11 +118,6 @@ def convert(self, value):
131118

132119
class DecimalField(StringField):
133120

134-
def __init__(self, *args, **kwargs):
135-
for attr in self.attrs:
136-
setattr(self, attr, kwargs.pop(attr, None))
137-
super(DecimalField, self).__init__(*args, **kwargs)
138-
139121
def prepare(self, value):
140122
if value is None:
141123
return None
@@ -152,11 +134,6 @@ def convert(self, value):
152134
class BooleanField(SearchField):
153135
field_type = 'boolean'
154136

155-
def __init__(self, *args, **kwargs):
156-
for attr in self.attrs:
157-
setattr(self, attr, kwargs.pop(attr, None))
158-
super(BooleanField, self).__init__(*args, **kwargs)
159-
160137
def prepare(self, value):
161138
return self.convert(super(BooleanField, self).prepare(value))
162139

@@ -167,19 +144,9 @@ def convert(self, value):
167144
return bool(value)
168145

169146

170-
class DateFieldBase(SearchField):
171-
attrs = SearchField.attrs + ('format', 'ignore_malformed',
172-
'precision_step')
173-
174-
175-
class DateField(DateFieldBase):
147+
class DateField(SearchField):
176148
field_type = 'date'
177149

178-
def __init__(self, *args, **kwargs):
179-
for attr in self.attrs:
180-
setattr(self, attr, kwargs.pop(attr, None))
181-
super(DateField, self).__init__(*args, **kwargs)
182-
183150
def prepare(self, value):
184151
if isinstance(value, (datetime.date, datetime.datetime)):
185152
return value.isoformat()
@@ -231,7 +198,6 @@ def convert(self, value):
231198

232199
class BinaryField(SearchField):
233200
field_type = 'binary'
234-
attrs = ()
235201

236202
def prepare(self, value):
237203
if value is None:

0 commit comments

Comments
 (0)