我在 elasticsearch 中有一个看起来像这样的类型:
"hotel" : {
"field" : 1,
"rooms" : [
{
"type" : "single",
"magicScore" : 1
},
{
"type" : "double",
"magicScore" : 2
}
]
}
Run Code Online (Sandbox Code Playgroud)
其中房间是嵌套类型。我使用嵌套的 functionScoreQuery 进行排序:
{
"query" : {
"filtered" : {
"query" : {
"nested" : {
"query" : {
"function_score" : {
"filter" : {
"match_all" : { }
},
"functions" : [ {
"script_score" : {
"script" : "return doc['hotel.field'].value"
}
} ]
}
},
"path" : "rooms",
"score_mode" : "max"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是hotel.field 总是返回0。有没有办法访问嵌套查询中的父字段?我知道我总是可以将字段打包在嵌套文档中,但它是一个黑客而不是解决方案。使用 dismax 查询对我有帮助吗?https://discuss.elastic.co/t/nested-value-on-function-score/29935
我实际使用的查询如下所示:
{
"query" : {
"bool" : {
"must" : {
"nested" : {
"query" : {
"function_score" : {
"query" : {
"not" : {
"query" : {
"terms" : {
"rooms.type" : [ "single", "triple" ]
}
}
}
},
"functions" : [ {
"script_score" : {
"script" : {
"inline" : "return doc['rooms.magicScore'].value;",
"lang" : "groovy",
"params" : {
"ratings" : {
"sample" : 0.5
},
"variable" : [ 0.0, 0.0, 0.0, 0.0, -0.5, -2.5]
}
}
}
} ],
"score_mode" : "max"
}
},
"path" : "rooms"
}
},
"filter" : {
"bool" : {
"filter" : [ {
"bool" : {
"should" : [ {
"term" : {
"cityId" : "166"
}
}, {
"term" : {
"cityId" : "165"
}
} ]
}
}, {
"nested" : {
"query" : {
"not" : {
"query" : {
"terms" : {
"rooms.type" : [ "single", "triple" ]
}
}
}
},
"path" : "rooms"
}
} ]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是访问例如嵌套的 function_score 查询中的 cityId。
| 归档时间: |
|
| 查看次数: |
653 次 |
| 最近记录: |