Forked from davebarkerxyz/rebuild_search_indices.py
Last active
May 21, 2018 23:55
-
-
Save estasney/343a17819da6c86288273b2b84b9ef48 to your computer and use it in GitHub Desktop.
Rebuild Flask-WhooshAlchemy search indices (Python 3, Mega-Tutorial Style)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
import sys | |
sys.path.append("mysite") | |
from app_folder import create_app | |
from app_folder.models import YOUR_MODEL | |
app = create_app() | |
app.app_context().push() | |
import flask_whooshalchemy | |
""" | |
Rebuild all Whoosh search indices | |
If this is intended as a full rebuild, you should consider deleting the | |
Whoosh search database (as specified in app.config["WHOOSH_BASE"]) | |
before running the rebuild. This will ensure that no old/stale | |
data is left in the search indices (this process doesn't delete removed | |
data, only recreated search entries for current data). | |
""" | |
program_start = datetime.datetime.utcnow() | |
def log(message): | |
logtime = datetime.datetime.utcnow() | |
logdiff = logtime - program_start | |
print("{0} (+{1:.3f}): {2}".format(logtime.strftime("%Y-%m-%d %H:%M:%S"), | |
logdiff.total_seconds(), | |
message)) | |
def rebuild_index(model): | |
"""Rebuild search index of Flask-SQLAlchemy model""" | |
log("Rebuilding {0} index...".format(model.__name__)) | |
primary_field = model.pure_whoosh.primary_key_name | |
searchables = model.__searchable__ | |
index_writer = flask_whooshalchemy.whoosh_index(app, model) | |
# Fetch all data | |
entries = model.query.all() | |
entry_count = 0 | |
with index_writer.writer() as writer: | |
for entry in entries: | |
index_attrs = {} | |
for field in searchables: | |
index_attrs[field] = str(getattr(entry, field)) | |
index_attrs[primary_field] = str(getattr(entry, primary_field)) | |
writer.update_document(**index_attrs) | |
entry_count += 1 | |
log("Rebuilt {0} {1} search index entries.".format(str(entry_count), model.__name__)) | |
if __name__ == "__main__": | |
model_list = [YOUR_MODEL] | |
for model in model_list: | |
rebuild_index(model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment