django-elasticsearch-dsl-drf icon indicating copy to clipboard operation
django-elasticsearch-dsl-drf copied to clipboard

SuggesterFilterBackend not returning expected SerializerMethod responses.

Open Ameer-Amr opened this issue 3 years ago • 5 comments

SuggesterFilterBackend not returning SerializerMethod responses. it is perfectly ok with CompoundSearchFilterBackend. but I am facing the issue with suggesters.

Here is my document.py

`@registry.register_document
class BrandDocument(Document):
    code = fields.TextField()
    name = fields.TextField(
        attr='name',
        fields={
            'suggest': fields.Completion(),
        }
    )
    name_ar = fields.TextField(
        attr='name_ar',
        fields={
            'suggest': fields.Completion(),
        }
    )
    brand_name_country = fields.TextField(
        attr='brand_name_country',
        fields={
            'suggest': fields.Completion(),
        }
    )
 .....
`

Here is my serializers.py

`class BrandDocumentSerializer(DocumentSerializer):
    ..........

    brand_name_country = serializers.SerializerMethodField()

    class Meta:
        document = BrandDocument
        fields = (
            'code',
            'name',
            'name_ar',
            'slug',
            'store',
            'label',
            'brand_name_country',
            'primary_category',
            'brand_denomination_range',
            'brand_image_gallery',
            'brand_image_data'
        )

      .................

    def get_brand_name_country(self, obj):
        return "{} {}".format(obj.name, obj.store['country']['name'])
`

And here is my viewset

    class BrandDocumentView(DocumentViewSet):
        document = BrandDocument
        serializer_class = BrandDocumentSerializer
        filter_backends = [
            CompoundSearchFilterBackend,
            FilteringFilterBackend,
            SuggesterFilterBackend,
        ]
        pagination_class = BrandSearchPagination
        # renderer_classes = [JSONRenderer]
        # for specify search fields
        search_fields = {
            'name': {'fuzziness': 'AUTO'},
            'name_ar': {'fuzziness': 'AUTO'},
        }
    
        # Define filtering fields
        filter_fields = {
            'store': 'store.country.code'
        }
    
        # specify fields for suggesters with auto-completion
        suggester_fields = {
            'name_suggest': {
                'field': 'name.suggest',
                'suggesters': [
                    SUGGESTER_COMPLETION,
                ],
            },
            'name_ar_suggest': {
                'field': 'name_ar.suggest',
                'suggesters': [
                    SUGGESTER_COMPLETION,
                ],
            },
            'brand_name_country_suggest': {
                'field': 'brand_name_country.suggest',
                'suggesters': [
                    SUGGESTER_COMPLETION,
                ],
                'serializer_field': 'brand_name_country',
            },
        }

When I make a search it perfectly shows the result. like this:

Screenshot 2022-07-15 at 12 31 40 PM

but in suggestion i did't get the actual response:

Screenshot 2022-07-15 at 12 35 53 PM

look here, suggester response data are not the same as search response data. in search 'brand_name_country' return its value. but in suggestion completion, it returns Null. and also in suggesters, it didn't return the images' absolute url's.

please have a look at this guys.

Ameer-Amr avatar Jul 15 '22 08:07 Ameer-Amr

Yep. It doesn't yet work with serializers.

barseghyanartur avatar Jul 15 '22 11:07 barseghyanartur

Oh.. could you please advise me if there is any hope for doing the same. or there is any way to pass params with SuggesterFilterBackend API..?

suppose I want to search for suggestions with 'amazon' in 'UAE' country, is that possible? ...suggest/?name_suggest__completion=amazon&country=ae

Ameer-Amr avatar Jul 16 '22 05:07 Ameer-Amr

Sure. Look, suggester backend comes from a view mix-in. You override it and fix it as you want.

barseghyanartur avatar Jul 16 '22 06:07 barseghyanartur

Sure. Look, suggester backend comes from a view mix-in. You override it and fix it as you want.

Ok, thank you for your quick reply. I think I can manage my requirement with category context suggesters. thank you for the great package.👍🏻

Ameer-Amr avatar Jul 16 '22 09:07 Ameer-Amr

Yep, they are also supported. Check some examples here.

barseghyanartur avatar Jul 16 '22 21:07 barseghyanartur