@@ -246,6 +246,69 @@ class ContactIndex(AlgoliaIndex):
246
246
should_index = ' is_adult'
247
247
```
248
248
249
+ ## Multiple indices per model
250
+
251
+ It is possible to have several indices for a single model.
252
+
253
+ - First, define all your indices that you want for a model:
254
+ ``` python
255
+ from django.contrib.algoliasearch import AlgoliaIndex
256
+
257
+ class MyModelIndex1 (AlgoliaIndex ):
258
+ name = ' MyModelIndex1'
259
+ ...
260
+
261
+ class MyModelIndex2 (AlgoliaIndex ):
262
+ name = ' MyModelIndex2'
263
+ ...
264
+ ```
265
+
266
+ - Then, define a meta model which will aggregate those indices:
267
+ ``` python
268
+ class MyModelMetaIndex (AlgoliaIndex ):
269
+ def __init__ (self , model , client , settings ):
270
+ self .indices = [
271
+ MyModelIndex1(model, client, settings),
272
+ MyModelIndex2(model, client, settings),
273
+ ]
274
+
275
+ def raw_search (self , query = ' ' , params = None ):
276
+ res = {}
277
+ for index in self .indices:
278
+ res[index.name] = index.raw_search(query, params)
279
+ return res
280
+
281
+ def update_records (self , qs , batch_size = 1000 , ** kwargs ):
282
+ for index in self .indices:
283
+ index.update_records(qs, batch_size, ** kwargs)
284
+
285
+ def reindex_all (self , batch_size = 1000 ):
286
+ for index in self .indices:
287
+ index.reindex_all(batch_size)
288
+
289
+ def set_settings (self ):
290
+ for index in self .indices:
291
+ index.set_settings()
292
+
293
+ def clear_index (self ):
294
+ for index in self .indices:
295
+ index.clear_index()
296
+
297
+ def save_record (self , instance , update_fields = None , ** kwargs ):
298
+ for index in self .indices:
299
+ index.save_record(instance, update_fields, ** kwargs)
300
+
301
+ def delete_record (self , instance ):
302
+ for index in self .indices:
303
+ index.delete_record(instance)
304
+ ```
305
+
306
+ - Finally, register this ` AlgoliaIndex ` with your ` Model ` :
307
+ ``` python
308
+ import algoliasearch_django as algoliasearch
309
+ algoliasearch.register(MyModel, MyModelMetaIndex)
310
+ ```
311
+
249
312
250
313
# Tests
251
314
0 commit comments