2

我想根据嵌套字段值计算我在 elasticsearch 上的查询得分。这是我的文档的示例:

{
  "title": "Mr",
  "first_name": "Barry",
  "last_name": "White",
  "experiences": [
     {
       "name": "parachute",
       "values": {
           "last_year": 4,
           "older": 12
       }
    },
    {
       "name": "base jump",
       "values": {
           "last_year": 2,
           "older": 1
       }
    }
   ]
}

我想要的是根据经验数量和范围(去年的值更重要)过滤和排名查询。问题是:我如何才能获得过滤经验的价值?

这是我的查询的简化版本:

{
"query" :{
    "nested": {
        "path": "experiences",
        "query":{
            "function_score" : {
                "functions": [
                    {
                        "filter": {"term": { "experiences.name": "parachute"} },
                        "script_score" : {
                            "script" : {
                              "lang": "painless",
                              "inline": "_score * doc['experiences.values.older'].value" //Here I want to get the "older" value of "parachute": 12
                            }
                        }
                    }
                ]
             }

        }
    }
}
}

希望有办法解决这个问题...

在此先感谢您的帮助

4

1 回答 1

1

多亏了官方 elasticsearch IRC 频道的帮助,我终于设法解决了这个问题。我不得不稍微改变一下文档映射:

  • 删除“价值观”
  • 将“last_year”和“older”设置为每个体验的根值

就是这样。所以这是最终的查询:

{
    "query" :{
        "nested": {
            "path": "experiences",
            "query":{
                "function_score" : {
                    "query":{
                        "term": { "experiences.name": "parachute"}
                    },
                    "functions": [
                        {
                            "field_value_factor": {
                                "field": "experiences.older",
                                "factor": 1,
                                "missing": 1
                            }
                        },{
                            "field_value_factor": {
                                "field": "experiences.last_year",
                                "factor": 2,
                                "missing": 1

                            }
                        }
                    ],
                    "score_mode": "sum"
                }
            }
        }
    }
}
于 2017-06-13T12:36:50.427 回答