Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 582
Alex Avatar asked Jul 21 '12 16:07

Alex


2 Answers

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.
like image 110
dani herrera Avatar answered Nov 18 '22 12:11

dani herrera


As the creator of django-watson, I can confirm that, with some database backends, it allows partial matches. Specifically, on MySQL and PostgreSQL, it allows prefix matching, which is a partial match from the beginning of a word.

Check out this database comparison page on the wiki:

https://github.com/etianen/django-watson/wiki/Database-support

like image 3
Dave Avatar answered Nov 18 '22 10:11

Dave