Skip to content

Commit

Permalink
use Whooshee for full text theses search (#117)
Browse files Browse the repository at this point in the history
* use whooshee for full text search in /theses
* remove name_en from Thesis model indexable list
* add whooshee index dir to gitignore
* reindex only when starting the app
  • Loading branch information
true-real-michael committed May 24, 2024
1 parent 87d23d6 commit 3ebee0d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ src/static/practice/*/*.pdf
src/static/thesis/*/*.pdf

# Do not track fulltext search files
src/msearch/*
src/whooshee/*
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Flask-BasicAuth==0.2.0
Flask-Login==0.6.3
Flask-Markdown==0.3
Flask-Migrate==4.0.7
flask-msearch==0.2.9.4
Flask-SimpleMDE==0.3.0
Flask-Whooshee==0.9.1
Flask-WTF==1.2.1
Frozen-Flask==1.0.2
google-auth-oauthlib==1.2.0
Expand Down
14 changes: 5 additions & 9 deletions src/flask_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
from se_models import (
db,
search,
init_db,
Staff,
Users,
Expand All @@ -37,6 +36,7 @@
DiplomaThemes,
CurrentThesis,
recalculate_post_rank,
whooshee,
)
from flask_se_auth import (
login_manager,
Expand Down Expand Up @@ -377,14 +377,8 @@
# Init Database
db.app = app
db.init_app(app)

app.config["MSEARCH_BACKEND"] = "whoosh"
app.config["MSEARCH_ENABLE"] = True
search.init_app(app)
# search.create_index(Thesis, update=True)
# search.create_index(Users, update=True)
# search.create_index(Thesis, update=True)
# search.create_index(Users, update=True)
app.config["WHOOSHEE_DIR"] = "whooshee"
whooshee.init_app(app)

# Init Migrate
migrate = Migrate(app, db, render_as_batch=True)
Expand Down Expand Up @@ -715,4 +709,6 @@ def sitemap():
with app.app_context():
init_db()
else:
with app.app_context():
whooshee.reindex()
app.run(port=5000, debug=True)
2 changes: 1 addition & 1 deletion src/flask_se_theses.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def fetch_theses():

if search:
records = (
Thesis.query.msearch(search)
Thesis.query.whooshee_search(search)
.filter(Thesis.temporary == False)
.filter(Thesis.publish_year >= startdate)
.filter(Thesis.publish_year <= enddate)
Expand Down
13 changes: 4 additions & 9 deletions src/se_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask import render_template
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from flask_msearch import Search
from flask_whooshee import Whooshee
from werkzeug.security import generate_password_hash
from datetime import datetime

Expand All @@ -35,10 +35,7 @@

metadata = MetaData(naming_convention=convention)
db = SQLAlchemy(metadata=metadata)
# Workaround: flask-msearch does not work with recent Flask-SQLAlchemy.
# Pass 'db' parameter explicitly to mitigate this problem.
search = Search(db=db)

whooshee = Whooshee()

tag = db.Table(
"tag",
Expand Down Expand Up @@ -132,9 +129,8 @@ def __str__(self):
return self.user.get_name()


@whooshee.register_model("first_name", "middle_name", "last_name")
class Users(db.Model, UserMixin):
__searchable__ = ["first_name", "middle_name", "last_name"]

id = db.Column(db.Integer, primary_key=True)

email = db.Column(db.String(255), unique=True, nullable=True)
Expand Down Expand Up @@ -462,9 +458,8 @@ def __repr__(self):
return "<%r>" % (self.name)


@whooshee.register_model("name_ru", "description", "author", "text")
class Thesis(db.Model):
__searchable__ = ["name_ru", "description", "author", "text"]

id = db.Column(db.Integer, primary_key=True)

type_id = db.Column(db.Integer, db.ForeignKey("worktype.id"), nullable=False)
Expand Down

0 comments on commit 3ebee0d

Please sign in to comment.