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 

}

それは、価格昇順を販売することによって、以下のソート携帯電話カテゴリ内のような携帯電話のカテゴリを向上することが可能です。

ブーストが正常に動作しています。どのように特定のブースト機能内で並べ替えるには?

ユーザーが 6S AppleのiPhoneで検索した場合、私はモバイルをしたいブーストするカテゴリと最低価格は、価格昇順の販売にも、第1およびケースとカバーのカテゴリの製品を来なければなりません。以下は

{ 
    "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に関係なく並べ替える必要があります。たとえば、2つのカテゴリにモバイルとケースカバーがある場合、モバイルカテゴリのすべての製品を価格の昇順でソートする必要があり、ケースカバーでも同様にソートする必要があります。 –

関連する問題