Get ElasticSearch suggestion based on filtered data in Django

50 Views Asked by At

I am using django-elasticsearch-dsl for integrating elastic search. I have an API that returns name suggestions using elastic search suggest feature.

It's working as expected but the issue is I can't apply a specific filter. For example, I want to get suggestions for the published products only.

#Document
class ProductDocument(Document):
status = fields.IntegerField()
meta = fields.ObjectField(
    properties={
        "is_published": fields.BooleanField(),
        "full_name": fields.TextField(
            analyzer=html_strip,
            fields={
                "raw": fields.KeywordField(),
                "suggest": fields.Completion(),
            }
        ),
    },
)

search = ProductDocument.search()
search = search.filter("match", meta__is_published=True)
completion = {
    'field': "meta.full_name.suggest",
    'size': 10,
    'fuzzy': {'fuzziness': "auto:4,8"},
}
suggest = search.suggest("auto_complete","abc", completion=completion)

But it return products with is_published=False. Is there any way to achieve this?

0

There are 0 best solutions below