Features: 1) Add top_level field to documents with preparation logic; 2) Add price field to documents with preparation logic; 3) Introduce score boosting with function_score using sales_rank field;

Fixes: 1) None;

Extra: 1) Replace direct query with boosted query in Elasticsearch `search` logic; 2) Minor refactoring for improved readability;
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-19 14:45:40 +03:00
parent 432e4bf110
commit 529fd39ff4
2 changed files with 32 additions and 1 deletions

View file

@ -65,9 +65,31 @@ def process_query(query: str = "", request: Request | None = None):
minimum_should_match=1,
)
functions = [
{
"gauss": {
"sales_rank": {
"origin": 100,
"scale": 500,
"offset": 0,
"decay": 0.3,
}
},
"weight": 3,
},
]
boosted = Q(
"function_score",
query=q,
boost_mode="sum",
score_mode="sum",
functions=functions,
)
search = (
Search(index=["products", "categories", "brands", "posts"])
.query(q)
.query(boosted)
.extra(size=100)
)
response = search.execute()

View file

@ -56,6 +56,15 @@ class ProductDocument(_BaseDoc):
rating = fields.FloatField(attr="rating")
product_type = fields.KeywordField()
sales_rank = fields.IntegerField()
top_level = fields.KeywordField()
price = fields.FloatField()
def prepare_price(self, obj):
return float(obj.price) if obj.price else 0.0
def prepare_top_level(self, obj):
root = obj.category.get_root() if obj.category else None
return root.slug if root else "other"
def prepare_product_type(self, obj):
return obj.category.slug.split("-")[0] if obj.category else "other"