Elasticsearch Painless 脚本按值获取嵌套字段?
Elasticsearch Painless script get nested field by value?
我想根据嵌套字段值计算我在 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
}
}
}
]
}
}
}
}
}
希望有办法解决这个问题...
在此先感谢您的帮助
感谢elasticsearch官方IRC频道的帮助,我终于设法解决了这个问题。我不得不稍微更改文档映射:
- 移除"values"
- 设置"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"
}
}
}
}
}
我想根据嵌套字段值计算我在 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
}
}
}
]
}
}
}
}
}
希望有办法解决这个问题...
在此先感谢您的帮助
感谢elasticsearch官方IRC频道的帮助,我终于设法解决了这个问题。我不得不稍微更改文档映射:
- 移除"values"
- 设置"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"
}
}
}
}
}