Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Full Text Search index with SQLAlchemy on PostgreSQL

Tags:

I need to create a PostgreSQL Full Text Search index in Python with SQLAlchemy. Here's what I want in SQL:

CREATE TABLE person ( id INTEGER PRIMARY KEY, name TEXT ); CREATE INDEX person_idx ON person USING GIN (to_tsvector('simple', name)); 

Now how do I do the second part with SQLAlchemy when using the ORM:

class Person(db.Model):     id = db.Column(db.Integer, primary_key=True)     name = db.Column(db.String) 
like image 522
Markus Meskanen Avatar asked Feb 22 '17 10:02

Markus Meskanen


People also ask

How do I use full text search in PostgreSQL?

In PostgreSQL, you use two functions to perform Full Text Search. They are to_tsvector() and to_tsquery(). Let's see how they work and to use them first. to_tsvector() function breaks up the input string and creates tokens out of it, which is then used to perform Full Text Search using the to_tsquery() function.

Is PostgreSQL good for full text search?

Yes, You Can Keep Full-Text Search in Postgres You can get even deeper and make your Postgres full-text search even more robust, by implementing features such as highlighting results, or writing your own custom dictionaries or functions.

How do I do a full text search?

Go to any cluster and select the “Search” tab to do so. From there, you can click on “Create Search Index” to launch the process. Once the index is created, you can use the $search operator to perform full-text searches.

Can I use SQLAlchemy with PostgreSQL?

This SQLAlchemy engine is a global object which can be created and configured once and use the same engine object multiple times for different operations. The first step in establishing a connection with the PostgreSQL database is creating an engine object using the create_engine() function of SQLAlchemy.


1 Answers

You could create index using Index in __table_args__. Also I use a function to create ts_vector to make it more tidy and reusable if more than one field is required. Something like below:

from sqlalchemy.dialects import postgresql  def create_tsvector(*args):     exp = args[0]     for e in args[1:]:         exp += ' ' + e     return func.to_tsvector('english', exp)  class Person(db.Model):     id = db.Column(db.Integer, primary_key=True)     name = db.Column(db.String)      __ts_vector__ = create_tsvector(         cast(func.coalesce(name, ''), postgresql.TEXT)     )      __table_args__ = (         Index(             'idx_person_fts',             __ts_vector__,             postgresql_using='gin'         )     ) 

Update: A sample query using index (corrected based on comments):

people = Person.query.filter(Person.__ts_vector__.match(expressions, postgresql_regconfig='english')).all() 
like image 53
sharez Avatar answered Sep 22 '22 01:09

sharez