2017-04-25 20 views
0

功能提升查詢中排序包含有關於手機的詳細信息的索引,殼體上蓋等我如何elasticsearch

下面是使用的查詢。

{ 
"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工作正常。如何在特定的提升功能內進行排序?

如果用戶搜索蘋果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" 
      } 
     } 
     ] 
    } 
} 

請幫助的結果?

+0

你能展示你想達到的目標嗎? – user3775217

+0

問題已更新。請檢查。 –

回答

0

你可以試試下面的查詢:

{ 
"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=25於具有情況和蓋類別的文檔文件提供了高權重weight=50

最後我們先根據得分然後出售價格排序。

+0

似乎在某些情況下工作正常。但是_score在某些情況下計算錯誤。 _score如何計算? –

+0

你能提供它不起作用的情況嗎? – Richa

+0

通過使用這種技術,它將首先根據_score進行排序,如果出現相同的_score,則會對價格進行排序。但我需要排序而不考慮_score,就像我必須提高類別,然後使用price asc在類別內進行排序。假設,如果兩個類別都有手機和案例封面,那麼我需要使用價格分類對移動類別中的所有產品進行分類,然後對案例封面也進行相同分類。 –