Multiple sum aggregations using the same script

Hi, could use some advice here:

I need to calculate the weighted sums of sets of documents. The weight is based on the fields of the document so I'm using scripts, in a particular a sum aggregation with a script inside filter aggregations. The weights are fixed per call (i.e. all the sum aggregations use the exact same script and pararms).

I have implemented this but I'm not completely satisfied with the solution for 2 reasons:

  1. The script runs multiple times for the same document (for every filter the document matches).
  2. I need to pass the parameters for the script multiple times. This param object could get quite big, and I don't want to risk hitting ES max request size.

Anything I could do differently?

Hey,

I think it would help greatly if you shared the requests you created, so people could get a feeling what you are after.

Also, do you think it is possible to precalculate anything of this before storing the document, so you could add it to the document itself to be faster on the query side?

--Alex

Thanks for the reply @spinscale .

I've simplified the search request as my mapping is a bit more complex, but it boils down to this:

{
  "size": 0,
  "query": {
    "filtered": {
      "filter": {
        
        ... global filter 
        
      }
    }
  },
  "aggs": {
    "agg1": {
      "filter": {
        
        ... filter #1 
        
      },
      "aggs": {
        "metric": {
          "sum": {
            "script_file": "my_script",
            "params": {
              "factors": {
                "Male": 0.48,
                "Female": 1.8
              }
            }
          }
        }
      }
    },
    "agg2": {
      "filter": {
        
        ... filter #2 
        
      },
      "aggs": {
        "metric": {
          "sum": {
            "script_file": "my_script",
            "params": {
              "factors": {
                "Male": 0.48,
                "Female": 1.8
              }
            }
          }
        }
      }
    },
    ...
  }
}

About your suggestion - the weights are determined by the user, and not on index time (and it's very possible a later request will have different weights on the exact same data), so sadly no.

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