drf-haystack icon indicating copy to clipboard operation
drf-haystack copied to clipboard

How to retrieve spelling suggestions in the serializer?

Open sparkplugmarcus opened this issue 9 years ago • 8 comments

I would like to return spelling suggestions in my serializer. How would I do that? Thanks

Haystack method: http://django-haystack.readthedocs.io/en/latest/searchqueryset_api.html?#SearchQuerySet.spelling_suggestion

sparkplugmarcus avatar Nov 22 '16 03:11 sparkplugmarcus

Hi. Support for spelling suggestion is not currently implemented. It's probably quite easy to add support for, so I'll see what I can do.

rhblind avatar Nov 22 '16 07:11 rhblind

Any chance there is an update on this?

sparkplugmarcus avatar Jan 06 '17 23:01 sparkplugmarcus

Hi, I have started looking into it, but I got busy with other work so it is currently a bit on hold. I'll see if I have som time soon-ish..

rhblind avatar Jan 08 '17 09:01 rhblind

Any news on this?

ChadTaljaardt avatar Jul 03 '17 12:07 ChadTaljaardt

@rhblind any pointer? I am also looking for same, if you are busy I can add it. please share if you have already done an analysis of it.

jpatel3 avatar Jul 05 '17 18:07 jpatel3

@jpatel3 have you made any progress on this implementation?

sparkplugmarcus avatar Aug 11 '17 20:08 sparkplugmarcus

@pymarco I was using ListAPIView, so I did override on list method to add a suggestion in it if any.

def list(self, request, *args, **kwargs):
    response = super(SearchList, self).list(request, args, kwargs)

    q = request.query_params.get('q', None)
    suggestion_query = SearchQuerySet().auto_query(q.strip())

    if suggestion_query.count() == 0:
        suggestion = suggestion_query.spelling_suggestion()
        if suggestion:
            suggestion = re.sub(r"[\(.*?\)]", "", suggestion)

        response.data['suggestion'] = suggestion

    response.data['searchQuery'] = q.strip()

    return response

Where suggestion is spelling suggestion, and searchQuery is actual query. The reason why I extracted the value using regular expression is that the spelling suggestion string was coming in bracket e.g. "(government)".

Hope it helps!

jpatel3 avatar Aug 14 '17 13:08 jpatel3

Hello guys. Sorry for not replying, been insanely busy lately.

I did start working on this, but didn't get very far. I started by adding this as a filter feature, but I think that's going down the wrong path. I quickly ran into issues where spelling suggestions duplicated themselves (probably because of stacked filters or something), and didn't have any more time investigating.

If anybody find a clean and generic way of implementing this, I'm always open of merging pull requests!

rhblind avatar Aug 14 '17 19:08 rhblind