# Painless script with GeoShape type query

**URL:** <https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 13, 2022, 7:43am UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647 "2022-05-13T07:43:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kindchri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kindchri/32/59044_2.png) [@kindchri](https://discuss.elastic.co/u/kindchri)\
**Post date:** [May 13, 2022, 7:43am UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/1 "2022-05-13T07:43:08Z")

</div>

I'm trying to create a runtime field in a search request [Link to documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html).

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!

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [May 13, 2022, 10:41am UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/2 "2022-05-13T10:41:49Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![kindchri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kindchri/32/59044_2.png) [@kindchri](https://discuss.elastic.co/u/kindchri)\
**Post date:** [May 13, 2022, 11:14am UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/3 "2022-05-13T11:14:02Z")

</div>

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?

```auto
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
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [May 13, 2022, 11:57am UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/4 "2022-05-13T11:57:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kindchri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kindchri/32/59044_2.png) [@kindchri](https://discuss.elastic.co/u/kindchri)\
**Post date:** [May 13, 2022, 12:23pm UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/5 "2022-05-13T12:23:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [May 25, 2022, 2:40pm UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/6 "2022-05-25T14:40:21Z")

</div>

Hi @kindchri you can open a feature request by going to the ES github repro, choose [new issue](https://github.com/elastic/elasticsearch/issues/new/choose), then select **Feature Request**.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 22, 2022, 2:40pm UTC](https://discuss.elastic.co/t/painless-script-with-geoshape-type-query/304647/7 "2022-06-22T14:40:55Z")

</div>

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