Nested script scoring


(Sankalp Gupta) #1

Hi
This is first time I'm posting so let me know if its less understandable.
I have a nested document structure

"question":{
    "questionId":{"type":"integer"},
    "userId":{"type":"integer"},
    "action": {
            "type":"nested",
            "properties": {
              "created": {"type": "date"},
              "interactionType": {"type": "text"},  //values can be answer,upvote etc
              "userId": {"type": "integer"},
              "score":{"type":"long"}
              }
   ....//other properties
  }

Now I want to do scoring of question on several functions. One of the function is:

  • iterate of every action and its type.

Actual query is little more complex. But bascially what I like to see is
that this query should return me the val= sum of scores of (all/script filtered) nested structures.

{
  "query": {"function_score": {
    "query": {"bool": {"should": [
      {"function_score": {"boost_mode": "max",
        "query": {"terms": {
        "questionId": [
          1,2,3,4,5,6,7,8,9,10
        ]
      }},
        "functions": [
          {"weight": 0}
        ]
      }},{
        "nested": {
          "path": "action",
          "query": {"bool": {"must": [
            {
            "function_score": {
              "query": {"script": {
                "script": "doc['userId'].value !=  doc['action.userId'].value"
              }},
              "functions": [
                {"weight": 0}
              ]
            }
          },{
            "function_score": {
            "query": {"bool": {"must": [
              {"terms": {
            "action.userId": [
              1100,1200,1300,1400,1500,1600,1700,1800
            ]
          }}
            ]}},
            "functions": [
              {"script_score": {
                "script": {
                  "inline": "double val=0; for(item in doc['action.score']){ val+=score;} return val;"
                }
              }}
            ]
          }}]}}
        }
      }
    ]}},
    "functions": [{
      "weight": 0}
    ],"boost_mode": "sum","score_mode":"sum" 
  }}
}  

For eg. 
question:{
    questionId:1,
    userId:1,
    action:[{
        created:null,
        interactionType: "answer",
        userId:1100,
        score:100},
     {
       created:null,
        interactionType: "upvote",
        userId:1200,
        score:10
     }]

Should return a val=200. But its actually returning 100.
It might due to the reason that every nested query is executing separately.
Can you please suggest a way to achieve this?

Please don't suggest me to index score at question level as going ahead I'll be enhancing the script to filter out certain nested documents or other complexities in the script score inline method.


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.