Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuggesterFilterBackend not returning expected SerializerMethod responses. #292

Open
Ameer-Amr opened this issue Jul 15, 2022 · 5 comments
Open
Labels

Comments

@Ameer-Amr
Copy link

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.

@barseghyanartur
Copy link
Owner

Yep. It doesn't yet work with serializers.

@Ameer-Amr
Copy link
Author

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

@barseghyanartur
Copy link
Owner

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

@Ameer-Amr
Copy link
Author

Ameer-Amr commented Jul 16, 2022

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.👍🏻

@barseghyanartur
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants