2016-08-21 21 views
0

我願做這樣一個單一的ElasticSearch查詢(而不是訴諸使用script_score):與區域= DE和語言= DE,通過createdDate分類是否可以在ElasticSearch中進行此類排序,而不使用script_score?

  1. 對象。
  2. region = DE和任何語言的對象,按照createdDate排序。
  3. 任何區域和語言的對象= en,按照createdDate排序。

起初,我以爲我可以做一個function_score查詢(boost_mode:更換,score_mode:總和)和:

  1. 如果區域= DE和語言= DE,設置比分3000000億+ createdDate。
  2. 如果region = DE和language!= de,則將score設置爲200000000000000 + createdDate。
  3. 如果region!= DE和language = en,則將score設置爲100000000000000 + createdDate。

我可以通過使用field_value_factor將createdDate添加到分數。但是我找不到一個function_score function在region = DE和language = de的情況下給300000000000000加分。

有沒有可能做到這一點,而不使用script_score?

回答

0

這裏是如何做到這一點:

{ 
    "sort": [ 
     {"_score": "desc"}, 
     {"created_date": "desc"} 
    ], 
    "query": { 
     "function_score": { 
      "query": { 
       "bool": { 
        "minimum_should_match": 1, 
        "should": [ 
         { 
          "constant_score": { 
           "filter": { 
            "bool": { 
             "must": [ 
              { "term": { "region": "DE" } }, 
              { "term": { "language": "de" } } 
             ] 
            } 
           }, 
           "boost": 3 
          } 
         }, 
         { 
          "constant_score": { 
           "filter": { 
            "bool": { 
             "must": [ 
              { "term": { "region": "DE" } }, 
              { "not": { "term": { "language": "de" } } } 
             ] 
            } 
           }, 
           "boost": 2 
          } 
         }, 
         { 
          "constant_score": { 
           "filter": { 
            "bool": { 
             "must": [ 
              { "not": { "term": { "region": "DE" } } }, 
              { "term": { "language": "en" } } 
             ] 
            } 
           }, 
           "boost": 1 
          } 
         } 
        ] 
       } 
      } 
     } 
    } 
} 
相關問題