I'm trying to use django-haystack
to include an elasticsearch
based search in my application. I'm using django-rest-framework
to build the API. Following is my index:
class ArtistIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='name')
def get_model(self):
return Artist
This is my view:
def search(request):
q = request.GET.get('q', '')
if q is '':
return Response(status=status.HTTP_400_BAD_REQUEST)
all_results = SearchQuerySet().models(Artist).filter(content=Clean(q))
serializer = ArtistSerializer(all_results, many=True)
print serializer.data
return Response(status=status.HTTP_200_OK)
And my serializer:
class ArtistSerializer(serializers.ModelSerializer):
class Meta:
model = Artist
fields = (
'name',
'type',
'gender',
'begin_life',
'end_life',
)
However, when I hit the endpoint, the print
statement prints something like this:
[{'name': None, 'type': None, 'gender': None, 'begin_life': None, 'end_life': None}, {'name': None, 'type': None, 'gender': None, 'begin_life': None, 'end_life': None}, {'name': None, 'type': None, 'gender': None, 'begin_life': None, 'end_life': None}, {'name': None, 'type': None, 'gender': None, 'begin_life': None, 'end_life': None}, {'name': None, 'type': None, 'gender': None, 'begin_life': None, 'end_life': None}]
Could someone please tell what am I doing wrong?
Thanks.
I found a better way to serialize the data. I am now using this:
Now, I am able to search in multiple models without worrying about serializing the data :)
The result data is like this:
so, now I can possibly differentiate results on my client.