3
3
from .exceptions import IllegalOperation
4
4
from .mapping import Mapping
5
5
6
+ DEFAULT_DOC_TYPE = 'doc'
7
+
6
8
class IndexTemplate (object ):
7
9
def __init__ (self , name , template , index = None , ** kwargs ):
8
10
if index is None :
@@ -28,7 +30,7 @@ def save(self, using=None):
28
30
es .indices .put_template (name = self ._template_name , body = self .to_dict ())
29
31
30
32
class Index (object ):
31
- def __init__ (self , name , doc_type = 'doc' , using = 'default' ):
33
+ def __init__ (self , name , doc_type = DEFAULT_DOC_TYPE , using = 'default' ):
32
34
"""
33
35
:arg name: name of the index
34
36
:arg using: connection alias to use, defaults to ``'default'``
@@ -39,7 +41,14 @@ def __init__(self, name, doc_type='doc', using='default'):
39
41
self ._settings = {}
40
42
self ._aliases = {}
41
43
self ._analysis = {}
42
- self ._mapping = Mapping (doc_type )
44
+ self ._mapping = None
45
+ if doc_type is not DEFAULT_DOC_TYPE :
46
+ self ._mapping = Mapping (doc_type )
47
+
48
+ def get_or_create_mapping (self , doc_type = DEFAULT_DOC_TYPE ):
49
+ if self ._mapping is None :
50
+ self ._mapping = Mapping (doc_type )
51
+ return self ._mapping
43
52
44
53
def as_template (self , template_name , pattern = None ):
45
54
# TODO: should we allow pattern to be a top-level arg?
@@ -48,10 +57,10 @@ def as_template(self, template_name, pattern=None):
48
57
return IndexTemplate (template_name , pattern or self ._name , index = self )
49
58
50
59
def resolve_field (self , field_path ):
51
- return self ._mapping .resolve_field (field_path )
60
+ return self .get_or_create_mapping () .resolve_field (field_path )
52
61
53
62
def load_mappings (self , using = None ):
54
- self ._mapping .update_from_es (self ._name , using = using or self ._using )
63
+ self .get_or_create_mapping () .update_from_es (self ._name , using = using or self ._using )
55
64
56
65
def clone (self , name = None , doc_type = None , using = None ):
57
66
"""
@@ -68,14 +77,18 @@ def clone(self, name=None, doc_type=None, using=None):
68
77
:arg name: name of the index
69
78
:arg using: connection alias to use, defaults to ``'default'``
70
79
"""
80
+ doc_type = doc_type or (
81
+ DEFAULT_DOC_TYPE if self ._mapping is None else self ._mapping .doc_type
82
+ )
71
83
i = Index (name or self ._name ,
72
- doc_type = doc_type or self . _mapping . doc_type ,
84
+ doc_type = doc_type ,
73
85
using = using or self ._using )
74
86
i ._settings = self ._settings .copy ()
75
87
i ._aliases = self ._aliases .copy ()
76
88
i ._analysis = self ._analysis .copy ()
77
89
i ._doc_types = self ._doc_types [:]
78
- i ._mapping = self ._mapping ._clone ()
90
+ if self ._mapping is not None :
91
+ i ._mapping = self ._mapping ._clone ()
79
92
return i
80
93
81
94
def _get_connection (self , using = None ):
@@ -89,11 +102,11 @@ def mapping(self, mapping):
89
102
This means that, when this index is created, it will contain the
90
103
mappings for the document type defined by those mappings.
91
104
"""
92
- if mapping .doc_type != self ._mapping .doc_type :
105
+ if self . _mapping is not None and mapping .doc_type != self ._mapping .doc_type :
93
106
raise IllegalOperation (
94
107
'Index object cannot have multiple types, %s already set, '
95
108
'trying to assign %s.' % (self ._mapping .doc_type , mapping .doc_type ))
96
- self ._mapping .update (mapping )
109
+ self .get_or_create_mapping ( mapping . doc_type ) .update (mapping )
97
110
98
111
def document (self , document ):
99
112
"""
@@ -117,17 +130,14 @@ class Post(Document):
117
130
s = i.search()
118
131
"""
119
132
name = document ._doc_type .name
120
- if name != self ._mapping .doc_type :
133
+ if self . _mapping is not None and name != self ._mapping .doc_type :
121
134
raise IllegalOperation (
122
135
'Index object cannot have multiple types, %s already set, '
123
136
'trying to assign %s.' % (self ._mapping .doc_type , name ))
124
137
self ._doc_types .append (document )
125
138
# TODO: do this at save time to allow Document to be modified after
126
139
# creation?
127
- self ._mapping .update (document ._doc_type .mapping )
128
-
129
- if document ._index is DEFAULT_INDEX :
130
- document ._index = self
140
+ self .get_or_create_mapping (document ._doc_type .name ).update (document ._doc_type .mapping )
131
141
return document
132
142
doc_type = document
133
143
@@ -188,9 +198,9 @@ def to_dict(self):
188
198
out ['settings' ] = self ._settings
189
199
if self ._aliases :
190
200
out ['aliases' ] = self ._aliases
191
- mappings = self ._mapping .to_dict ()
192
- analysis = self ._mapping ._collect_analysis ()
193
- if mappings [self ._mapping .doc_type ]:
201
+ mappings = self ._mapping .to_dict () if self . _mapping else None
202
+ analysis = self ._mapping ._collect_analysis () if self . _mapping else {}
203
+ if mappings and mappings [self ._mapping .doc_type ]:
194
204
out ['mappings' ] = mappings
195
205
if analysis or self ._analysis :
196
206
for key in self ._analysis :
@@ -548,5 +558,3 @@ def shrink(self, using=None, **kwargs):
548
558
``Elasticsearch.indices.shrink`` unchanged.
549
559
"""
550
560
return self ._get_connection (using ).indices .shrink (index = self ._name , ** kwargs )
551
-
552
- DEFAULT_INDEX = Index ('*' )
0 commit comments