Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

elasticsearch - 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
  }
}

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"
      }                 
    }                                                                       
  }    
}

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"
    }
  }
}

The error is:

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]];

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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"
    }
  }
}'

I've opened an issue to see if we can get this fixed #4914


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...