4

How can I store json in elasticsearch by django-haystack fields?

At the moment I am using following trick:

# search_indexes.py

class BlogPostIndex(indexes.SearchIndex, indexes.Indexable):
    author = indexes.CharField()

    @staticmethod
    def prepare_author(obj):
        return json.dumps(serializers.UserMinimalSerializer(obj.author).data)
# serializers.py

class BlogPostSearchSerializer(HaystackSerializer):
    author = serializers.SerializerMethodField()

    @staticmethod
    def get_author(obj):
        return json.loads(obj.author)
ehsan ahmadi
  • 365
  • 2
  • 13

0 Answers0