Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add custom SearchVector class (#10109)
Krzysztof Kwaśniakbf74f5efb419
diff --git a/saleor/product/postgres.py b/saleor/product/postgres.pynew file mode 100644index 0000000000..b2efd3d9cc--- /dev/null+++ b/saleor/product/postgres.py@@ -0,0 +1,37 @@+from django.contrib.postgres.search import (+ CombinedSearchVector,+ SearchVector,+ SearchVectorCombinable,+)+++class NoValidationSearchVectorCombinable(SearchVectorCombinable):+ def _combine(self, other, connector, reversed):+ if not isinstance(other, NoValidationSearchVectorCombinable):+ raise TypeError(+ "SearchVector can only be combined with other SearchVector "+ "instances, got %s." % type(other).__name__+ )+ if reversed:+ return NoValidationCombinedSearchVector(other, connector, self, self.config)+ return NoValidationCombinedSearchVector(self, connector, other, self.config)+++class NoValidationCombinedSearchVector(+ NoValidationSearchVectorCombinable, CombinedSearchVector+):+ contains_aggregate = False+ contains_over_clause = False+++class NoValidationSearchVector(SearchVector, NoValidationSearchVectorCombinable):+ """The purpose of this class is to omit Django's SQL compiler's validation.++ This validation can lead to RecursionError while processing a large number of+ expressions.+ If expressions contained aggregate or over clause, then exception still be raised+ during SQL execution instead of before preparing SQL.++ This class is only safe to use with expressions that do not contain aggregation+ and/or over clause.+ """diff --git a/saleor/product/search.py b/saleor/product/search.pyindex 22c676d4ff..893a4e1172 100644--- a/saleor/product/search.py+++ b/saleor/product/search.py@@ -2,12 +2,13 @@ from functools import reducefrom operator import addfrom typing import TYPE_CHECKING, Optional, Union-from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector+from django.contrib.postgres.search import SearchQuery, SearchRankfrom django.db.models import F, Q, Value, prefetch_related_objectsfrom ..attribute import AttributeInputTypefrom ..core.utils.editorjs import clean_editor_jsfrom .models import Product+from .postgres import NoValidationSearchVectorif TYPE_CHECKING:from django.db.models import QuerySet@@ -57,12 +58,14 @@ def update_product_search_vector(product: "Product"):def prepare_product_search_vector_value(product: "Product", *, already_prefetched=False-) -> SearchVector:+) -> NoValidationSearchVector:if not already_prefetched:prefetch_related_objects([product], *PRODUCT_FIELDS_TO_PREFETCH)- search_vector = SearchVector(+ search_vector = NoValidationSearchVector(Value(product.name), config="simple", weight="A"- ) + SearchVector(Value(product.description_plaintext), config="simple", weight="C")+ ) + NoValidationSearchVector(+ Value(product.description_plaintext), config="simple", weight="C"+ )attributes_vector = generate_attributes_search_vector_value(product.attributes.all())@@ -75,15 +78,17 @@ def prepare_product_search_vector_value(return search_vector-def generate_variants_search_vector_value(product: "Product") -> Optional[SearchVector]:+def generate_variants_search_vector_value(+ product: "Product",+) -> Optional[NoValidationSearchVector]:variants = list(product.variants.all())variant_vectors = [- SearchVector(+ NoValidationSearchVector(Value(variant.sku), Value(variant.name), config="simple", weight="A")if variant.sku- else SearchVector(Value(variant.name), config="simple", weight="A")+ else NoValidationSearchVector(Value(variant.name), config="simple", weight="A")for variant in variantsif variant.sku or variant.name]@@ -105,7 +110,7 @@ def generate_variants_search_vector_value(product: "Product") -> Optional[Searchdef generate_attributes_search_vector_value(assigned_attributes: "QuerySet",-) -> Optional[SearchVector]:+) -> Optional[NoValidationSearchVector]:"""Prepare `search_vector` value for assigned attributes.Method should received assigned attributes with prefetched `values`@@ -140,7 +145,7 @@ def generate_attributes_search_vector_value(new_vector = reduce(add,(- SearchVector(Value(v), config="simple", weight="B")+ NoValidationSearchVector(Value(v), config="simple", weight="B")for v in values_list),)