Skip to content

Commit d54b82b

Browse files
committed
7.x compatibility
TODO: concurrency control
1 parent e3d4336 commit d54b82b

20 files changed

+270
-468
lines changed

elasticsearch_dsl/document.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fnmatch import fnmatch
77

88
from elasticsearch.exceptions import NotFoundError, RequestError
9-
from six import iteritems, add_metaclass, string_types
9+
from six import iteritems, add_metaclass
1010

1111
from .field import Field
1212
from .mapping import Mapping
@@ -68,11 +68,8 @@ class DocumentOptions(object):
6868
def __init__(self, name, bases, attrs):
6969
meta = attrs.pop('Meta', None)
7070

71-
# get doc_type name, if not defined use 'doc'
72-
doc_type = getattr(meta, 'doc_type', 'doc')
73-
7471
# create the mapping instance
75-
self.mapping = getattr(meta, 'mapping', Mapping(doc_type))
72+
self.mapping = getattr(meta, 'mapping', Mapping())
7673

7774
# register all declared fields into the mapping
7875
for name, value in list(iteritems(attrs)):
@@ -114,8 +111,7 @@ class Document(ObjectBase):
114111
"""
115112
@classmethod
116113
def _matches(cls, hit):
117-
return fnmatch(hit.get('_index', ''), cls._index._name) \
118-
and cls._doc_type.name == hit.get('_type')
114+
return fnmatch(hit.get('_index', ''), cls._index._name)
119115

120116
@classmethod
121117
def _get_using(cls, using=None):
@@ -154,7 +150,7 @@ def __repr__(self):
154150
return '{}({})'.format(
155151
self.__class__.__name__,
156152
', '.join('{}={!r}'.format(key, getattr(self.meta, key)) for key in
157-
('index', 'doc_type', 'id') if key in self.meta)
153+
('index', 'id') if key in self.meta)
158154
)
159155

160156
@classmethod
@@ -185,7 +181,6 @@ def get(cls, id, using=None, index=None, **kwargs):
185181
es = cls._get_connection(using)
186182
doc = es.get(
187183
index=cls._default_index(index),
188-
doc_type=cls._doc_type.name,
189184
id=id,
190185
**kwargs
191186
)
@@ -225,7 +220,6 @@ def mget(cls, docs, using=None, index=None, raise_on_error=True,
225220
results = es.mget(
226221
body,
227222
index=cls._default_index(index),
228-
doc_type=cls._doc_type.name,
229223
**kwargs
230224
)
231225

@@ -283,7 +277,6 @@ def delete(self, using=None, index=None, **kwargs):
283277
doc_meta.update(kwargs)
284278
es.delete(
285279
index=self._get_index(index),
286-
doc_type=self._doc_type.name,
287280
**doc_meta
288281
)
289282

@@ -292,7 +285,7 @@ def to_dict(self, include_meta=False, skip_empty=True):
292285
Serialize the instance into a dictionary so that it can be saved in elasticsearch.
293286
294287
:arg include_meta: if set to ``True`` will include all the metadata
295-
(``_index``, ``_type``, ``_id`` etc). Otherwise just the document's
288+
(``_index``, ``_id`` etc). Otherwise just the document's
296289
data is serialized. This is useful when passing multiple instances into
297290
``elasticsearch.helpers.bulk``.
298291
:arg skip_empty: if set to ``False`` will cause empty values (``None``,
@@ -314,7 +307,6 @@ def to_dict(self, include_meta=False, skip_empty=True):
314307
if index is not None:
315308
meta['_index'] = index
316309

317-
meta['_type'] = self._doc_type.name
318310
meta['_source'] = d
319311
return meta
320312

@@ -398,7 +390,6 @@ def update(self, using=None, index=None, detect_noop=True,
398390

399391
meta = self._get_connection(using).update(
400392
index=self._get_index(index),
401-
doc_type=self._doc_type.name,
402393
body=body,
403394
refresh=refresh,
404395
**doc_meta
@@ -442,7 +433,6 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
442433
doc_meta.update(kwargs)
443434
meta = es.index(
444435
index=self._get_index(index),
445-
doc_type=self._doc_type.name,
446436
body=self.to_dict(skip_empty=skip_empty),
447437
**doc_meta
448438
)

elasticsearch_dsl/field.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def empty(self):
173173

174174
def to_dict(self):
175175
d = self._mapping.to_dict()
176-
_, d = d.popitem()
177176
d.update(super(Object, self).to_dict())
178177
return d
179178

elasticsearch_dsl/index.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def save(self, using=None):
3333
es.indices.put_template(name=self._template_name, body=self.to_dict())
3434

3535
class Index(object):
36-
def __init__(self, name, doc_type=DEFAULT_DOC_TYPE, using='default'):
36+
def __init__(self, name, using='default'):
3737
"""
3838
:arg name: name of the index
3939
:arg using: connection alias to use, defaults to ``'default'``
@@ -45,19 +45,10 @@ def __init__(self, name, doc_type=DEFAULT_DOC_TYPE, using='default'):
4545
self._aliases = {}
4646
self._analysis = {}
4747
self._mapping = None
48-
if doc_type is not DEFAULT_DOC_TYPE:
49-
self._mapping = Mapping(doc_type)
5048

51-
def _get_doc_type(self):
52-
if self._mapping is not None:
53-
return self._mapping.doc_type
54-
for d in self._doc_types:
55-
return d._doc_type.name
56-
return None
57-
58-
def get_or_create_mapping(self, doc_type=DEFAULT_DOC_TYPE):
49+
def get_or_create_mapping(self):
5950
if self._mapping is None:
60-
self._mapping = Mapping(doc_type)
51+
self._mapping = Mapping()
6152
return self._mapping
6253

6354
def as_template(self, template_name, pattern=None):
@@ -87,7 +78,7 @@ def resolve_field(self, field_path):
8778
def load_mappings(self, using=None):
8879
self.get_or_create_mapping().update_from_es(self._name, using=using or self._using)
8980

90-
def clone(self, name=None, doc_type=None, using=None):
81+
def clone(self, name=None, using=None):
9182
"""
9283
Create a copy of the instance with another name or connection alias.
9384
Useful for creating multiple indices with shared configuration::
@@ -102,10 +93,7 @@ def clone(self, name=None, doc_type=None, using=None):
10293
:arg name: name of the index
10394
:arg using: connection alias to use, defaults to ``'default'``
10495
"""
105-
doc_type = doc_type or self._get_doc_type() or DEFAULT_DOC_TYPE
106-
i = Index(name or self._name,
107-
doc_type=doc_type,
108-
using=using or self._using)
96+
i = Index(name or self._name, using=using or self._using)
10997
i._settings = self._settings.copy()
11098
i._aliases = self._aliases.copy()
11199
i._analysis = self._analysis.copy()
@@ -128,11 +116,7 @@ def mapping(self, mapping):
128116
This means that, when this index is created, it will contain the
129117
mappings for the document type defined by those mappings.
130118
"""
131-
if self._mapping is not None and mapping.doc_type != self._mapping.doc_type:
132-
raise IllegalOperation(
133-
'Index object cannot have multiple types, %s already set, '
134-
'trying to assign %s.' % (self._mapping.doc_type, mapping.doc_type))
135-
self.get_or_create_mapping(mapping.doc_type).update(mapping)
119+
self.get_or_create_mapping().update(mapping)
136120

137121
def document(self, document):
138122
"""
@@ -155,12 +139,6 @@ class Post(Document):
155139
# properly deserialized Post instances
156140
s = i.search()
157141
"""
158-
name = document._doc_type.name
159-
doc_type = self._get_doc_type()
160-
if doc_type and name != doc_type:
161-
raise IllegalOperation(
162-
'Index object cannot have multiple types, %s already set, '
163-
'trying to assign %s.' % (doc_type, name))
164142
self._doc_types.append(document)
165143

166144
# If the document index does not have any name, that means the user
@@ -170,7 +148,6 @@ class Post(Document):
170148
document._index = self
171149

172150
return document
173-
doc_type = document
174151

175152
def settings(self, **kwargs):
176153
"""
@@ -234,7 +211,7 @@ def to_dict(self):
234211
mapping = d._doc_type.mapping
235212
merge(mappings, mapping.to_dict(), True)
236213
merge(analysis, mapping._collect_analysis(), True)
237-
if mappings and mappings[self._get_doc_type()]:
214+
if mappings:
238215
out['mappings'] = mappings
239216
if analysis or self._analysis:
240217
merge(analysis, self._analysis)
@@ -265,7 +242,6 @@ def updateByQuery(self, using=None):
265242
return UpdateByQuery(
266243
using=using or self._using,
267244
index=self._name,
268-
doc_type=self._doc_types
269245
)
270246

271247
def create(self, using=None, **kwargs):
@@ -328,8 +304,7 @@ def save(self, using=None):
328304
# exception
329305
mappings = body.pop('mappings', {})
330306
if mappings:
331-
for doc_type in mappings:
332-
self.put_mapping(using=using, doc_type=doc_type, body=mappings[doc_type])
307+
self.put_mapping(using=using, body=mappings)
333308

334309
def analyze(self, using=None, **kwargs):
335310
"""

elasticsearch_dsl/mapping.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616
))
1717

1818
class Properties(DslBase):
19+
# FIXME: to_dict pop
20+
name = 'properties'
1921
_param_defs = {'properties': {'type': 'field', 'hash': True}}
20-
def __init__(self, name):
21-
self._name = name
22+
def __init__(self):
2223
super(Properties, self).__init__()
2324

2425
def __repr__(self):
25-
return 'Properties(%r)' % self._name
26+
return 'Properties()'
2627

2728
def __getitem__(self, name):
2829
return self.properties[name]
2930

3031
def __contains__(self, name):
3132
return name in self.properties
3233

33-
@property
34-
def name(self):
35-
return self._name
34+
def to_dict(self):
35+
return super().to_dict()['properties']
3636

3737
def field(self, name, *args, **kwargs):
3838
self.properties[name] = construct_field(*args, **kwargs)
@@ -66,21 +66,21 @@ def update(self, other_object):
6666

6767

6868
class Mapping(object):
69-
def __init__(self, name):
70-
self.properties = Properties(name)
69+
def __init__(self):
70+
self.properties = Properties()
7171
self._meta = {}
7272

7373
def __repr__(self):
74-
return 'Mapping(%r)' % self.doc_type
74+
return 'Mapping()'
7575

7676
def _clone(self):
77-
m = Mapping(self.properties.name)
77+
m = Mapping()
7878
m.properties._params = self.properties._params.copy()
7979
return m
8080

8181
@classmethod
82-
def from_es(cls, index, doc_type, using='default'):
83-
m = cls(doc_type)
82+
def from_es(cls, index, using='default'):
83+
m = cls()
8484
m.update_from_es(index, using)
8585
return m
8686

@@ -131,18 +131,17 @@ def _collect_analysis(self):
131131

132132
def save(self, index, using='default'):
133133
from .index import Index
134-
index = Index(index, doc_type=self.doc_type, using=using)
134+
index = Index(index, using=using)
135135
index.mapping(self)
136136
return index.save()
137137

138138
def update_from_es(self, index, using='default'):
139139
es = connections.get_connection(using)
140-
raw = es.indices.get_mapping(index=index, doc_type=self.doc_type)
140+
raw = es.indices.get_mapping(index=index)
141141
_, raw = raw.popitem()
142142
self._update_from_dict(raw['mappings'])
143143

144144
def _update_from_dict(self, raw):
145-
raw = raw[self.doc_type]
146145
for name, definition in iteritems(raw.get('properties', {})):
147146
self.field(name, definition)
148147

@@ -180,10 +179,6 @@ def __getitem__(self, name):
180179
def __iter__(self):
181180
return iter(self.properties.properties)
182181

183-
@property
184-
def doc_type(self):
185-
return self.properties.name
186-
187182
def field(self, *args, **kwargs):
188183
self.properties.field(*args, **kwargs)
189184
return self
@@ -199,7 +194,6 @@ def meta(self, name, params=None, **kwargs):
199194
return self
200195

201196
def to_dict(self):
202-
d = self.properties.to_dict()
203197
meta = self._meta
204198

205199
# hard coded serialization of analyzers in _all
@@ -209,5 +203,5 @@ def to_dict(self):
209203
for f in ('analyzer', 'search_analyzer', 'search_quote_analyzer'):
210204
if hasattr(_all.get(f, None), 'to_dict'):
211205
_all[f] = _all[f].to_dict()
212-
d[self.doc_type].update(meta)
213-
return d
206+
meta.update(self.properties.to_dict())
207+
return meta

elasticsearch_dsl/response/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..utils import AttrDict, AttrList
1+
from ..utils import AttrDict, AttrList, _wrap
22

33
from .hit import Hit, HitMeta
44

@@ -52,7 +52,7 @@ def hits(self):
5252
# avoid assigning _hits into self._d_
5353
super(AttrDict, self).__setattr__('_hits', hits)
5454
for k in h:
55-
setattr(self._hits, k, h[k])
55+
setattr(self._hits, k, _wrap(h[k]))
5656
return self._hits
5757

5858
@property

elasticsearch_dsl/response/hit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __repr__(self):
2828
return '<Hit({}): {}>'.format(
2929
'/'.join(
3030
getattr(self.meta, key)
31-
for key in ('index', 'doc_type', 'id')
31+
for key in ('index', 'id')
3232
if key in self.meta),
3333
super(Hit, self).__repr__()
3434
)

0 commit comments

Comments
 (0)