当使用高斯衰减分数函数时,它在嵌套元素上总是得1

时间:2015-12-25 22:38:13

标签: elasticsearch nested gaussian

适用于

等文件
{
"_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高斯显然在找不到场时返回1。 但问题是为什么它无法在嵌套文档中找到该字段以及如何找到它。

1 个答案:

答案 0 :(得分:0)

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"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

这有帮助吗?