# Filtering/Querying by indexed geo\_shapes

**URL:** <https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800>\
**Category:** Elasticsearch\
**Created:** [July 3, 2019, 7:31pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800 "2019-07-03T19:31:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenjaminHenry](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@BenjaminHenry](https://discuss.elastic.co/u/BenjaminHenry)\
**Post date:** [July 3, 2019, 7:31pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/1 "2019-07-03T19:31:05Z")

</div>

Hi there,

I am attempting to filter or query a index with geo points by using a geo\_shape I have indexed already. For context, the "ais\_2016\_nov\_zone11\_enriched5" index is the one with geo points and the "ben\_clusters" index has geo\_shapes.

The following is what I am attempting to query with:

```auto
GET /ais_2016_nov_zone11_enriched5/_search
  {
    "query": {
        "geo_shape": {
            "location": {
               "relation" : "within",
                "indexed_shape": {
                        "index" : "ben_clusters",
                        "id": "b6qJs2sBrzkYGJItEBqq",
                        "type" : "_doc",
                        "path": "location"
                    }
                }
            }
        }
  }

```

And this is the error message I am receiving:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to find geo_shape field [location]",
        "index_uuid": "CP5DfnHITaK83JS-rlidHA",
        "index": "ais_2016_nov_zone11_enriched5"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "ais_2016_nov_zone11_enriched5",
        "node": "XGYWOAm0TvCS_ZY6wAVE_w",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to find geo_shape field [location]",
          "index_uuid": "CP5DfnHITaK83JS-rlidHA",
          "index": "ais_2016_nov_zone11_enriched5"
        }
      }
    ]
  },
  "status": 400
}

```

I was using this page for reference [https://www.elastic.co/guide/en/elasticsearch/guide/1.x/indexed-geo-shapes.html](https://www.elastic.co/guide/en/elasticsearch/guide/1.x/indexed-geo-shapes.html)

Any help would be appreciated. Also as a note, the geo\_shapes are being stored in a field named "location", and my geo\_points are being stored in a field named "GEO\_POINT".

---

<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:** [July 4, 2019, 6:39am UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/2 "2019-07-04T06:39:55Z")

</div>

You are looking to very old documentation, could you tell us which version of Elasticsearch you are using?

---

<div class="post-metadata">

**Author:** ![BenjaminHenry](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@BenjaminHenry](https://discuss.elastic.co/u/BenjaminHenry)\
**Post date:** [July 5, 2019, 11:47am UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/3 "2019-07-05T11:47:11Z")

</div>

I believe I am on version 7. By running GET / I receive the following in Dev Tools:

```auto
{
  "name" : "instance-0000000022",
  "cluster_name" : "de6583e6d4dc4032bb77e2ad6cf11d02",
  "cluster_uuid" : "o8LiGHC0QPOrALpcmWjVeg",
  "version" : {
    "number" : "7.0.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "e4efcb5",
    "build_date" : "2019-04-29T12:56:03.145736Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

```

---

<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:** [July 5, 2019, 11:54am UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/4 "2019-07-05T11:54:21Z")

</div>

You need to look into the documentation for your version:

[https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-shape-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-shape-query.html)

The `geo_shape` queries only work on `geo_shape` fields, therefore you cannot apply that query in a `geo_point` field.

That is what the error is telling you, it could not find a field called `location` of type `geo_shape`.

---

<div class="post-metadata">

**Author:** ![BenjaminHenry](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@BenjaminHenry](https://discuss.elastic.co/u/BenjaminHenry)\
**Post date:** [July 5, 2019, 12:26pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/5 "2019-07-05T12:26:30Z")

</div>

So from the documentation it seems like there is a point being stored as a geo\_shape, so I would need to reindex my current data into the geo\_shape type. Then run a filter for geoshape points inside the Polygon geoshapes, and then reindex them back into geo\_points to visualize them on a coordinate map?

---

<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:** [July 5, 2019, 12:28pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/6 "2019-07-05T12:28:57Z")

</div>

What you can do is to index them as both geo\_point and geo\_shape. Once you filter the documents using the geo\_shape field you will have the geo\_point field available if that is what you need to visualise it.

---

<div class="post-metadata">

**Author:** ![BenjaminHenry](https://avatars.discourse-cdn.com/v4/letter/b/eb8c5e/32.png) [@BenjaminHenry](https://discuss.elastic.co/u/BenjaminHenry)\
**Post date:** [July 8, 2019, 1:20pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/7 "2019-07-08T13:20:39Z")

</div>

I'm having a bit of trouble reindexing my points as geoshapes. I've tried the following to reindex

```auto
POST _reindex
{
  "source": {
    "index": "EnrichedDataSet"
  },
  "dest": {
    "index": "DataSetForPointShapes"
  },
  "script": {
    "source": "ctx._source.Shape_Point = 'POINT (ctx._source.LAT ctx._source.LON)'"
  }
}

```

and

```auto
POST _reindex
{
  "source": {
    "index": "enrichedDataSet"
  },
  "dest": {
    "index": "DataSetForPointShapes"
  },
  "script": {
    "source": "ctx._source.Shape_Point",
        "location": {
        "type": "point",
        "coordinates" : "[ctx.LAT , ctx.LON]"
    }
  }
}

Getting errors of "ctx is not a number" parsing error, and "[script] unknown field [location], parser not found"

I was hoping you could maybe shed some light on the proper syntax as the only references I have are for specific point inputs, which I cannot do manually.

References: 

https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html 

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html
```

---

<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:** [July 8, 2019, 1:28pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/8 "2019-07-08T13:28:44Z")

</div>

Here is my example, I have the following source mapping with geo point:

```auto
PUT /source
{
 "mappings": {
   "properties": {
      "location": {
        "type": "geo_point"
     }
   }
 }
}

```

And here is the target with geo\_shape:

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

```

To reindex from source to target I can do the following:

```auto
POST _reindex
{
  "source": {
    "index": "source"
  },
  "dest": {
    "index": "target"
  },
  "script": {
    "source": "ctx._source.location = 'POINT (' + ctx._source.location.lon + ' ' + ctx._source.location.lat + ')'"
  }
}

```

\*\* EDIT: Note the order of lat/lon

---

<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:** [August 5, 2019, 1:28pm UTC](https://discuss.elastic.co/t/filtering-querying-by-indexed-geo-shapes/188800/9 "2019-08-05T13:28:48Z")

</div>

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