首页 文章

ElasticSearch功能评分查询

提问于
浏览
1

以下是我的function_score查询 . 我想为产品质量更好的文档提供额外的分数 .

但是搜索响应中的_score总是为0.我缺少什么?谢谢 .

当我删除bool查询并仅使用术语过滤器替换它时,分数不为零 . 我猜这是关于查询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 回答

  • 4

    就像@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"
        }
      }
    }
    

相关问题