Example #1
0
def search(query, search_query, vector=None, regconfig=None, sort=False):
    """
    Search given query with full text search.

    :param search_query: the search query
    :param vector: search vector to use
    :param regconfig: postgresql regconfig to be used
    :param sort: order results by relevance (quality of hit)
    """
    if not search_query.strip():
        return query

    if vector is None:
        entity = query._entities[0].entity_zero.class_
        search_vectors = inspect_search_vectors(entity)
        vector = search_vectors[0]

    if regconfig is None:
        regconfig = search_manager.options["regconfig"]

    query = query.filter(
        vector.op("@@")(func.tsq_parse(regconfig, search_query)))
    if sort:
        query = query.order_by(
            desc(func.ts_rank_cd(vector, func.tsq_parse(search_query))))

    return query.params(term=search_query)
Example #2
0
def search(*, query_str: str, query: Query, model: str, sort=False):
    """Perform a search based on the query."""
    search_model = get_class_by_tablename(model)

    if not query_str.strip():
        return query

    vector = search_model.search_vector

    query = query.filter(vector.op("@@")(func.tsq_parse(query_str)))
    if sort:
        query = query.order_by(
            desc(func.ts_rank_cd(vector, func.tsq_parse(query_str))))

    return query.params(term=query_str)
Example #3
0
def search(*, db_session, search_query: str, model: str, sort=False):
    """Perform a search based on the query."""
    search_model = get_class_by_tablename(model)
    query = db_session.query(search_model)

    if not search_query.strip():
        return query

    vector = search_model.search_vector

    query = query.filter(vector.op("@@")(func.tsq_parse(search_query)))
    if sort:
        query = query.order_by(
            desc(func.ts_rank_cd(vector, func.tsq_parse(search_query))))

    return query.params(term=search_query)
Example #4
0
def search_products_by_param(search_query: str,
                             product_id: int = None,
                             category_id: int = None) -> list or None:
    vector = inspect_search_vectors(Product)[0]
    try:
        result = db.session.query(Product).filter(
            Product.search_vector.match(search_query))
    except exc.ProgrammingError:
        return None
    if product_id:
        result = result.filter_by(producer_id=product_id)
    if category_id:
        result = result.filter_by(category_id=category_id)
    return result.order_by(
        desc(func.ts_rank_cd(vector, func.tsq_parse(search_query))))