【问题标题】:When using gauss decay score funtion, it always scores 1 on nested elements使用高斯衰减分数函数时,它总是在嵌套元素上得分 1
【发布时间】:2018-11-05 17:40:31
【问题描述】:

对于像

这样的文件
{
"_id" : "abc123",
"_score" : 3.7613528,
"_source" : {
    "id" : "abc123",
    "pricePeriods" : [{
            "periodTo" : "2016-01-02",
            "eur" : 1036,
            "gbp" : 782,
            "dkk" : 6880,
            "sek" : 9025,
            "periodFrom" : "2015-12-26",
            "nok" : 8065
        }, {
            "periodTo" : "2016-06-18",
            "eur" : 671,
            "gbp" : 457,
            "dkk" : 4625,
            "sek" : 5725,
            "periodFrom" : "2016-01-02",
            "nok" : 5430
        }       ]

 }
}

我想对价格进行高斯衰减函数评分。

我试过这样

"query" : {
    "function_score" : {
        "functions" : [{
                "gauss" : {
                    "pricePeriods.dkk" : {
                        "origin" : "2500",
                        "scale" : "2500",
                        "decay" : 0.8

                    }
                },
                "filter" : {
                    "nested" : {
                        "filter" : {
                            "range" : {
                                "pricePeriods.periodTo" : {
                                    "gte" : "2016-03-17T00:00:00.000"
                                }
                            }

                        },
                        "path" : "pricePeriods"

                    }
                }
            }
            ]

并且过滤器似乎找到了我想要进行高斯计算的价格,但结果分数始终为 1。

解释说

{ "value": 1,
  "description": "min of:",
   "details": [
    {
         "value": 1,
         "description": "function score, score mode [multiply]",
          "details": [
                    {
              "value": 1,
               "description": "function score, product of:",
                 "details": [
                  {
                    "value": 1,
                       "description": "match filter: ToParentBlockJoinQuery (+ConstantScore(pricePeriods.periodTo:[[32 30 31 36 2d 30 33 2d 31 37 54 30 30 3a 30 30 3a 30 30 2e 30 30 30] TO *]) #QueryWrapperFilter(_type:__pricePeriods))",
                                         "details": []
                                      },
                                      {
                                         "value": 1,
                                         "description": "Function for field pricePeriods.dkk:",
                                         "details": [
                                            {
                                               "value": 1,
                                               "description": "exp(-0.5*pow(MIN[0.0],2.0)/1.4004437867889222E7)",
                                               "details": []
                                            }
                                         ]
                                      }
                                   ]
                                }
                             ]
                          }

我可以看到here gauss 在找不到该字段时显然返回 1。 但问题是为什么它无法在嵌套文档中找到该字段以及如何修复它。

【问题讨论】:

    标签: elasticsearch nested gaussian


    【解决方案1】:

    gauss function 返回 1 的原因是因为正如您所说,它无法找到 nested 的字段,您基本上需要将整个 function_score 查询包装到 nested query

    {
      "query": {
        "nested": {
          "path": "pricePeriods",
          "query": {
            "function_score": {
              "functions": [
                {
                  "gauss": {
                    "pricePeriods.dkk": {
                      "origin": "2500",
                      "scale": "2500",
                      "decay": 0.8
                    }
                  },
                  "filter": {
                    "range": {
                      "pricePeriods.periodTo": {
                        "gte": "2016-03-17T00:00:00.000"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    这有帮助吗?

    【讨论】:

    • 谢谢,但还是有两个问题。 1:无论是否有价格,它仍然为所有结果返回分数 1。 link 2:无法为父级添加其他功能。完整的查询是一个相当复杂的查询,包含许多不同字段的分数。但我不知道如何添加嵌套和顶级函数分数。
    • 我创建了一个测试索引并且它有效,它只给那些不符合条件的文档评分 1。您使用的是什么版本的 ES?
    • 谢谢。似乎现在正在工作。我忘记给函数添加权重,所以所有匹配高斯过滤器的文档都被排序在 1 以下。通过添加一个 wight,它现在的得分似乎好多了。我使用版本 2.1.1 和 1.7
    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2018-03-12
    • 2017-02-03
    • 2016-10-25
    相关资源
    最近更新 更多