Reputation: 881
I have an index containing details regarding mobiles, case covers etc.
Below is the query used.
{
"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
}
Is it possible to boost mobiles category like below and sort within the mobiles category by selling price ascending order.
Boost is working fine. How to sort within the specific boost function?
If user searched for apple iphone 6s,I want mobiles category to be boosted and lowest price should comes first and then case and cover category products also in selling price ascending order.
Below are the results needed
{
"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"
}
}
]
}
}
Please help?.
Upvotes: 2
Views: 2776
Reputation: 7649
Can you try following query:
{
"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"
}
}
]
}
We are providing high weight weight=50
to the documents which have mobile category and low weight weight=25
to the documents which have case-and-cover category.
Finally We are sorting first on the basis of score and then selling price.
Upvotes: 3