3

Is there a way to use a "script_fields" in a nested query adding fields to the returned inner_hits? Example:

{
   "nested": {
      "inner_hits": {},
      "path": "companies",
      "score_mode": "sum",
      "query": {},
      "script_fields": {
         "overlap" : {
            "script": {
               "source": "................................",
               "params": {
                   "from": "2012-01-01",
                    "to": "2015-06-30"
                }
            }
         }                
      }         
}

I execute n nested queries passing to each one a set of specific params. The idea is having the source script to assign a value to the overlap field for each one of the inner hits based on the provided params.

Update after Val suggestion on adding script_fields into the inner hits

Looks like executing more nested queries on the same nested path defining different inner_hits makes ES to strip inner_hits matches. Example:

{
       "nested": {
          "inner_hits": {
            "script_fields": {
               "overlap" : {
                  "script": {
                     "source": "................................",
                        "params": {
                           "from": "2012-01-01",
                           "to": "2015-06-30"
                         }
                      }
                   }                
                 } 
          },
          "path": "companies",
          "score_mode": "sum",
          "query": {},

    },
    {
       "nested": {
          "inner_hits": {
            "script_fields": {
               "overlap" : {
                  "script": {
                     "source": "................................",
                        "params": {
                           "from": "2012-01-01",
                           "to": "2015-06-30"
                         }
                      }
                   }                
                 } 
          },
          "path": "companies",
          "score_mode": "sum",
          "query": {},

    } 

If a run more nested query like these I get the correct matches but not all the expected inner_hits are returned. I can obtain all the hits only giving a random name to the different inner_hits like:

"inner_hits": {
   "name": [random name]
   "script_fields": {
      "overlap" : {
         ......
      }
    }
 }   

Then I get the correct hits results but with some downside. I must check for duplicated results as hits are added to different result sets and are not uniq anymore. I have to parse the results using a regex for rebuilding a unified list.

Does this sounds like a bug?

1 Answer 1

5

Yes that's doable. you simply need to move the script_fields section inside inner_hits, like this:

{
   "nested": {
      "path": "companies",
      "score_mode": "sum",
      "query": {}.
      "inner_hits": {
         "script_fields": {
           "overlap" : {
             "script": {
               "source": "................................",
               "params": {
                   "from": "2012-01-01",
                    "to": "2015-06-30"
               }
             }
           }                
         }     
      }
   }              
}
Sign up to request clarification or add additional context in comments.

9 Comments

Yep this is working. Adding the script_fields into the inner_hits works ... even if is not documented. While I thought this was the answer to my issue my use case opens a new challenge as I need to execute n nested query and I MUST defined a n inner_hits {script_fields: ...} for each one of them since the params are I need to calculate the scripted fields are different by query. I have updated the example in the the question
Why not sending your queries in a multi-search?
Because I would have more "parent" matches while I want to match "parents" executing n query on the nested array and having back the hits with some extra calculated field. Make sense?
Actually, we should have closed this question since the initial need is solved and you should have created a new question with the new problem.
OK I will close and create a new question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.