falcony-io / sqlalchemy-searchable

Fulltext searchable models for SQLAlchemy. Only supports PostgreSQL
Other
263 stars 44 forks source link

Sorting by rank when matching with combined vector #104

Closed tariksetia closed 8 months ago

tariksetia commented 3 years ago

Consider the following example from documentation:

import sqlalchemy as sa
from sqlalchemy_searchable import parse_search_query

search_query = u'matrix'

combined_search_vector = Article.search_vector | Category.search_vector

articles = (
    session.query(Article)
    .join(Category)
    .filter(
        combined_search_vector.match(
            sa.func.tsq_parse(search_query)
        )
    )
)

How do I sort the result by rank?

tariksetia commented 3 years ago

Any help is deeply appreciated.

tariksetia commented 3 years ago

I'm still not able to solve this. @kvesteri, any help is deeply appreciated.

davidjb99 commented 3 years ago

Have you tried to weight them?

https://sqlalchemy-searchable.readthedocs.io/en/latest/configuration.html#weighting-search-results

jpvanhal commented 8 months ago

Use the search function with sort=True parameter to sort the results by rank:

import sqlalchemy as sa

from sqlalchemy_searchable import search

combined_search_vector = Article.search_vector | Category.search_vector
articles = session.scalars(
    search(
        sa.select(Article).join(Category),
        search_query="matrix",
        vector=combined_search_vector,
        sort=True,
    )
).all()