Features: 1) Add support for fine-tuned boosts in Elasticsearch scoring for title and related fields; 2) Adjust product, brand, and category scoring multipliers to improve search ranking flexibility.

Fixes: 1) Correct `total_orders` calculation in `Product` model to count only relevant statuses.

Extra: 1) Refactor comments for clarity in Elasticsearch configuration; 2) Minor formatting adjustments.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-23 16:52:39 +03:00
parent 23351c79ab
commit 0a81b12100
2 changed files with 28 additions and 24 deletions

View file

@ -12,34 +12,35 @@ from rest_framework.request import Request
from core.models import Brand, Category, Product from core.models import Brand, Category, Product
SMART_FIELDS = [ SMART_FIELDS = [
"name^4", "name^5",
"name.ngram^3", "name.ngram^4",
"name.phonetic", "name.phonetic",
"description^2",
"description.ngram",
"description.phonetic",
"name.auto^4",
"description.auto^2",
"brand__name^2",
"brand__name.ngram",
"brand__name.auto",
"category__name^2",
"category__name.ngram",
"category__name.auto",
"title^4", "title^4",
"title.ngram^3", "title.ngram^3",
"title.phonetic", "title.phonetic",
"title.auto^4",
"description^2",
"description.ngram",
"description.phonetic",
"brand__name^3",
"brand__name.ngram",
"brand__name.auto",
"category__name^2",
"category__name.ngram",
"category__name.auto",
] ]
functions = [ functions = [
# — product boost whenever hitting the products` index — # product-level boosts when searching for products
{ {
"filter": Q("term", **{"_index": "products"}), "filter": Q("term", **{"_index": "products"}),
"field_value_factor": { "field_value_factor": {
"field": "brand_priority", "field": "brand_priority",
"modifier": "log1p", "modifier": "log1p",
"factor": 1, "factor": 1.5,
"missing": 0, "missing": 0,
}, },
}, },
@ -48,7 +49,7 @@ functions = [
"field_value_factor": { "field_value_factor": {
"field": "rating", "field": "rating",
"modifier": "log1p", "modifier": "log1p",
"factor": 1, "factor": 2.0,
"missing": 0, "missing": 0,
}, },
}, },
@ -57,7 +58,7 @@ functions = [
"field_value_factor": { "field_value_factor": {
"field": "total_orders", "field": "total_orders",
"modifier": "log1p", "modifier": "log1p",
"factor": 1, "factor": 3.0,
"missing": 0, "missing": 0,
}, },
}, },
@ -66,29 +67,29 @@ functions = [
"field_value_factor": { "field_value_factor": {
"field": "category_priority", "field": "category_priority",
"modifier": "log1p", "modifier": "log1p",
"factor": 1, "factor": 1.2,
"missing": 0, "missing": 0,
}, },
}, },
# — category boost whenever hitting the categories` index — # category-level boost when searching for categories
{ {
"filter": Q("term", **{"_index": "categories"}), "filter": Q("term", **{"_index": "categories"}),
"field_value_factor": { "field_value_factor": {
"field": "priority", "field": "priority",
"modifier": "log1p", "modifier": "log1p",
"factor": 2, # you can tweak "factor": 2.0,
"missing": 0, "missing": 0,
}, },
}, },
# — brand boost whenever hitting the brands` index — # brand-level boost when searching for brands
{ {
"filter": Q("term", **{"_index": "brands"}), "filter": Q("term", **{"_index": "brands"}),
"field_value_factor": { "field_value_factor": {
"field": "priority", "field": "priority",
"modifier": "log1p", "modifier": "log1p",
"factor": 2, "factor": 2.0,
"missing": 0, "missing": 0,
}, },
}, },

View file

@ -439,7 +439,10 @@ class Product(ExportModelOperationsMixin("product"), NiceModel):
@property @property
def total_orders(self): def total_orders(self):
return OrderProduct.objects.filter(product__uuid=self.uuid).count() return OrderProduct.objects.filter(
product__uuid=self.uuid,
status__in=["FINISHED", "DELIVERING", "CREATED", "PAYMENT"],
).count()
class Attribute(ExportModelOperationsMixin("attribute"), NiceModel): class Attribute(ExportModelOperationsMixin("attribute"), NiceModel):