How to implement full text search in Django?
django, django-queryset, full-text-search, python
Solution
I suggest you to adopt a search engine.
We've used Haystack search, a modular search application for django supporting many search engines (Solr, Xapian, Whoosh, etc...)
Advantages:
- Faster
- perform search queries even without querying the database.
- Highlight searched terms
- "More like this" functionality
- Spelling suggestions
- Better ranking
- etc...
Disadvantages:
- Search Indexes can grow in size pretty fast
- One of the best search engines (Solr) run as a Java servlet (Xapian does not)
We're pretty happy with this solution and it's pretty easy to implement.
Problem
I would like to implement a search function in a django blogging application. The status quo is that I have a list of strings supplied by the user and the queryset is narrowed down by each string to include only those objects that match the string. See: ``` if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): posts = Post.objects.all() for string in form.cleaned_data['query'].split(): posts = posts.filter( Q(title__icontains=string) | Q(text__icontains=string) | Q(tags__name__exact=string) ) return archive_index(request, queryset=posts, date_field='date') ``` Now, what if I didn't want do concatenate each word that is searched for by a logical AND but with a logical OR? How would I do that? Is there a way to do that with Django's own Queryset methods or does one have to fall back to raw SQL queries? In general, is it a proper solution to do full text search like this or would you recommend using a search engine like Solr, Whoosh or Xapian. What are their benefits?