Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sorting query by rank ascending. #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions sqlalchemy_searchable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .parser import SearchQueryParser
from .vectorizers import Vectorizer

__version__ = '0.9.2'
__version__ = '0.9.3'


parser = SearchQueryParser()
Expand Down Expand Up @@ -64,7 +64,7 @@ def inspect_search_vectors(entity):
]


def search(query, search_query, vector=None, regconfig=None, sort=False):
def search(query, search_query, vector=None, regconfig=None, sort=False, desc=True):
"""
Search given query with full text search.

Expand All @@ -91,15 +91,17 @@ def search(query, search_query, vector=None, regconfig=None, sort=False):

query = query.filter(vector.match(search_query, **kwargs))
if sort:
query = query.order_by(
sa.desc(
sa.func.ts_rank_cd(
vector,
sa.func.to_tsquery(search_query)
)
)
rank_clause = sa.func.ts_rank_cd(
vector,
sa.func.to_tsquery(search_query)
)

if desc:
query = query.order_by(sa.desc(rank_clause))

else:
query = query.order_by(sa.asc(rank_clause))

return query.params(term=search_query)


Expand Down