From 0a81b12100dd3261652f8ce6901b195394680f2c Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 23 Jun 2025 16:52:39 +0300 Subject: [PATCH] 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. --- core/elasticsearch/__init__.py | 47 +++++++++++++++++----------------- core/models.py | 5 +++- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/core/elasticsearch/__init__.py b/core/elasticsearch/__init__.py index 69e835d3..f7c7bb7b 100644 --- a/core/elasticsearch/__init__.py +++ b/core/elasticsearch/__init__.py @@ -12,34 +12,35 @@ from rest_framework.request import Request from core.models import Brand, Category, Product SMART_FIELDS = [ - "name^4", - "name.ngram^3", + "name^5", + "name.ngram^4", "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.ngram^3", "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 = [ - # — product boost whenever hitting the products` index — + # product-level boosts when searching for products { "filter": Q("term", **{"_index": "products"}), "field_value_factor": { "field": "brand_priority", "modifier": "log1p", - "factor": 1, + "factor": 1.5, "missing": 0, }, }, @@ -48,7 +49,7 @@ functions = [ "field_value_factor": { "field": "rating", "modifier": "log1p", - "factor": 1, + "factor": 2.0, "missing": 0, }, }, @@ -57,7 +58,7 @@ functions = [ "field_value_factor": { "field": "total_orders", "modifier": "log1p", - "factor": 1, + "factor": 3.0, "missing": 0, }, }, @@ -66,29 +67,29 @@ functions = [ "field_value_factor": { "field": "category_priority", "modifier": "log1p", - "factor": 1, + "factor": 1.2, "missing": 0, }, }, - # — category boost whenever hitting the categories` index — + # category-level boost when searching for categories { "filter": Q("term", **{"_index": "categories"}), "field_value_factor": { "field": "priority", "modifier": "log1p", - "factor": 2, # you can tweak + "factor": 2.0, "missing": 0, }, }, - # — brand boost whenever hitting the brands` index — + # brand-level boost when searching for brands { "filter": Q("term", **{"_index": "brands"}), "field_value_factor": { "field": "priority", "modifier": "log1p", - "factor": 2, + "factor": 2.0, "missing": 0, }, }, diff --git a/core/models.py b/core/models.py index 64b3e976..2e8e637a 100644 --- a/core/models.py +++ b/core/models.py @@ -439,7 +439,10 @@ class Product(ExportModelOperationsMixin("product"), NiceModel): @property 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):