diff --git a/core/elasticsearch/__init__.py b/core/elasticsearch/__init__.py index d3674193..f12e30bb 100644 --- a/core/elasticsearch/__init__.py +++ b/core/elasticsearch/__init__.py @@ -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() diff --git a/core/elasticsearch/documents.py b/core/elasticsearch/documents.py index ebebc4c5..75df0ed2 100644 --- a/core/elasticsearch/documents.py +++ b/core/elasticsearch/documents.py @@ -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"