-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add search * Update search * Update * add postgres_search and update docs * Fix CI * Fix CI * Fix test
- Loading branch information
Showing
11 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from enum import Enum | ||
from typing import Any, Optional | ||
|
||
from pypika.enums import Comparator | ||
from pypika.terms import BasicCriterion | ||
from pypika.terms import Function as PypikaFunction | ||
from pypika.terms import Term | ||
|
||
|
||
class Comp(Comparator): # type: ignore | ||
search = " " | ||
|
||
|
||
class Mode(Enum): | ||
NATURAL_LANGUAGE_MODE = "IN NATURAL LANGUAGE MODE" | ||
NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPRESSION = "IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION" | ||
BOOL_MODE = "IN BOOLEAN MODE" | ||
WITH_QUERY_EXPRESSION = "WITH QUERY EXPANSION" | ||
|
||
|
||
class Match(PypikaFunction): # type: ignore | ||
def __init__(self, *columns: Term): | ||
super(Match, self).__init__("MATCH", *columns) | ||
|
||
|
||
class Against(PypikaFunction): # type: ignore | ||
def __init__(self, expr: Term, mode: Optional[Mode] = None): | ||
super(Against, self).__init__("AGAINST", expr) | ||
self.mode = mode | ||
|
||
def get_special_params_sql(self, **kwargs: Any) -> Any: | ||
if not self.mode: | ||
return "" | ||
return self.mode.value | ||
|
||
|
||
class SearchCriterion(BasicCriterion): # type: ignore | ||
""" | ||
Only support for CharField, TextField with full search indexes. | ||
""" | ||
|
||
def __init__(self, *columns: Term, expr: Term, mode: Optional[Mode] = None): | ||
super().__init__(Comp.search, Match(*columns), Against(expr, mode)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pypika.terms import Function, Term | ||
|
||
|
||
class ToTsVector(Function): # type: ignore | ||
""" | ||
to to_tsvector function | ||
""" | ||
|
||
def __init__(self, field: Term): | ||
super(ToTsVector, self).__init__("TO_TSVECTOR", field) | ||
|
||
|
||
class ToTsQuery(Function): # type: ignore | ||
""" | ||
to_tsquery function | ||
""" | ||
|
||
def __init__(self, field: Term): | ||
super(ToTsQuery, self).__init__("TO_TSQUERY", field) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pypika.enums import Comparator | ||
from pypika.terms import BasicCriterion, Term | ||
|
||
from tortoise.contrib.postgres.functions import ToTsQuery, ToTsVector | ||
|
||
|
||
class Comp(Comparator): # type: ignore | ||
search = " @@ " | ||
|
||
|
||
class SearchCriterion(BasicCriterion): # type: ignore | ||
def __init__(self, field: Term, expr: Term): | ||
super().__init__(Comp.search, ToTsVector(field), ToTsQuery(expr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters