How to implement full text search in Django?

Posted by Jannis on Stack Overflow See other posts from Stack Overflow or by Jannis
Published on 2010-03-17T10:09:44Z Indexed on 2010/03/17 10:11 UTC
Read the original article Hit count: 582

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 there benefits?

Thanks for taking the time

© Stack Overflow or respective owner

Related posts about python

Related posts about django