| 1 |
.. _multiple-indexes-label: |
| 2 |
|
| 3 |
Multiple search indexes |
| 4 |
======================= |
| 5 |
|
| 6 |
So far, we have only used one class in the ``index_classes`` attribute of our serializers. However, you are able to specify |
| 7 |
a list of them. This can be useful when your search engine has indexed multiple models and you want to provide aggregate |
| 8 |
results across two or more of them. To use the default multiple index support, simply add multiple indexes the ``index_classes`` |
| 9 |
list |
| 10 |
|
| 11 |
.. code-block:: python |
| 12 |
|
| 13 |
class PersonIndex(indexes.SearchIndex, indexes.Indexable): |
| 14 |
text = indexes.CharField(document=True, use_template=True) |
| 15 |
firstname = indexes.CharField(model_attr="first_name") |
| 16 |
lastname = indexes.CharField(model_attr="last_name") |
| 17 |
|
| 18 |
def get_model(self): |
| 19 |
return Person |
| 20 |
|
| 21 |
class PlaceIndex(indexes.SearchIndex, indexes.Indexable): |
| 22 |
text = indexes.CharField(document=True, use_template=True) |
| 23 |
address = indexes.CharField(model_attr="address") |
| 24 |
|
| 25 |
def get_model(self): |
| 26 |
return Place |
| 27 |
|
| 28 |
class ThingIndex(indexes.SearchIndex, indexes.Indexable): |
| 29 |
text = indexes.CharField(document=True, use_template=True) |
| 30 |
name = indexes.CharField(model_attr="name") |
| 31 |
|
| 32 |
def get_model(self): |
| 33 |
return Thing |
| 34 |
|
| 35 |
class AggregateSerializer(HaystackSerializer): |
| 36 |
|
| 37 |
class Meta: |
| 38 |
index_classes = [PersonIndex, PlaceIndex, ThingIndex] |
| 39 |
fields = ["firstname", "lastname", "address", "name"] |
| 40 |
|
| 41 |
|
| 42 |
class AggregateSearchViewSet(HaystackViewSet): |
| 43 |
|
| 44 |
serializer_class = AggregateSerializer |
| 45 |
|
| 46 |
.. note:: |
| 47 |
|
| 48 |
The ``AggregateSearchViewSet`` class above omits the optional ``index_models`` attribute. This way results from all the |
| 49 |
models are returned. |
| 50 |
|
| 51 |
The result from searches using multiple indexes is a list of objects, each of which contains only the fields appropriate to |
| 52 |
the model from which the result came. For instance if a search returned a list containing one each of the above models, it |
| 53 |
might look like the following: |
| 54 |
|
| 55 |
.. code-block:: javascript |
| 56 |
|
| 57 |
[ |
| 58 |
{ |
| 59 |
"text": "John Doe", |
| 60 |
"firstname": "John", |
| 61 |
"lastname": "Doe" |
| 62 |
}, |
| 63 |
{ |
| 64 |
"text": "123 Doe Street", |
| 65 |
"address": "123 Doe Street" |
| 66 |
}, |
| 67 |
{ |
| 68 |
"text": "Doe", |
| 69 |
"name": "Doe" |
| 70 |
} |
| 71 |
] |
| 72 |
|
| 73 |
Declared fields |
| 74 |
--------------- |
| 75 |
|
| 76 |
You can include field declarations in the serializer class like normal. Depending on how they are named, they will be |
| 77 |
treated as common fields and added to every result or as specific to results from a particular index. |
| 78 |
|
| 79 |
Common fields are declared as you would any serializer field. Index-specific fields must be prefixed with "_<index class name>__". |
| 80 |
The following example illustrates this usage: |
| 81 |
|
| 82 |
.. code-block:: python |
| 83 |
|
| 84 |
class AggregateSerializer(HaystackSerializer): |
| 85 |
extra = serializers.SerializerMethodField() |
| 86 |
_ThingIndex__number = serializers.SerializerMethodField() |
| 87 |
|
| 88 |
class Meta: |
| 89 |
index_classes = [PersonIndex, PlaceIndex, ThingIndex] |
| 90 |
fields = ["firstname", "lastname", "address", "name"] |
| 91 |
|
| 92 |
def get_extra(self,instance): |
| 93 |
return "whatever" |
| 94 |
|
| 95 |
def get__ThingIndex__number(self,instance): |
| 96 |
return 42 |
| 97 |
|
| 98 |
The results of a search might then look like the following: |
| 99 |
|
| 100 |
.. code-block:: javascript |
| 101 |
|
| 102 |
[ |
| 103 |
{ |
| 104 |
"text": "John Doe", |
| 105 |
"firstname": "John", |
| 106 |
"lastname": "Doe", |
| 107 |
"extra": "whatever" |
| 108 |
}, |
| 109 |
{ |
| 110 |
"text": "123 Doe Street", |
| 111 |
"address": "123 Doe Street", |
| 112 |
"extra": "whatever" |
| 113 |
}, |
| 114 |
{ |
| 115 |
"text": "Doe", |
| 116 |
"name": "Doe", |
| 117 |
"extra": "whatever", |
| 118 |
"number": 42 |
| 119 |
} |
| 120 |
] |
| 121 |
|
| 122 |
Multiple Serializers |
| 123 |
-------------------- |
| 124 |
|
| 125 |
Alternatively, you can specify a 'serializers' attribute on your Meta class to use a different serializer class |
| 126 |
for different indexes as show below: |
| 127 |
|
| 128 |
.. code-block:: python |
| 129 |
|
| 130 |
class AggregateSearchSerializer(HaystackSerializer): |
| 131 |
class Meta: |
| 132 |
serializers = { |
| 133 |
PersonIndex: PersonSearchSerializer, |
| 134 |
PlaceIndex: PlaceSearchSerializer, |
| 135 |
ThingIndex: ThingSearchSerializer |
| 136 |
} |
| 137 |
|
| 138 |
The ``serializers`` attribute is the important thing here, It's a dictionary with ``SearchIndex`` classes as |
| 139 |
keys and ``Serializer`` classes as values. Each result in the list of results from a search that contained |
| 140 |
items from multiple indexes would be serialized according to the appropriate serializer. |
| 141 |
|
| 142 |
.. warning:: |
| 143 |
|
| 144 |
If a field name is shared across serializers, and one serializer overrides the field mapping, the overridden |
| 145 |
mapping will be used for *all* serializers. See the example below for more details. |
| 146 |
|
| 147 |
.. code-block:: python |
| 148 |
|
| 149 |
from rest_framework import serializers |
| 150 |
|
| 151 |
class PersonSearchSerializer(HaystackSerializer): |
| 152 |
# NOTE: This override will be used for both Person and Place objects. |
| 153 |
name = serializers.SerializerMethodField() |
| 154 |
|
| 155 |
class Meta: |
| 156 |
fields = ['name'] |
| 157 |
|
| 158 |
class PlaceSearchSerializer(HaystackSerializer): |
| 159 |
class Meta: |
| 160 |
fields = ['name'] |
| 161 |
|
| 162 |
class AggregateSearchSerializer(HaystackSerializer): |
| 163 |
class Meta: |
| 164 |
serializers = { |
| 165 |
PersonIndex: PersonSearchSerializer, |
| 166 |
PlaceIndex: PlaceSearchSerializer, |
| 167 |
ThingIndex: ThingSearchSerializer |
| 168 |
} |