Hello. I'm trying to writing something to generate full text searches
for postgres. Here's the function I've got so far:
from sqlalchemy import sql
import operator
def full_text(fields, text):
def alternate(items):
for i in items[:-1]:
yield i
yield sql.text(" ' ' ")
yield items[-1]
return "%s @@ to_tsquery('%s')" % (
sql.func.to_tsvector(
reduce(operator.add, alternate(fields))
), text)
You pass it a list of columns to match against and a full-text string
to search with, and it returns a string that you can use in a filter()
clause. It works fine if you only pass one or two column names--any
more than that, and it dies. At first, I thought it was something
wrong with my generator, but it turns out there's something wrong with
the way I'm concatenating columns and raw text--that, or there's a bug
there. Below is a simple example of what goes wrong.
from sqlalchemy import *
table1 = Table('table1', MetaData(),
Column('col1', String()),
Column('col2', String()),
Column('col3', String()),
)
#these work
print table1.c.col1 + table1.c.col2 + table1.c.col3
print table1.c.col1 + text('sdf') + table1.c.col2 + table1.c.col3
print table1.c.col1 + text('sdf') + table1.c.col2
#these don't
print table1.c.col1 + table1.c.col2 + text('sdf')
print table1.c.col1 + table1.c.col2 + table1.c.col3 + text('sdf')
print table1.c.col1 + text('sdf') + table1.c.col2 + text('sdf')
print table1.c.col1 + text('sdf') + text('sdf')
The ones that don't work die with "AttributeError: 'NoneType' object
has no attribute 'adapt_operator'" on line 1328 in sqlachemy/sql/
expression.py
Is this a bug, or am I doing this wrong? And all that aside, is there
an existing way to generate a full-text search like that?
Thanks,
Jeff
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---