Below are some thoughts on how to do the above efficiently:
====================================================

1) Create a temporary table (or Model) with two fields

       Class Temp(.......):
             personid = FK(Person)
             searchstring =  TextField()


2)  When every a person is  saved you will embed a string into the
above table as follows
          =======>  (personid,personname)
    whenever a album is stored you modify the search string as follows
           ===========> (personid,personname + albumname )==>(the same
row which has foriegn key to person)
    whenever a Song is saved you modify the search string as follows
        ==============> (personid, personname + albumname + songname)

 2a)   Basciall if any node is added or updated or deleted we will
form a search string by concatenating the search strings of every node
  until the top and store it  into the Temp table /models , we also
associate the search string with person as this is what we really
want.

2b) To do the above you should rely on Django signals


3) Now define a search function on top TEMP table
      TempTable.objects.filter(searchstring__icontains=search_string)

4) IT may take up bit of memory but it will be superfast.

5) Instead of temporary table you can replace it with simple/basic
full text search engines like
     (whoosh) advanced ones like (solr,sphinx etc............)



--rama


















On Sep 22, 7:32 pm, Jeff Gentry <[email protected]> wrote:
> Hello ...
>
> I have a tree like structure created out of models using ForeignKey
> linkages.  As an example:
>
> Model Person:
>    name = CharField
>
> Model Book:
>    name = CharField
>    author = FK(Person)
>
> Model Movie:
>    name = CharField
>    director = FK(Person)
>
> Model Album:
>    name = CharField
>    director = FK(Person)
>
> Model Chapter:
>    name = CharField
>    book = FK(Book)
>
> Model Scene:
>    name = CharField
>    movie = FK(Movie)
>
> Model Song:
>    name = CharField
>    album = FK(Album)
>
> The caveat here is the real structure is both deeper and broader, and a
> node might have multiple non-FK fields (ie not just 'name').
>
> What I'd like to do is have a search function such that there is a string
> supplied which will return any Person that either matches a Person object
> itself, or a field in any of the child nodes.  IOW, if "beat it" is the
> string, and the name of a song associated with an album associated w/ the
> person matches, the person will be returned.
>
> What I've done so far is the following:
>
> For any leaf node, have a Manager object w/ a search method which does
> something like:
>    return Song.objects.filter(name__icontains=search_string)
>
> Then for the root node (Person) and any interior nodes, there is also a
> Manager object w/ a search() method which looks something like:
>     class AlbumManager(models.Model):
>         def search(self, search_string):
>            from_songs = 
> Album.objects.filter(song__in=Song.objects.search(search_string))
>            return 
> Album.objects.filter(name__icontains=search_string)|from_songs
>
> As you might imagine, once you get to the root node, this unleashes a
> massive number of queries and is really inefficient.  I'd be pretty
> surprised if there wasn't a better way to do this ... one idea is to just
> have one search() method at the top of the tree that manually searches
> through everything, but a) that seems very messy (albeit probably more
> efficient) and b) it would be nice to be able to search individual nodes
> arbitrarily.
>
> So with all of this said, what would be a more efficient method of getting
> where I want to be instead of my bonehead method here?
>
> Thanks
> -J
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to