Fixes: 1) Reworked search weights so text matches appear first.
This commit is contained in:
parent
b3a0fbef56
commit
d948d51f91
1 changed files with 36 additions and 17 deletions
|
|
@ -34,54 +34,60 @@ functions = [
|
|||
"field_value_factor": {
|
||||
"field": "brand_priority",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.2,
|
||||
"factor": 0.2,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.6,
|
||||
},
|
||||
{
|
||||
"filter": Q("term", **{"_index": "products"}),
|
||||
"field_value_factor": {
|
||||
"field": "rating",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.2,
|
||||
"factor": 0.15,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.5,
|
||||
},
|
||||
{
|
||||
"filter": Q("term", **{"_index": "products"}),
|
||||
"field_value_factor": {
|
||||
"field": "total_orders",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.3,
|
||||
"factor": 0.25,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.7,
|
||||
},
|
||||
{
|
||||
"filter": Q("term", **{"_index": "products"}),
|
||||
"field_value_factor": {
|
||||
"field": "category_priority",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.2,
|
||||
"factor": 0.2,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.6,
|
||||
},
|
||||
{
|
||||
"filter": Q("term", **{"_index": "categories"}),
|
||||
"field_value_factor": {
|
||||
"field": "priority",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.5,
|
||||
"factor": 0.25,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.8,
|
||||
},
|
||||
{
|
||||
"filter": Q("term", **{"_index": "brands"}),
|
||||
"field_value_factor": {
|
||||
"field": "priority",
|
||||
"modifier": "log1p",
|
||||
"factor": 1.5,
|
||||
"factor": 0.25,
|
||||
"missing": 0,
|
||||
},
|
||||
"weight": 0.8,
|
||||
},
|
||||
]
|
||||
|
||||
|
|
@ -93,8 +99,8 @@ def process_query(query: str = "", request: Request | None = None) -> dict[str,
|
|||
query = query.strip()
|
||||
try:
|
||||
exact_shoulds = [
|
||||
Q("term", **{"name.raw": query}),
|
||||
Q("term", **{"slug": slugify(query)}),
|
||||
Q("term", **{"name.raw": {"value": query, "boost": 3.0}}),
|
||||
Q("term", **{"slug": {"value": slugify(query), "boost": 2.0}}),
|
||||
]
|
||||
|
||||
lang = ""
|
||||
|
|
@ -142,18 +148,31 @@ def process_query(query: str = "", request: Request | None = None) -> dict[str,
|
|||
minimum_should_match=1,
|
||||
)
|
||||
|
||||
function_score_query = Q(
|
||||
"function_score",
|
||||
query=query_base,
|
||||
functions=functions,
|
||||
boost_mode="multiply",
|
||||
score_mode="sum",
|
||||
search = (
|
||||
Search(index=["products", "categories", "brands", "posts"])
|
||||
.query(query_base)
|
||||
.extra(
|
||||
rescore={
|
||||
"window_size": 200,
|
||||
"query": {
|
||||
"rescore_query": Q(
|
||||
"function_score",
|
||||
query=Q("match_all"),
|
||||
functions=functions,
|
||||
boost_mode="sum",
|
||||
score_mode="sum",
|
||||
max_boost=2.0,
|
||||
).to_dict(),
|
||||
"query_weight": 1.0,
|
||||
"rescore_query_weight": 1.0,
|
||||
},
|
||||
"track_scores": True,
|
||||
}
|
||||
)
|
||||
.extra(size=100)
|
||||
)
|
||||
|
||||
search = Search(index=["products", "categories", "brands", "posts"]).query(function_score_query).extra(size=100)
|
||||
response = search.execute()
|
||||
|
||||
# Batch-load related image data to avoid N+1 queries
|
||||
results: dict = {"products": [], "categories": [], "brands": [], "posts": []}
|
||||
uuids_by_index: dict[str, list] = {"products": [], "categories": [], "brands": []}
|
||||
hit_cache: list = []
|
||||
|
|
|
|||
Loading…
Reference in a new issue