I've been a while trying to implement full-text search under
PostgreSQL, I created some code that works, but is not optimal.
I asked Malcolm Tredinnick and he told me:
"In fact, thinking about this a bit more, it might even be possible to
do
it all via a custom Q-like object. You might not need to do any
QuerySet/Query modifications. You can create a class that is passed to
filter() and exclude() calls, similar to how Q() is done now. That
class
really only has to support an add_to_query() method -- see the
Query.add_q() method to see how it's called. When add_to_query() is
called on your object, you have full access to the underlying Query
instance, so you can add table joins, add things to the where clause,
do
whatever you want."
But I can't do it, so ask for your help.
The code I have now is:
from django.db.models.sql.query import Query
from django.db import connection
class nQuery( Query ):
def get_from_clause(self):
r = super( nQuery, self).get_from_clause()
r[0].append(r", plainto_tsquery(%s) as query" )
r[1].append(r"'%s'" % (palabras,) )
return r
def full_text( self ):
select={
'headline': "ts_headline( %s, query,'StartSel
= <strong>, StopSel = </strong>')" % ( 'content', ),
'rank': "ts_rank_cd(tsv, query, 32)",
}
self.add_extra( select,None,('tsv @@
query',),None,None,None )
q = nQuery( MyModel, connection )
q.full_text()
from django.db.models.query import QuerySet
items = QuerySet( MyModel, q ).order_by('-rank')
So what I want now is to create the custom Q-like object and get rid
of the previous code.
This Q object should have an add_to_query() method, where I have
access to the Query instance and I can modify everything.
Here is where the problem comes. I cant get an sql clause like this:
SELECT *, ts_headline( content_field, query,'StartSel = <strong>,
StopSel = </strong>')" ) as headline, ts_rank_cd(tsv, query, 32) as
rank FROM table_name,plainto_tsquery('words I want to search') as
query WHERE tsv@@ query.
Once this custom Q object is created, then we can use it like this:
FT( words='words I want to search' )
optionally we can pass parameters to retrieve the headline or not.
I'm completly stuck with this. I hope someone here can help me.
Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---