5

I have the following query which currently uses dynamic scripting. I have since found that my host doesn't support this, as it has wider reaching security implications. How would I rewrite this script so that it doesn't use dynamic script?

{
  "size": 0,
  "aggs": {
    "filtered_cells": {
      "filter": {
        "geo_bounding_box": {
          "loc": {
            "top_left": "58.645976, -13.515625",
            "bottom_right": "50.524473, 2.436523"
          }
        }
      },
      "aggs": {
        "cells": {
          "geohash_grid": {
            "field": "loc",
            "precision": 2
          },
          "aggs": {
            "center_lat": {
              "avg": {
                "script": "doc['loc'].lat"
              }
            },
            "center_lon": {
              "avg": {
                "script": "doc['loc'].lon"
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "match_all": {}
  }
}
1
  • Can you move the script to file? Or, better said, does your host allow you to place .groovy files inside the ES installation directory? Commented May 28, 2015 at 11:45

2 Answers 2

2
+50

You can store your scripts on the file system and reference them from within your query/aggregations.

Create a file named config/scripts/lat.groovy with the following content

 doc['loc'].lat

Create another file named config/scripts/lon.groovy with the following content

 doc['loc'].lon

Then change your query to this:

{
  "size": 0,
  "aggs": {
    "filtered_cells": {
      "filter": {
        "geo_bounding_box": {
          "loc": {
            "top_left": "58.645976, -13.515625",
            "bottom_right": "50.524473, 2.436523"
          }
        }
      },
      "aggs": {
        "cells": {
          "geohash_grid": {
            "field": "loc",
            "precision": 2
          },
          "aggs": {
            "center_lat": {
              "avg": {
                "script_file": "lat"
              }
            },
            "center_lon": {
              "avg": {
                "script_file": "lon"
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "match_all": {}
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

"Indexing your scripts" will not work if dynamic scripting is disabled ;-).
1

Apart from placing the actual script on a .groovy file, like I mentioned (more details here), you could define a native script. More involved than the groovy-on-file approach but is much more flexible. In your case, the script is really simple :-) and you don't need the flexibility though, but the option exists (implementation sample from yours truly): ElasticSearch: aggregation on _score field w/ Groovy disabled

Comments

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.