Skip to content
This repository was archived by the owner on May 20, 2018. It is now read-only.

Commit 7649f79

Browse files
author
Lev Lazinskiy
committed
Added Full Text Search to Note model
1 parent f32afa3 commit 7649f79

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

app/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
from datetime import datetime
99
from app.exceptions import ValidationError
1010

11+
# Full Text Search
12+
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
13+
from sqlalchemy_searchable import SearchQueryMixin
14+
from sqlalchemy_searchable import make_searchable
15+
from sqlalchemy_utils.types import TSVectorType
16+
1117
import bleach
1218
import hashlib
1319

20+
make_searchable()
21+
1422
@login_manager.user_loader
1523
def load_user(user_id):
1624
return User.query.get(int(user_id))
@@ -179,7 +187,11 @@ def to_json(self):
179187
def __repr__(self):
180188
return '<User {0}>'.format(self.username)
181189

190+
class NoteQuery(BaseQuery, SearchQueryMixin):
191+
pass
192+
182193
class Note(db.Model):
194+
query_class = NoteQuery
183195
__tablename__ = 'notes'
184196
id = db.Column(db.Integer, primary_key=True)
185197
title = db.Column(db.String(200))
@@ -192,6 +204,9 @@ class Note(db.Model):
192204

193205
tags = db.relationship("Tag", secondary=note_tag, backref="Note", passive_deletes=True)
194206

207+
# Full Text Search
208+
search_vector = db.Column(TSVectorType('title', 'body'))
209+
195210
def to_json(self):
196211
json_note = {
197212
'url': url_for('api.get_note', id=self.id, _external=True),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Add Full Text Search
2+
3+
Revision ID: 488e3dae5a17
4+
Revises: 34fa673d7905
5+
Create Date: 2015-12-07 10:49:44.124657
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = '488e3dae5a17'
11+
down_revision = '34fa673d7905'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
import sqlalchemy_utils
16+
from sqlalchemy_searchable import sync_trigger
17+
18+
def upgrade():
19+
### commands auto generated by Alembic - please adjust! ###
20+
conn = op.get_bind()
21+
op.drop_table('sections')
22+
op.add_column('notes', sa.Column('search_vector', sqlalchemy_utils.types.ts_vector.TSVectorType(), nullable=True))
23+
op.create_index('ix_notes_search_vector', 'notes', ['search_vector'], unique=False, postgresql_using='gin')
24+
### end Alembic commands ###
25+
sync_trigger(conn, 'notes', 'search_vector', ['title', 'body'])
26+
27+
def downgrade():
28+
### commands auto generated by Alembic - please adjust! ###
29+
op.drop_index('ix_notes_search_vector', table_name='notes')
30+
op.drop_column('notes', 'search_vector')
31+
op.create_table('sections',
32+
sa.Column('id', sa.INTEGER(), nullable=False),
33+
sa.Column('title', sa.VARCHAR(length=200), autoincrement=False, nullable=True),
34+
sa.Column('notebook_id', sa.INTEGER(), autoincrement=False, nullable=True),
35+
sa.ForeignKeyConstraint(['notebook_id'], [u'notebooks.id'], name=u'sections_notebook_id_fkey'),
36+
sa.PrimaryKeyConstraint('id', name=u'sections_pkey')
37+
)
38+
### end Alembic commands ###

0 commit comments

Comments
 (0)