'Multiple languages on individual fields for ElasticSearch

I've got a django app and am using django_elasticsearch_dsl_drf for managing the documents for Elasticsearch (7.1) and handling multiple languages through django-parler.

I've got a bunch of documents and some fields have multiple languages. I thought I'd deal with this like so:

topic = fields.ObjectField(properties={
    'id': fields.IntegerField(attr='id'),
    'name': StringField(
        analyzer=html_strip,
        fields={
            'raw': StringField(analyzer='keyword'),
            'suggest': fields.CompletionField()
        }
    ),
    'about': fields.ObjectField(properties={
        'en': StringField(
            analyzer=html_strip_english,
            fields={
                'raw': StringField(analyzer="keyword")
            }),
        'es': StringField(
            analyzer=html_strip_english,
            fields={
                'raw': StringField(analyzer="keyword")
            })
    })
})

This is HTML text in a particular language.

The problem is en doesn't exist so I thought the best way to do this is use prepare_en etc:

def prepare_en(self, instance):
    // query to get the en for that instance

However this throws an error VariableLookupError: Failed lookup for key [en] and this doesn't really solve the issue where there is multiple fields that have a translation, I can't really see the difference between about in this case name (if it was translated).

How do I go about prepare for a nested field like this and if there is a way, how do I go about differentiating between the two fields? Or is there a better way to do this? This is a related_models definition rather than the document itself if that makes a difference.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source