# Shape mapping, only return certain type(s) on a query

**URL:** <https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552>\
**Category:** Elasticsearch\
**Created:** [February 5, 2024, 4:00pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552 "2024-02-05T16:00:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul5](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@paul5](https://discuss.elastic.co/u/paul5)\
**Post date:** [February 5, 2024, 4:00pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/1 "2024-02-05T16:00:03Z")

</div>

I don't see a way for a query on mapping type shape to only return certain shapes. Something like this:

```auto
POST /example/_doc
{
  "location" : {
    "type" : "point",
    "coordinates" : [-377.03653, 389.897676]
  },
 "return": {
  "type": "POLYGON"
}

```

Is this possible?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 5, 2024, 5:15pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/2 "2024-02-05T17:15:26Z")

</div>

Welcome!

No I don't think this is doable.  
The [geo\_shape query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html) will return whatever shape which is matching.

`type` sub field is not accessible for search but seems to be used only at index time.

So this does not work:

```auto
DELETE /example
PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

POST /example/_doc?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location": {
    "type": "point",
    "coordinates": [13.400544, 52.530286]
  }
}
GET /example/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "location.type": {
              "value": "point"
            }
          }
        },
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "envelope",
                "coordinates": [[ 13.0, 53.0], [14.0, 52.0] ]
              },
              "relation": "within"
            }
          }
        }
      ]
    }
  }
}

```

But if you copy the `type` field to another field, like `location_type`, this is working.

```auto
POST /example/_doc?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location_type": "point",
  "location": {
    "type": "point",
    "coordinates": [13.400544, 52.530286]
  }
}
GET /example/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "location_type": {
              "value": "point"
            }
          }
        },
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "envelope",
                "coordinates": [[ 13.0, 53.0], [14.0, 52.0] ]
              },
              "relation": "within"
            }
          }
        }
      ]
    }
  }
}

```

You might be able to automate that, using an ingest pipeline with a `set` processor...

---

<div class="post-metadata">

**Author:** ![paul5](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@paul5](https://discuss.elastic.co/u/paul5)\
**Post date:** [February 5, 2024, 7:09pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/3 "2024-02-05T19:09:04Z")

</div>

Darn. It would be really useful and powerful if this was a thing because then you could only return certain types without having to bloat the doc.

Thank you for looking into this!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 5, 2024, 8:19pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/4 "2024-02-05T20:19:29Z")

</div>

May be open an issue?

---

<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:** [February 6, 2024, 11:15am UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/5 "2024-02-06T11:15:45Z")

</div>

This can actually be solved using a runtime field. For example something like:

```auto
GET example/_search
{
  "runtime_mappings": {
    "geometry_type": {
      "type": "keyword",
      "script": {
        "source": "emit(params._source.location.type)"
      }
    }
  },
  "query": {
    "term": {
      "geometry_type": {
        "value": "point"
      }
    }
  }
}

```

This will be slow but you can add it to the mapping to get it indexed:

```auto
PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      },
      "geometry_type": {
        "type": "keyword",
        "script": {
          "source": "emit(params._source.location.type)"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![paul5](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@paul5](https://discuss.elastic.co/u/paul5)\
**Post date:** [February 7, 2024, 2:55pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/6 "2024-02-07T14:55:16Z")

</div>

> [@Ignacio\_Vera](#):
>
> `type`

Sorry but this did not work for me. When putting the mapping, I get this error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Root mapping definition has unsupported parameters: [geometry_type : {type=keyword, script={source=emit(params._source.location.type)}}] [location : {type=geo_shape}]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping: Root mapping definition has unsupported parameters: [geometry_type : {type=keyword, script={source=emit(params._source.location.type)}}] [location : {type=geo_shape}]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Root mapping definition has unsupported parameters: [geometry_type : {type=keyword, script={source=emit(params._source.location.type)}}] [location : {type=geo_shape}]"
    }
  },
  "status": 400
}

```

I am running elasticsearch 8.6.2

---

<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:** [February 7, 2024, 3:22pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/7 "2024-02-07T15:22:44Z")

</div>

Are you updating a mapping? then the syntax should belike:

```auto
PUT /example/_mapping
{
    "properties": {
      "location": {
        "type": "geo_shape"
      },
      "geometry_type": {
        "type": "keyword",
        "script": {
          "source": "emit(params._source.location.type)"
        }
      }
    }
}

```

---

<div class="post-metadata">

**Author:** ![paul5](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@paul5](https://discuss.elastic.co/u/paul5)\
**Post date:** [February 9, 2024, 2:16pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/8 "2024-02-09T14:16:54Z")

</div>

whoops, was being an idiot with my mapping, but this solution still doesn't work because some of my docs' geometry are in WKT, so I get this error

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "emit(params._source.location.type)",
          " ^---- HERE"
        ],
        "script": "emit(params._source.location.type)",
        "lang": "painless",
        "position": {
          "offset": 28,
          "start": 0,
          "end": 34
        }
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Error executing script on field [geometry_type]",
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "emit(params._source.location.type)",
        " ^---- HERE"
      ],
      "script": "emit(params._source.location.type)",
      "lang": "painless",
      "position": {
        "offset": 28,
        "start": 0,
        "end": 34
      },
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "dynamic getter [java.lang.String, type] not found"
      }
    }
  },
  "status": 400
}

```

Basically saying it is looking for type but in WKT, there is no type.

---

<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:** [February 12, 2024, 1:23pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/9 "2024-02-12T13:23:39Z")

</div>

I did not mean to give a full solution but a pointer how to overcome the issue. You can still support both formats with a more complex function:

```auto
PUT /example/_mapping
{
  "properties": {
    "location": {
      "type": "geo_shape"
    },
    "geometry_type": {
      "type": "keyword",
      "script": {
        "source": """
          if (params._source.location instanceof String) {
            def s = params._source.location;
            emit(s.substring(0, s.indexOf('(')).trim().toLowerCase(Locale.ROOT));
          } else {
             emit(params._source.location.type.toLowerCase(Locale.ROOT));
          }
          """
      }
    }
  }
}

```

---

<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:** [March 11, 2024, 1:24pm UTC](https://discuss.elastic.co/t/shape-mapping-only-return-certain-type-s-on-a-query/352552/10 "2024-03-11T13:24:26Z")

</div>

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