Login

MYSQL Full Text Expression

Author:
Bidaya0
Posted:
May 12, 2022
Language:
Python
Version:
3.2
Score:
0 (after 0 ratings)

MYSQL Full Text Expression

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.db import models
import copy
from django.db.models import Expression


class SearchLanguageMode(Expression):
    template = "MATCH( %(expressions)s ) AGAINST (+ '%(search_keyword)s' IN NATURAL LANGUAGE MODE )"

    def __init__(self, expressions, search_keyword):
        super().__init__(output_field=models.IntegerField())
        self.search_keyword = search_keyword
        for expression in expressions:
            if not hasattr(expression, 'resolve_expression'):
                raise TypeError('%r is not an Expression' % expression)
        self.expressions = expressions

    def resolve_expression(self,
                           query=None,
                           allow_joins=True,
                           reuse=None,
                           summarize=False,
                           for_save=False):
        c = self.copy()
        c.is_summary = summarize
        for pos, expression in enumerate(self.expressions):
            c.expressions[pos] = expression.resolve_expression(
                query, allow_joins, reuse, summarize, for_save)
        return c

    def as_sql(self, compiler, connection, template=None):
        sql_expressions, sql_params = [], []
        for expression in self.expressions:
            sql, params = compiler.compile(expression)
            sql_expressions.append(sql)
            sql_params.extend(params)
        template = template or self.template
        data = {
            'expressions': ','.join(sql_expressions),
            'search_keyword': self.search_keyword
        }
        return template % data, sql_params

    def get_source_expressions(self):
        return self.expressions

    def set_source_expressions(self, expressions):
        self.expressions = expressions

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.