77from django import VERSION as DJANGO_VERSION
88from django .db import models
99from elasticsearch .helpers import bulk , parallel_bulk
10- from elasticsearch_dsl import Document as DSLDocument
10+ from elasticsearch . dsl import Document as DSLDocument
1111from six import iteritems
1212
1313from .exceptions import ModelFieldNotMappedError
2121 KeywordField ,
2222 LongField ,
2323 ShortField ,
24- TextField , TimeField ,
24+ TextField ,
25+ TimeField ,
2526)
2627from .search import Search
2728from .signals import post_index
@@ -88,7 +89,7 @@ def search(cls, using=None, index=None):
8889 using = cls ._get_using (using ),
8990 index = cls ._default_index (index ),
9091 doc_type = [cls ],
91- model = cls .django .model
92+ model = cls .django .model ,
9293 )
9394
9495 def get_queryset (self ):
@@ -104,7 +105,7 @@ def get_indexing_queryset(self):
104105 qs = self .get_queryset ()
105106 kwargs = {}
106107 if DJANGO_VERSION >= (2 ,) and self .django .queryset_pagination :
107- kwargs = {' chunk_size' : self .django .queryset_pagination }
108+ kwargs = {" chunk_size" : self .django .queryset_pagination }
108109 return qs .iterator (** kwargs )
109110
110111 def init_prepare (self ):
@@ -113,7 +114,7 @@ def init_prepare(self):
113114 from the model and generate a list of callables to avoid doing that
114115 work on every object instance over.
115116 """
116- index_fields = getattr (self , ' _fields' , {})
117+ index_fields = getattr (self , " _fields" , {})
117118 fields = []
118119 for name , field in iteritems (index_fields ):
119120 if not isinstance (field , DEDField ):
@@ -122,15 +123,20 @@ def init_prepare(self):
122123 if not field ._path :
123124 field ._path = [name ]
124125
125- prep_func = getattr (self , ' prepare_%s_with_related' % name , None )
126+ prep_func = getattr (self , " prepare_%s_with_related" % name , None )
126127 if prep_func :
127- fn = partial (prep_func , related_to_ignore = self ._related_instance_to_ignore )
128+ fn = partial (
129+ prep_func , related_to_ignore = self ._related_instance_to_ignore
130+ )
128131 else :
129- prep_func = getattr (self , ' prepare_%s' % name , None )
132+ prep_func = getattr (self , " prepare_%s" % name , None )
130133 if prep_func :
131134 fn = prep_func
132135 else :
133- fn = partial (field .get_value_from_instance , field_value_to_ignore = self ._related_instance_to_ignore )
136+ fn = partial (
137+ field .get_value_from_instance ,
138+ field_value_to_ignore = self ._related_instance_to_ignore ,
139+ )
134140
135141 fields .append ((name , field , fn ))
136142
@@ -166,8 +172,9 @@ def to_field(cls, field_name, model_field):
166172 model field to ES field logic
167173 """
168174 try :
169- return cls .get_model_field_class_to_field_class ()[
170- model_field .__class__ ](attr = field_name )
175+ return cls .get_model_field_class_to_field_class ()[model_field .__class__ ](
176+ attr = field_name
177+ )
171178 except KeyError :
172179 raise ModelFieldNotMappedError (
173180 "Cannot convert model field {} "
@@ -178,17 +185,16 @@ def bulk(self, actions, **kwargs):
178185 response = bulk (client = self ._get_connection (), actions = actions , ** kwargs )
179186 # send post index signal
180187 post_index .send (
181- sender = self .__class__ ,
182- instance = self ,
183- actions = actions ,
184- response = response
188+ sender = self .__class__ , instance = self , actions = actions , response = response
185189 )
186190 return response
187191
188192 def parallel_bulk (self , actions , ** kwargs ):
189- if self .django .queryset_pagination and 'chunk_size' not in kwargs :
190- kwargs ['chunk_size' ] = self .django .queryset_pagination
191- bulk_actions = parallel_bulk (client = self ._get_connection (), actions = actions , ** kwargs )
193+ if self .django .queryset_pagination and "chunk_size" not in kwargs :
194+ kwargs ["chunk_size" ] = self .django .queryset_pagination
195+ bulk_actions = parallel_bulk (
196+ client = self ._get_connection (), actions = actions , ** kwargs
197+ )
192198 # As the `parallel_bulk` is lazy, we need to get it into `deque` to run it instantly
193199 # See https://discuss.elastic.co/t/helpers-parallel-bulk-in-python-not-working/39498/2
194200 deque (bulk_actions , maxlen = 0 )
@@ -207,29 +213,26 @@ def generate_id(cls, object_instance):
207213
208214 def _prepare_action (self , object_instance , action ):
209215 return {
210- '_op_type' : action ,
211- '_index' : self ._index ._name ,
212- '_id' : self .generate_id (object_instance ),
213- '_source' : (
214- self .prepare (object_instance ) if action != 'delete' else None
215- ),
216+ "_op_type" : action ,
217+ "_index" : self ._index ._name ,
218+ "_id" : self .generate_id (object_instance ),
219+ "_source" : (self .prepare (object_instance ) if action != "delete" else None ),
216220 }
217221
218222 def _get_actions (self , object_list , action ):
219223 for object_instance in object_list :
220- if action == ' delete' or self .should_index_object (object_instance ):
224+ if action == " delete" or self .should_index_object (object_instance ):
221225 yield self ._prepare_action (object_instance , action )
222-
226+
223227 def get_actions (self , object_list , action ):
224228 """
225229 Generate the elasticsearch payload.
226230 """
227231 return self ._get_actions (object_list , action )
228232
229-
230233 def _bulk (self , * args , ** kwargs ):
231234 """Helper for switching between normal and parallel bulk operation"""
232- parallel = kwargs .pop (' parallel' , False )
235+ parallel = kwargs .pop (" parallel" , False )
233236 if parallel :
234237 return self .parallel_bulk (* args , ** kwargs )
235238 else :
@@ -242,24 +245,22 @@ def should_index_object(self, obj):
242245 """
243246 return True
244247
245- def update (self , thing , refresh = None , action = ' index' , parallel = False , ** kwargs ):
248+ def update (self , thing , refresh = None , action = " index" , parallel = False , ** kwargs ):
246249 """
247250 Update each document in ES for a model, iterable of models or queryset
248251 """
249252 if refresh is not None :
250- kwargs [' refresh' ] = refresh
253+ kwargs [" refresh" ] = refresh
251254 elif self .django .auto_refresh :
252- kwargs [' refresh' ] = self .django .auto_refresh
255+ kwargs [" refresh" ] = self .django .auto_refresh
253256
254257 if isinstance (thing , models .Model ):
255258 object_list = [thing ]
256259 else :
257260 object_list = thing
258261
259262 return self ._bulk (
260- self ._get_actions (object_list , action ),
261- parallel = parallel ,
262- ** kwargs
263+ self ._get_actions (object_list , action ), parallel = parallel , ** kwargs
263264 )
264265
265266
0 commit comments