@@ -254,17 +254,19 @@ def _close_search_context(self, request):
254254 total_instances = sum (len (pk_list ) for pk_list in captured_tasks .values ())
255255 threshold = getattr (settings , "WATSON_ASYNC_INDEX_UPDATE_THRESHOLD" , 100 )
256256
257- # If threshold is below 0, async updating is disabled
258- if threshold < 0 :
259- logger .debug (f"AsyncSearchContextMiddleware: Async updating disabled (threshold={ threshold } ), using synchronous update" )
260- elif total_instances > threshold :
261- logger .debug (f"AsyncSearchContextMiddleware: { total_instances } instances > { threshold } threshold, triggering async update" )
262- self ._trigger_async_index_update (captured_tasks )
263- # Invalidate to prevent synchronous index update by super()._close_search_context()
264- search_context_manager .invalidate ()
265- else :
266- logger .debug (f"AsyncSearchContextMiddleware: { total_instances } instances <= { threshold } threshold, using synchronous update" )
267- # Let watson handle synchronous update for small numbers
257+ # only needed when at least one model instance is updated
258+ if total_instances > 0 :
259+ # If threshold is below 0, async updating is disabled
260+ if threshold < 0 :
261+ logger .debug (f"AsyncSearchContextMiddleware: Async updating disabled (threshold={ threshold } ), using synchronous update" )
262+ elif total_instances > threshold :
263+ logger .debug (f"AsyncSearchContextMiddleware: { total_instances } instances > { threshold } threshold, triggering async update" )
264+ self ._trigger_async_index_update (captured_tasks )
265+ # Invalidate to prevent synchronous index update by super()._close_search_context()
266+ search_context_manager .invalidate ()
267+ else :
268+ logger .debug (f"AsyncSearchContextMiddleware: { total_instances } instances <= { threshold } threshold, using synchronous update" )
269+ # Let watson handle synchronous update for small numbers
268270
269271 super ()._close_search_context (request )
270272
0 commit comments