2016-10-03 39 views
1

以下是我的function_score查詢。我想給產品質量更好的文檔提供額外的分數。ElasticSearch函數分數查詢

但是,搜索響應中的_score始終爲0.我錯過了什麼?謝謝。

當我刪除bool查詢並用一個術語過濾器替換它時,得分不爲零。我猜這是關於查詢布爾,但無法弄清楚爲什麼。

Elasticsearch版本是2.4

{ 
    "from": 0, 
    "size": 20, 
    "query": { 
    "function_score": { 
     "query": { 
     "bool": { 
      "filter": [ 
      { 
       "bool": { 
       "should": { 
        "terms": { 
        "categories.category1Id": [ 
         63 
        ] 
        } 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "functions": [ 
     { 
      "gauss": { 
      "updatedDate": { 
       "origin": "2016-10-03 05:10:18", 
       "scale": "0.5h", 
       "decay": 0.1, 
       "offset": "1h" 
      } 
      } 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "EXCELLENT" 
      } 
      }, 
      "weight": 7 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "HIGH" 
      } 
      }, 
      "weight": 5 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "MEDIUM" 
      } 
      }, 
      "weight": 3 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "LOW" 
      } 
      }, 
      "weight": 1 
     } 
     ], 
     "score_mode": "sum" 
    } 
    } 
} 
+1

您的查詢的第一個部分是一個過濾器。過濾器只會返回一個常數分數。您可以使用explain api來查看查詢的工作原理:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html – jay

+0

@jay是對的,['bool/filter'查詢不提供評分](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#_scoring_with_literal_bool_filter_literal) – Val

回答

2

至於什麼@val說。

bool.filter爲所有文檔指定0分,因爲沒有指定評分查詢(link)。

如果您需要評分,您可以在您的查詢中添加"must": {"match_all": {}}match_all將爲所有文檔分配1.0(link)。

這裏是match_all查詢:

{ 
    "from": 0, 
    "size": 20, 
    "query": { 
    "function_score": { 
     "query": { 
     "bool": { 
      "must": { 
       "match_all": {} 
      }, 
      "filter": [ 
      { 
       "bool": { 
       "should": { 
        "terms": { 
        "categories.category1Id": [ 
         63 
        ] 
        } 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "functions": [ 
     { 
      "gauss": { 
      "updatedDate": { 
       "origin": "2016-10-03 05:10:18", 
       "scale": "0.5h", 
       "decay": 0.1, 
       "offset": "1h" 
      } 
      } 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "EXCELLENT" 
      } 
      }, 
      "weight": 7 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "HIGH" 
      } 
      }, 
      "weight": 5 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "MEDIUM" 
      } 
      }, 
      "weight": 3 
     }, 
     { 
      "filter": { 
      "term": { 
       "productQuality": "LOW" 
      } 
      }, 
      "weight": 1 
     } 
     ], 
     "score_mode": "sum" 
    } 
    } 
}