嵌套分数函数中的Max score_mode是平均值

时间:2017-04-06 22:08:04

标签: elasticsearch nested

目标:返回按2个因子排名的商店(A:高斯衰减函数,它对商店的距离与纬度的长度进行评分.B:乘以(A :)乘以1或100;如果没有商店的特价匹配则为1一个标准,如果商店的特价中的一个(或多个)符合标准,则为100。

示例:

说我有这个sudo-data描述的商店;

Steve's store
  -Special (does not match criteria)
  -Special (does not match criteria)
  -Special (does match criteria)
-Location (somewhere near the given lat long)


Bob's store
  -Special (does not match criteria)
  -Special (does not match criteria)
-Location (somewhere near the given lat long)

Sally's store
  -Special (does match criteria)
  -Special (does match criteria)
  -Special (does match criteria)
-Location (somewhere far away from the given lat long)

然后,史蒂夫的商店应该有89.2933的分数,鲍勃的商店应该有89.23223的分数,莎莉的分数应该是12.1

映射:

{
"store": {
"properties": {
  "explain": {
    "type": "boolean"
  },
  "specials": {
    "type": "nested",
    "properties": {
      "end_date": {
        "type": "date"
      },
      "store_id": {
        "analyzer": "lowercase_keyword",
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      },
      "image_url": {
        "type": "keyword"
      },
      "details": {
        "type": "keyword"
      },
      "id": {
        "analyzer": "lowercase_keyword",
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      },
      "title": {
        "type": "keyword"
      },
      "fine_print": {
        "type": "keyword"
      },
      "order": {
        "type": "integer"
      },
      "start_date": {
        "type": "date"
      }
    }
  },
  "city": {
    "type": "keyword"
  },
  "logo_image_url": {
    "type": "keyword"
  },
  "cover_image_url": {
    "type": "keyword"
  },
  "number_of_reviews": {
    "type": "integer"
  },
  "tag_line": {
    "type": "keyword"
  },
  "address2": {
    "type": "keyword"
  },
  "address1": {
    "type": "keyword"
  },
  "phone": {
    "type": "keyword"
  }… and so on.

查询:

POST stores/store/_search
{ 
  "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "location": {
                  "scale": "8mi",
                  "origin": "47.1338379197262,-122.266475055481",
                  "decay": 0.5
                }
              }
            }
          ],
          "query": {
            "bool": {
              "must": [
                {
                  "nested": {
                    "path": "specials",
                    "query": {
                      "function_score": {
                        "query": {
                          "bool": {
                            "must_not": [
                              {
                                "range": {
                                  "specials.start_date": {
                                    "gt": "now"
                                  }
                                }
                              },
                              {
                                "range": {
                                  "specials.end_date": {
                                    "lt": "now"
                                  }
                                }
                              }
                            ]
                          }
                        },
                        "boost_mode": "replace",
                        "score_mode": "max", 
                        "functions": [
                          {
                            "script_score": {

                              "script": {
                                "lang": "painless",
                                "inline": "<my_script>"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }

结果解释json(相关部分):

 …{
     "value": 12,
     "description": "Score based on 9 child docs in range from 24064 to 24072, best match:",
    "details": [
        {
         "value": 100,
         "description": "sum of:",
         "details": [
              {
               "value": 100,
               "description": "min of:",
               "details": [
                   {
                    "value": 100,
                    "description": "script score function, computed with script:\"<my_script>" and parameters: \n{}",
                     "details": [
                                {
                                  "value": 0,
                                  "description": "_score: ",
                                  "details": [
                                    {
                                      "value": 0,
                                      "description": "sum of:",
                                      "details": [
                                        {
                                          "value": 0,
                                          "description": "match on required clause, product of:",
                                          "details": [
                                            {
                                              "value": 0,
                                              "description": "# clause",
                                              "details": []
                                            },
                                            {
                                              "value": 1,
                                              "description": "*:*, product of:",
                                              "details": [
                                                {
                                                  "value": 1,
                                                  "description": "boost",
                                                  "details": []
                                                },
                                                {
                                                  "value": 1,
                                                  "description": "queryNorm",
                                                  "details": []
                                                }
                                              ]
                                            }
                                          ]
                                        }
                                      ]
                                    }
                                  ]
                                }
                              ]
                            },
                            {
                              "value": 3.4028235e+38,
                              "description": "maxBoost",
                              "details": []
                            }
                          ]
                          …

我的剧本:     如果在“特殊”中满足某些标准,则为100,否则为1。

结果:

该脚本功能的得分为12(因为商店有9个特价,其中只有一个符合得分脚本的标准) - 所以12来自(100 + 1 + 1 + 1 + 1 + 1 + 1) + 1 + 1)/ 9 = 12。将高斯距离衰减函数(0.36248797)的得分乘以等于4.7596407。换句话说,脚本分数最终是所有商店特价的平均值。

预期:

我希望/希望脚本功能得分能够通过从应用于商店的每个特殊功能的脚本得分功能中获取最大值来对商店进行评分。所以在上面的例子中,因为商店有1个特殊的匹配脚本的条件,那个特殊应该被评分为100.那100是该商店中所有特价的所有分数的最大值,因为其他8个不匹配标准。所以100应该乘以高斯衰减函数得到0.36248797 * 100 = 36.248797

这是一个错误,还是我做错了什么?

很多,非常感谢!

1 个答案:

答案 0 :(得分:1)

我认为(根据你提供的精简/减少的信息)你做错了什么。

nested query also has a score_mode和这个用来组合孩子的分数。您在查询中所做的是组合根文档的分数(子代的父母)。但是使用nested查询的score_mode组合匹配的子项分数。嵌套score_mode的默认值为avg。您应该将其更改为max,然后重试。

所以你认为我的查询应该是:

{ 
  "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "location": {
                  "scale": "8mi",
                  "origin": "47.1338379197262,-122.266475055481",
                  "decay": 0.5
                }
              }
            }
          ],
          "query": {
            "bool": {
              "must": [
                {
                  "nested": {
                    "path": "specials",
                    "score_mode": "max",
                    "query": {
                      "function_score": {
                      ...........