我有一个索引,其中包含有关手机、保护壳等的详细信息。
下面是使用的查询。
{
"query": {
"function_score": {
"query": { "match": {"title" :"Apple iPhone 6s"} },
"boost": "5",
"functions": [
{
"filter": { "match": { "main_category": "mobiles" } },
"weight": 8
},
{
"filter": { "match": {"main_category": "cases-and-covers" } },
"weight": 6
}
],
"max_boost": 8,
"score_mode": "max",
"boost_mode": "multiply",
"min_score" : 5
}
},
"_source":["title","main_category","selling_price"],
"size" : 1000
}
是否可以像下面这样提升手机类别,并通过销售价格升序在手机类别内排序。
Boost 工作正常。如何在特定的 boost 函数内排序?
如果用户搜索apple iphone 6s,我希望提升手机类别,并且最低价格应该排在第一位,然后是保护壳和保护套品类产品也按售价升序排列。
以下是所需的结果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 104,
"max_score": 40.645245,
"hits": [
{
"_index": "test",
"_type": "products",
"_id": "shop_24",
"_score": 40.645245,
"_source": {
"selling_price": 72000,
"main_category": "mobiles",
"title": "Apple iPhone 6s"
}
},
{
"_index": "test",
"_type": "products",
"_id": "shop_20",
"_score": 40.168346,
"_source": {
"selling_price": 82000,
"main_category": "mobiles",
"title": "Apple iPhone 6s Plus"
}
},
{
"_index": "test",
"_type": "products",
"_id": "shop_15",
"_score": 39.365562,
"_source": {
"selling_price": 92000,
"main_category": "mobiles",
"title": "Apple iPhone 6s Plus"
}
},
{
"_index": "test",
"_type": "products",
"_id": "shop_17",
"_score": 39.365562,
"_source": {
"selling_price": 2000,
"main_category": "cases-and-covers",
"title": "Case cover for Apple iPhone 6s"
}
},
{
"_index": "test",
"_type": "products",
"_id": "shop_18",
"_score": 39.365562,
"_source": {
"selling_price": 2300,
"main_category": "cases-and-covers",
"title": "Case cover for Apple iPhone 6s Plus"
}
}
]
}
}
请帮忙?
最佳答案
您可以尝试以下查询吗:
{
"query": {
"function_score": {
"query": {
"match_all": {} // Change this to query as per your need
},
"boost": "5",
"functions": [
{
"filter": {
"match": {
"main_category": "mobiles"
}
},
"weight": 50
},
{
"filter": {
"match": {
"main_category": "cases-and-covers"
}
},
"weight": 25
}
]
}
},
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"selling_price": {
"order": "asc"
}
}
]
}
我们提供高权重weight=50
具有移动类别且权重较低的文档weight=25
到具有case-and-cover类别的文档。
最后,我们首先根据分数进行排序,然后根据售价进行排序。
关于php - 如何在elasticsearch中的功能增强查询中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609871/