Question

I am building a Note Making application where users can create notes, add photos, tag and share it with friends. I am using python flask framework for building the tool.

I want to provide a feature wherein user can search through their notes. How do I go about building the index, are there any libraries which can do this quickly ?

I am using MongoDB as a backend DB

Was it helpful?

Solution

Have you tried using Flask-WhooshAlchemy.

Flask-WhooshAlchemy is a Flask extension that integrates the text-search functionality of Whoosh with the ORM of SQLAlchemy for use in Flask applications.

Install

pip install flask_whooshalchemy

Quickstart

import flask.ext.whooshalchemy

# set the location for the whoosh index
app.config['WHOOSH_BASE'] = 'path/to/whoosh/base'


class BlogPost(db.Model):
  __tablename__ = 'blogpost'
  __searchable__ = ['title', 'content']  # these fields will be indexed by whoosh

  id = app.db.Column(app.db.Integer, primary_key=True)
  title = app.db.Column(app.db.Text)
  content = app.db.Column(app.db.Text)
  created = db.Column(db.DateTime, default=datetime.datetime.utcnow)

  def __repr__(self):
     return '{0}(title={1})'.format(self.__class__.__name__, self.title)

Create a post

db.session.add(
    BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()

Text searching

results = BlogPost.query.whoosh_search('cool')

We can limit results:

# get 2 best results:
results = BlogPost.query.whoosh_search('cool', limit=2)

source: Flask-WhooshAlchemy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top