带有部分句子匹配的Django文本搜索已更新为django3 [英] Django text search with partial sentence match update to django3

查看:65
本文介绍了带有部分句子匹配的Django文本搜索已更新为django3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django postgres中应用部分搜索,与此处所述完全相同

I am trying to apply partial search in Django postgres, exactly the same as described here django-text-search-with-partial-sentence-match I found there a pretty nice solution

from psycopg2.extensions import adapt

from django.contrib.postgres.search import SearchQuery


class PrefixedPhraseQuery(SearchQuery):
"""
Alter the tsquery executed by SearchQuery
"""
    def as_sql(self, compiler, connection):
        # Or <-> available in Postgres 9.6
        value = adapt('%s:*' % ' & '.join(self.value.split()))

        if self.config:
            config_sql, config_params = compiler.compile(self.config)
            template = 'to_tsquery({}::regconfig, {})'\
                .format(config_sql, value)
            params = config_params

        else:
            template = 'to_tsquery({})'\
                .format(value)
            params = []

        if self.invert:
            template = '!!({})'.format(template)

        return template, params

它在python 3.6上工作正常,但在3.9下不起作用.与3.6 SearchQuery从Value继承的区别在于:

It works fine for python 3.6 but does not work for 3.9. The difference is, than in 3.6 SearchQuery inherits from Value:

class SearchQuery(SearchQueryCombinable, Value):
    output_field = SearchQueryField()
    SEARCH_TYPES = {
        'plain': 'plainto_tsquery',
        'phrase': 'phraseto_tsquery',
        'raw': 'to_tsquery',
    }
    def __init__(self, value, output_field=None, *, config=None, invert=False, search_type='plain'):
        self.config = config
        self.invert = invert
        if search_type not in self.SEARCH_TYPES:
            raise ValueError("Unknown search_type argument '%s'." % search_type)
        self.search_type = search_type
        super().__init__(value, output_field=output_field)

并且在python 3.9中SearchQuery继承自Func:

and in python 3.9 SearchQuery inherits from Func:

class SearchQuery(SearchQueryCombinable, Func):
    output_field = SearchQueryField()
    SEARCH_TYPES = {
        'plain': 'plainto_tsquery',
        'phrase': 'phraseto_tsquery',
        'raw': 'to_tsquery',
        'websearch': 'websearch_to_tsquery',
    }

    def __init__(self, value, output_field=None, *, config=None, invert=False, search_type='plain'):
        self.function = self.SEARCH_TYPES.get(search_type)
        if self.function is None:
            raise ValueError("Unknown search_type argument '%s'." % search_type)
        if not hasattr(value, 'resolve_expression'):
            value = Value(value)
        expressions = (value,)
        self.config = SearchConfig.from_parameter(config)
        if self.config is not None:
            expressions = (self.config,) + expressions
        self.invert = invert
        super().__init__(*expressions, output_field=output_field)

在Func中,与Value不同,没有 self.value

In Func unlike in Value, there is no self.value

class Func(SQLiteNumericMixin, Expression):
    """An SQL function call."""
    function = None
    template = '%(function)s(%(expressions)s)'
    arg_joiner = ', '
    arity = None  # The number of arguments the function accepts.

    def __init__(self, *expressions, output_field=None, **extra):
        if self.arity is not None and len(expressions) != self.arity:
            raise TypeError(
                "'%s' takes exactly %s %s (%s given)" % (
                    self.__class__.__name__,
                    self.arity,
                    "argument" if self.arity == 1 else "arguments",
                    len(expressions),
                )
            )
        super().__init__(output_field=output_field)
        self.source_expressions = self._parse_expressions(*expressions)
        self.extra = extra

值看起来像这样

class Value(Expression):
    """Represent a wrapped value as a node within an expression."""
    def __init__(self, value, output_field=None):
        """
        Arguments:
         * value: the value this expression represents. The value will be
           added into the sql parameter list and properly quoted.

         * output_field: an instance of the model field type that this
           expression will return, such as IntegerField() or CharField().
        """
        super().__init__(output_field=output_field)
        self.value = value

有没有人帮助我将代码示例调整为python 3.9或为类似搜索推荐任何解决方案?

Cound anybody help me to adjust the code sample to python 3.9 or recommend any solution for similar search?

推荐答案

因此,我们很清楚,您的问题不是Python版本,而是Django版本.

So we're clear, your problem here is not with the Python version, but with the Django version.

您要查找的应该是 self.source_expressions [0] .

但是,如果我今天要重写此内容,我将从查看 原始搜索类型(

However if I were rewriting this today, I'd start by looking at the raw search type (example) and pass my own query using the proximity operator <-> (example).

这篇关于带有部分句子匹配的Django文本搜索已更新为django3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆