|
10 | 10 |
|
11 | 11 |
|
12 | 12 | @task |
13 | | -def index_objects(mapping_type, ids, chunk_size=100, es=None, index=None): |
| 13 | +def index_objects(mapping_type, ids, chunk_size=100, id_field='id', es=None, |
| 14 | + index=None): |
14 | 15 | """Index documents of a specified mapping type. |
15 | 16 |
|
16 | 17 | This allows for asynchronous indexing. |
@@ -48,21 +49,24 @@ def update_in_index(sender, instance, **kw): |
48 | 49 |
|
49 | 50 | # Get the model this mapping type is based on. |
50 | 51 | model = mapping_type.get_model() |
| 52 | + filter_key = '{0}__in'.format(id_field) |
51 | 53 |
|
52 | 54 | # Retrieve all the objects that we're going to index and do it in |
53 | 55 | # bulk. |
54 | 56 | for id_list in chunked(ids, chunk_size): |
55 | 57 | documents = [] |
56 | 58 |
|
57 | | - for obj in model.objects.filter(id__in=id_list): |
| 59 | + for obj in model.objects.filter(**{filter_key: id_list}): |
58 | 60 | try: |
59 | | - documents.append(mapping_type.extract_document(obj.id, obj)) |
60 | | - except Exception as exc: |
| 61 | + _id = str(getattr(obj, id_field)) |
| 62 | + documents.append(mapping_type.extract_document(_id, obj)) |
| 63 | + except StandardError as exc: |
61 | 64 | log.exception('Unable to extract document {0}: {1}'.format( |
62 | 65 | obj, repr(exc))) |
63 | 66 |
|
64 | 67 | if documents: |
65 | | - mapping_type.bulk_index(documents, id_field='id', es=es, index=index) |
| 68 | + mapping_type.bulk_index(documents, id_field=id_field, es=es, |
| 69 | + index=index) |
66 | 70 |
|
67 | 71 |
|
68 | 72 | @task |
|
0 commit comments