def quotes(wordlist):
     return ["%s" % adapt(x.replace("\\", "")) for x in wordlist]
Exemple #2
0
    def search(self, query, rank_field=None, rank_function='ts_rank', config=None,
               rank_normalization=32, raw=False, using=None, fields=None,
               headline_field=None, headline_document=None):
        '''
        Convert query with to_tsquery or plainto_tsquery, depending on raw is
        `True` or `False`, and return a QuerySet with the filter.

        If `rank_field` is not `None`, a field with this name will be added
        containing the search rank of the instances, and the queryset will be
        ordered by it. The rank_function and normalization are explained here:

        http://www.postgresql.org/docs/9.1/interactive/textsearch-controls.html#TEXTSEARCH-RANKING

        If an empty query is given, no filter is made so the QuerySet will
        return all model instances.

        If `fields` is not `None`, the filter is made with this fields instead
        of defined on a constructor of manager.

        If `headline_field` and `headline_document` is not `None`, a field with
        this `headline_field` name will be added containing the headline of the
        instances, which will be searched inside `headline_document`.

        Search headlines are explained here:
        http://www.postgresql.org/docs/9.1/static/textsearch-controls.html#TEXTSEARCH-HEADLINE
        '''

        if not config:
            config = self.manager.config

        db_alias = using if using is not None else self.db
        connection = connections[db_alias]
        qn = connection.ops.quote_name

        qs = self
        if using is not None:
            qs = qs.using(using)

        if query:
            function = "to_tsquery" if raw else "plainto_tsquery"
            ts_query = smart_text(
                "%s('%s', %s)" % (function, config, adapt(query))
            )

            full_search_field = "%s.%s" % (
                qn(self.model._meta.db_table),
                qn(self.manager.search_field)
            )

            # if fields is passed, obtain a vector expression with
            # these fields. In other case, intent use of search_field if
            # exists.
            if fields:
                search_vector = self.manager._get_search_vector(
                    config, using, fields=fields
                )
            else:
                if not self.manager.search_field:
                    raise ValueError("search_field is not specified")

                search_vector = full_search_field

            where = " (%s) @@ (%s)" % (search_vector, ts_query)
            select_dict, order = {}, []

            if rank_field:
                select_dict[rank_field] = '%s(%s, %s, %d)' % (
                    rank_function,
                    search_vector,
                    ts_query,
                    rank_normalization
                )
                order = ['-%s' % (rank_field,)]

            if headline_field is not None and headline_document is not None:
                select_dict[headline_field] = "ts_headline('%s', %s, %s)" % (
                    config,
                    headline_document,
                    ts_query
                )

            qs = qs.extra(select=select_dict, where=[where], order_by=order)

        return qs
 def quotes(wordlist):
     return ["%s" % adapt(x.replace("\\", "")) for x in wordlist]