Skip to main content

Whoosh indexing capabilities for Flask-SQLAlchemy, Python 3 compatibility fork.

Project description

Flask-WhooshAlchemy3

PyPI version license

Whoosh indexing capabilities for Flask-SQLAlchemy, Python 3 compatibility fork. Performance improvements and suggestions are readily welcome.

Inspired from gyllstromk's Flask-WhooshAlchemy.

Install

$ pip install flask-whooshalchemy3

..alternatively from source,

$ pip install git+git://github.com/blakev/Flask-WhooshAlchemy3.git@master

Quickstart

from datetime import datetime

import flask_sqlalchemy
import flask_whooshalchemy3
from whoosh.analysis import StemmingAnalyzer

db = flask_sqlalchemy.SQLAlchemy()


class BlogPost(db.Model):
    __tablename__ = 'posts'
    __searchable__ = ['title', 'content', 'summary']  # indexed fields
    __analyzer__ = StemmingAnalyzer()

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    content = db.Column(db.Text(32 * 1024))
    summary = db.Column(db.String(1024))
    created = db.Column(db.DateTime, default=datetime.utcnow)

Committing model instances to the session will write or update the Whoosh index.

db.session.add(BlogPost(title='First Post!', content='This is awesome.'))
db.session.commit()

Searching is done via Model.query.search(..). However, the request must be done within the Flask request context otherwise the database connection may not be established.

@app.route('/posts')
def posts():
    num_posts = min(request.args.get('limit', 10), 50)
    query = request.args.get('q', '')
    results = BlogPost.query.search(query, limit=num_posts)

Results are ordered by Whoosh's ranking-algorithm, but can be overwritten with SQLAlchemy .order_by.

yesterday = datetime.utcnow() - timedelta(days=1)
results = BlogPost.query
            .filter(BlogPost.created > yesterday)
            .search('first')
            .order_by(desc(BlogPost.created))

Flask Configuration

WHOOSH_ANALYZER (whoosh.Analyzer)

WHOOSH_INDEX_PATH (str)

  • File path to where the text indexes will be saved.
  • Default: {cwd}/.indexes/*

WHOOSH_INDEXING_CPUS (int)

  • The number of system processes to spawn for indexing new and modified documents.
  • Default: 2

WHOOSH_INDEXING_RAM (int)

  • The amount of RAM, in megabytes, to reserve per indexing process for data processing.
  • Default: 256

WHOOSH_RAM_CACHE (bool)

  • Allows common queries and their fields to be stored in cache, in RAM.
  • Default: False

License

MIT License

Copyright (c) 2017 Blake VandeMerwe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-whooshalchemy3-0.3.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

flask_whooshalchemy3-0.3.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-whooshalchemy3-0.3.0.tar.gz.

File metadata

  • Download URL: flask-whooshalchemy3-0.3.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.11 Linux/5.10.56-1-lts

File hashes

Hashes for flask-whooshalchemy3-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4fcbc46b0d2980dd783fa50ad67917838ec5b954cd0d74c177b779d0412e4eac
MD5 473e858f849d58a6d75fbe8246ca3295
BLAKE2b-256 30bf24f09a3be51fe457a8f9962f0cd69ec2d1449371e9df062f46e7a2a208df

See more details on using hashes here.

File details

Details for the file flask_whooshalchemy3-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_whooshalchemy3-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 141f846fd79a6cb5f1fbcb766db6a2faad753facedefb1c6ef5dac3c5b4011cb
MD5 c5e0f649f60f946c983ea815a8b42c20
BLAKE2b-256 a720a3bf33475756cca5933668d6e288306e4ffeff76c290ca70a804b2abac1d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page