Painless script with GeoShape type query

I'm trying to create a runtime field in a search request Link to documentation.

In the script for the field I need to check if a geo point (lat, lng) in a field on the document is within a poiygon/bounding box that is provided as a parameter to the script.

I have tried to find information on this but have failed to find the solution so far.
Does anyone know if this is possible?

Thank you in advance!

Could you share what you have tried so far? I am not sure I fully understand what you are trying to do.

Thanks!

Below is a sample query of what I want to achieve.
Customers have a list of purchases 'Purchases' where each purchase has a type, price and location (geo_point).

I then want to create a sum field of the purchases that match my criteria on type and location (purchase location within a polygon). These will change with each request.

Then I want to filter on that summary field.

So far I can create the summary field and only summarize purchases matching types I provide but I need to match them on location as well. The second condition in the script below is completely made up. My question is if that method (polygon contains geo point) exists in painless and if it does, what the syntax looks like?

GET customers/_search
{
  "runtime_mappings": {
    "sumPriceOfMatchingPurchases": {
      "type": "long",
      "script": {
        "lang": "painless",
        "source": """
        int sumPrice = 0;
        for(p in params._source['Purchases']){
          if(params.purchaseTypes.contains(p.type) && params.geographicalArea.contains(p.location))) {
            sumPrice += p.price;
          }
        } 
        emit(sumPrice);
      """,
        "params": {
          "purchaseTypes": [
            220,
            320
          ],
          "geographicalArea": "POLYGON(coordinates)"
        }
      }
    }
  },
   "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "sumPriceOfMatchingPurchases": {
              "gte": 10,
              "lte": 200
            }
          }
        }
      ]
    }
  }
}

That's currently not supported, painless does not support spatial relationship methods so you cannot check easily if a geo_point is inside of a polygon. Maybe you might want to open a feature request in the Elasticsearch repository?

Okay, thank you for the answer!

Another way to solve the problem might be to map the Purchases as type nested.
Then filter on type and location in a normal nested query.
Then in post_filter summarize the inner_hits in a painless script that also performs the range filtering.

I don't think that inner_hits are available in scripts in post_filter though.

Some way to do this kind of thing would be really nice.
How do I open a feature request?

Hi @kindchri you can open a feature request by going to the ES github repro, choose new issue, then select Feature Request.

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