3
votes

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

1 Answers

4
votes

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