Fix broken search weighting

Fixes #176
This commit is contained in:
rubenwardy 2020-01-18 17:54:46 +00:00
parent f4209d7a67
commit 5a496f6858
3 changed files with 38 additions and 6 deletions

@ -24,8 +24,7 @@ from flask import Flask, url_for
from flask_sqlalchemy import SQLAlchemy, BaseQuery from flask_sqlalchemy import SQLAlchemy, BaseQuery
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter
from sqlalchemy import func from sqlalchemy import func, CheckConstraint
from sqlalchemy.orm import validates
from sqlalchemy_searchable import SearchQueryMixin from sqlalchemy_searchable import SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable from sqlalchemy_searchable import make_searchable
@ -310,7 +309,7 @@ class Dependency(db.Model):
package = db.relationship("Package", foreign_keys=[package_id]) package = db.relationship("Package", foreign_keys=[package_id])
meta_package_id = db.Column(db.Integer, db.ForeignKey("meta_package.id"), nullable=True) meta_package_id = db.Column(db.Integer, db.ForeignKey("meta_package.id"), nullable=True)
optional = db.Column(db.Boolean, nullable=False, default=False) optional = db.Column(db.Boolean, nullable=False, default=False)
__table_args__ = (db.UniqueConstraint('depender_id', 'package_id', 'meta_package_id', name='_dependency_uc'), ) __table_args__ = (db.UniqueConstraint("depender_id", "package_id", "meta_package_id", name="_dependency_uc"), )
def __init__(self, depender=None, package=None, meta=None): def __init__(self, depender=None, package=None, meta=None):
if depender is None: if depender is None:
@ -377,14 +376,17 @@ class Package(db.Model):
# Basic details # Basic details
author_id = db.Column(db.Integer, db.ForeignKey("user.id")) author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
name = db.Column(db.String(100), nullable=False) name = db.Column(db.Unicode(100), nullable=False)
title = db.Column(db.Unicode(100), nullable=False) title = db.Column(db.Unicode(100), nullable=False)
short_desc = db.Column(db.Unicode(200), nullable=False) short_desc = db.Column(db.Unicode(200), nullable=False)
desc = db.Column(db.UnicodeText, nullable=True) desc = db.Column(db.UnicodeText, nullable=True)
type = db.Column(db.Enum(PackageType)) type = db.Column(db.Enum(PackageType))
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
search_vector = db.Column(TSVectorType("title", "short_desc", "desc")) name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$'")
search_vector = db.Column(TSVectorType("title", "short_desc", "desc", \
weights={ "title": "A", "short_desc": "B", "desc": "C" }))
license_id = db.Column(db.Integer, db.ForeignKey("license.id"), nullable=False, default=1) license_id = db.Column(db.Integer, db.ForeignKey("license.id"), nullable=False, default=1)
license = db.relationship("License", foreign_keys=[license_id]) license = db.relationship("License", foreign_keys=[license_id])

@ -62,7 +62,7 @@ class QueryBuilder:
query = query.filter(Package.type.in_(self.types)) query = query.filter(Package.type.in_(self.types))
if self.search: if self.search:
query = query.search(self.search) query = query.search(self.search, sort=True)
if self.random: if self.random:
query = query.order_by(func.random()) query = query.order_by(func.random())

@ -0,0 +1,30 @@
"""empty message
Revision ID: 6dca6eceb04d
Revises: fd25bf3e57c3
Create Date: 2020-01-18 17:32:21.885068
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy_searchable import sync_trigger
# revision identifiers, used by Alembic.
revision = '6dca6eceb04d'
down_revision = 'fd25bf3e57c3'
branch_labels = None
depends_on = None
def upgrade():
conn = op.get_bind()
sync_trigger(conn, 'package', 'search_vector', ["name", "title", "short_desc", "desc"])
op.create_check_constraint("name_valid", "package", "name ~* '^[a-z0-9_]+$'")
def downgrade():
conn = op.get_bind()
sync_trigger(conn, 'package', 'search_vector', ["title", "short_desc", "desc"])
op.drop_constraint("name_valid", "package", type_="check")