KennyPowers
KennyPowers

Reputation: 5025

SQLAlchemy Match Against clause

I have MySQL table with full-text index. I want to know how to use MATCH ... AGAINST() with SQLAlchemy. Do you have some examples?

...
    trs = dbsession.query(Trans).filter(Trans.client_id==user_id)
    if search and search != '':
        trs = trs.filter(or_(Trans.transcribe.like('%'+search+'%'), Trans.lang_name.like('%'+search+'%')))
...

I want to replace LIKE with 'MATCH/AGAINST IN BOOLEAN MODE' construction but I don't know how

Upvotes: 5

Views: 2317

Answers (1)

Pedro Romano
Pedro Romano

Reputation: 11213

You should simply need to use the clause:

Trans.lang_name.match(str_to_match)

where str_to_match is the string you want to match.

This compiles (for the MySQL dialects) to (assuming the Trans model table name is trans and the model attributes match the column names):

'MATCH(trans.lang_name) AGAINST (%s IN BOOLEAN MODE)' % str_to_match

Upvotes: 5

Related Questions