def _search_query_text(query, **kwargs): """Prepare the initial query, searching by text and ranking by the proximity.""" language = kwargs.get('language') text = kwargs.get('text') if text: if language is not None: to_tsquery = func.to_tsquery(SearchItem.getDictForLanguage(language), text) else: to_tsquery = func.to_tsquery(text) query = query.filter(SearchItem.terms.op('@@')(to_tsquery)) return query
def _search_query_rank(query, **kwargs): """ Rank query results, sorting by search rank for most content types and integrating the rating for subjects. """ rank_func = None text = kwargs.get('text') language = kwargs.get('language') rank_cutoff = kwargs.get('rank_cutoff') if language is not None: rank_func = func.ts_rank_cd(SearchItem.terms, func.to_tsquery(SearchItem.getDictForLanguage(language), text)) else: rank_func = func.ts_rank_cd(SearchItem.terms, func.to_tsquery(text)) if rank_cutoff is not None: query = query.filter(rank_func >= rank_cutoff) if kwargs.get('use_rating'): query = query.order_by((SearchItem.rating * rank_func).desc()) else: query = query.order_by(rank_func.desc()) return query