包含脚本 function_score 的 has_parent 查询出现问题 [英] Trouble with has_parent query containing scripted function_score

查看:20
本文介绍了包含脚本 function_score 的 has_parent 查询出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种文档类型,处于父子关系中:

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](filtered(function score (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使用score,则不会发生错误,尽管结果分数都是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 映射作为默认上下文,即使您在 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.

但是,您可以通过在完整字段名称中包含 type 名称来解决此问题:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆