2
I am creating a search system on my platform. I do a search for the title of the content, description and tags within it. Follow the code:
questions = Question.objects.filter(reduce(lambda x, y: x | y, [(Q(name__icontains=word) | Q(description__icontains=word) | Q(tags__name__icontains=word)) for word in words]))
That way, I can break the words of the text and do a really cool search. The problem is, I’d like to sort by a priority that I create, in case, wanted to do in the following order:
- Exact result
- Result starting with word/phrase
- Result ending with word/phrase
- Result containing in word/phrase
This way I will display first the result which is exactly what the user looks for, then results that start with what he searches for, finish and which contains.
But with my code, I can’t see a way to do that sql pure using case when.