对包含脚本的function_score的has_parent查询有麻烦 [英] Trouble with has_parent query containing scripted function_score
问题描述
我有两种文件类型,在父子关系中:
I have two document types, in a parent-child relationship:
"myParent" : {
"properties" : {
"weight" : {
"type" : "double"
}
}
}
"myChild" : {
"_parent" : {
"type" : "myParent"
},
"_routing" : {
"required" : true
}
}
weight
字段用于自定义评分/排序。这个直接针对父文档的查询按照预期的方式工作:
The weight
field is to be used for custom scoring/sorting. This query directly against the parent documents works as intended:
{
"query" : {
"function_score" : {
"script_score" : {
"script" : "_score * doc['weight'].value"
}
}
}
}
但是,当尝试使用 has_parent
查询,我收到一个错误:
However, when trying to do similar scoring for the child documents with a has_parent
query, I get an error:
{
"query" : {
"has_parent" : {
"query" : {
"function_score" : {
"script_score" : {
"script" : "_score * doc['weight'].value"
}
}
},
"parent_type" : "myParent",
"score_type" : "score"
}
}
}
错误是:
QueryPhaseExecutionException [[myIndex] [3]:query [filtered(ParentQuery [myParent](已过滤(功能得分(ConstantScore(:),function = script [_score * doc ['weight']。value],params [null])) - > cache(_type:myParent)) - > cache(_type:myChild)],from [0] size [10]:查询失败[执行上下文重写失败]];嵌套:ElasticSearchIllegalArgumentException [在类型[myChild]]映射中找不到[weight]字段;
QueryPhaseExecutionException[[myIndex][3]: query[filtered(ParentQuery[myParent](filtered(function score (ConstantScore(:),function=script[_score * doc['weight'].value], params [null]))->cache(_type:myParent)))->cache(_type:myChild)],from[0],size[10]: Query Failed [failed to execute context rewrite]]; nested: ElasticSearchIllegalArgumentException[No field found for [weight] in mapping with types [myChild]];
似乎不是应用对父母的评分功能,然后将其结果传递给孩子,ES试图将评分功能本身应用于孩子,导致错误。
It seems like instead of applying the scoring function to the parent and then passing its result to the child, ES is trying to apply the scoring function itself to the child, causing the error.
如果我不对于 score_type
使用分数
,则不会发生错误,尽管结果分数全为 1.0
,记录在案。
If I don't use score
for score_type
, the error doesn't occur, although the results scores are then all 1.0
, as documented.
我在这里缺少什么?如何查询这些子文档,并根据父字段进行自定义评分?
What am I missing here? How can I query these child documents with custom scoring based on a parent field?
推荐答案
这将是一个错误:使用 myChild
映射作为默认上下文,即使您位于 has_parent
查询中。但我不知道修复错误会有多么容易。
This I would say is a bug: it is using the myChild
mapping as the default context, even though you are inside a has_parent
query. But I'm not sure how easy the bug would be to fix. properly.
但是,您可以通过在完整字段名称中包含类型
名称来解决问题: p>
However, you can work around it by including the type
name in the full field name:
curl -XGET "http://localhost:9200/t/myChild/_search" -d'
{
"query": {
"has_parent": {
"query": {
"function_score": {
"script_score": {
"script": "_score * doc[\"myParent.weight\"].value"
}
}
},
"parent_type": "myParent",
"score_type": "score"
}
}
}'
我发现了一个问题,看看我们是否可以获得这个固定的#4914
I've opened an issue to see if we can get this fixed #4914
这篇关于对包含脚本的function_score的has_parent查询有麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!