2016-10-03 77 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" 
    } 
    } 
}