Search code examples
djangofull-text-search

Django text search with partial sentence match


I am building a site in which I want to implement text search for the title and description of some objects. Since I will have little amount of objects (~500 documents) I am not considering Haystack and the such.

I only need 2 features:

  • Be able to prioritize matches on the title over the description (with some kind of weight).
  • Allow partial match of the sentence. For example, if I search for 'ice cream', get also the results for 'ice' and 'cream'.

I have looked into django-watson and django-full-text-search but I am not sure if they allow partial matching. Any ideas?


Solution

  • How many hits by second have your site? Each document, how many data stores?

    If we are talking about 500 docs and few hits by minute perhaps django api is enough:

    q = None
    for word in search_string.split():
       q_aux = Q( title__icontains = word ) | Q( description__icontains = word )
       q = ( q_aux & q ) if bool( q ) else q_aux
    
    result = Document.objects.filter( q ) 
    

    You ever considered this option?

    Be careful:

    • This approach don't priorize title over description
    • Only "all words" matches appear in results.