On Wed, 2009-03-04 at 05:01 -0800, koranthala wrote:
> Hi,
>      How do I implement a substring query?
>      Please find the example below.
> From Django Documentation:
>   Entry.objects.get(headline__icontains='Lennon')
>   => This matches the headline 'Today lennon honoured'
> 
> My query is the opposite:
>    I have the string 'Lennon' inside my DB, and I have to match 'Today
> Lennon honoured'
> Say:
> Entry.objects.get(headline__isubstring='Today Lennon Honoured')
> should return headline 'Lennon'.
> 
>     I checked iregex too, but I cannot seem to solve it myself. If
> somebody has solved this, could you please help me out.

This isn't the type of searching operating that is going to be standard
outside of specialised packages. It's actually a really hard problem
(not impossible, there are solutions, but hard) in general. The
complexity comes from the fact that the match could start anywhere and
involve any number of characters. So to do this efficiently requires
some special datastructures (with suffix trees and some other
structures, you can the search in about O(total length of text)).

Now, I'm sure it wouldn't be impossible to write, say, an extension for,
say, PostgreSQL (picking that database because of its excellent support
for extensions) that supported this kind of searching, but it would be a
fair bit of work. It would require a particular sort of index structure
to support the searches. I don't know any package off the top of my head
that does this for databases. Might be a fun project for somebody with a
bit of time and curiosity.

I've done this kind of thing in the past for a couple of clients and
I've always pulled the text to be searched into memory. Create a data
structure of all the headlines to be searched (using your example) and
then match the search string against them to find the longest common
substring match. There's a lot of literature on this sort of stuff, and
searching for things like "longest common substring" will give you a
place to start.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to