# Geospatial query with aggregations does not return any results ES7.6

**URL:** <https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762>\
**Category:** Elasticsearch\
**Created:** [July 10, 2020, 8:37pm UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762 "2020-07-10T20:37:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmanohar1](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vmanohar1](https://discuss.elastic.co/u/vmanohar1)\
**Post date:** [July 10, 2020, 8:37pm UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/1 "2020-07-10T20:37:57Z")

</div>

`1) MY template for indices of type myindex-YYYY-MM-DD is -`

```auto
PUT _template/myindex_template
{
    "index_patterns" : ["myindex-*"],
    "aliases" : {
      "myindex_alias" : {}
    },
    "settings": {
    "number_of_replicas": 2,
    "number_of_shards": 2,
    "analysis": {},
    "refresh_interval": "1m",
    "index.mapping.ignore_malformed": true
   },
    "mappings": {
            "properties": {
                "my_id" : {"type" : "long"},
                "my_ts" : {"type": "date"},
                "location1" : {"type": "geo_shape"},
                "prev_my_ts" : {"type": "date"},
                "prev_location1" : {"type": "geo_shape"}
            }
        }
} <---- Here my_id,my_ts gives uniqueness(mentioning for understanding purposes.

```

`2) My Query is -->`

```auto
GET /myindex_alias/_search
{
  "size" : 0,
  "query": {
    "bool": {
      "must": {
        "range": {
          "my_ts": {
            "gte": "2020-06-05T00:00:59",
            "lte": "2020-06-12T21:59:59"
          }
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "geo_shape": {
                      "location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "disjoint"
                      }
                    }
                  },
                  {
                    "geo_shape": {
                      "prev_location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "within"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "geo_shape": {
                      "location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "within"
                      }
                    }
                  },
                  {
                    "geo_shape": {
                      "prev_location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "disjoint"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "dist_id": {
      "terms": {
        "field": "my_id",
        "size": 10000
      }
    }
  }
}

```

1. `Index my_polygons has predefined polygon - mypoly_1.`  
`The query mentioned in 2) above, used to work fine in a bit earlier versions than 7.6.`  
`I have millions of rows under myindex_alias related indices(each index is daily index) used in the query.`

`The query is trying to get unique list of my_id for a given time-range for cases where (location1 is inside polygon mypoly_1 and prev_location1 is outside) or the opposite.`

`Not sure why it does not give any results ( I am certain that it is supposed to give results).`

Result is -

```auto
{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 98,
    "successful" : 98,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "dist_id" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : []
    }
  }
}

```

`when it should be giving unique values if my_id (along with count for each)..``

`I do not get any errors when I run the query. Is there some setting I am missing here? Has anything changed?? I am certain that same query worked in earlier versions of ES (including I think 7.4).`

`Please review. Thanks.`

---

<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 11, 2020, 8:36am UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/2 "2020-07-11T08:36:02Z")

</div>

I have tried to reproduce the issue and it works for me. This is what I have done:

I started a 7.6.0 cluster. First I create the `my_polygons` index with a simple polygon that contains point at (0,0):

```auto
PUT my_polygons

POST my_polygons/_mapping
{
  "properties":
    {
      "polygon_shape": {
        "type": "geo_shape"
      }
    }
}

POST my_polygons/_doc/mypoly_1
{
    "polygon_shape": "POLYGON((-1 -1, 1 -1, 1 1, -1 1, -1 -1))"
}

```

Second, a create the template:

```auto
PUT _template/myindex_template
{
    "index_patterns" : ["myindex-*"],
    "aliases" : {
      "myindex_alias" : {}
    },
    "settings": {
    "number_of_replicas": 2,
    "number_of_shards": 2,
    "analysis": {},
    "refresh_interval": "1m",
    "index.mapping.ignore_malformed": true
   },
    "mappings": {
            "properties": {
                "my_id" : {"type" : "long"},
                "my_ts" : {"type": "date"},
                "location1" : {"type": "geo_shape"},
                "prev_my_ts" : {"type": "date"},
                "prev_location1" : {"type": "geo_shape"}
            }
        }
}

```

Then I create an index that matches the template with three documents:

```auto
# should match
POST myindex-test/_doc
{
"my_id" : 1,
"my_ts" : "2020-07-02",
"location1" : "POINT(0 0)",
"prev_my_ts" : "2020-07-01",
"prev_location1" : "POINT(100 0)"
}

# should match
POST myindex-test/_doc
{
"my_id" : 2,
"my_ts" : "2020-07-02",
"location1" : "POINT(100 0)",
"prev_my_ts" : "2020-07-01",
"prev_location1" : "POINT(0 0)"
}

# should not match
POST myindex-test/_doc
{
"my_id" : 3,
"my_ts" : "2020-07-02",
"location1" : "POINT(100 0)",
"prev_my_ts" : "2020-07-01",
"prev_location1" : "POINT(50 0)"
}

POST myindex-test/_refresh

```

Execute the query:

```auto

GET /myindex_alias/_search
{
  "size" : 0,
  "query": {
    "bool": {
      "must": {
        "range": {
          "my_ts": {
            "gte": "2020-06-05T00:00:59",
            "lte": "2020-07-12T21:59:59"
          }
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "geo_shape": {
                      "location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "disjoint"
                      }
                    }
                  },
                  {
                    "geo_shape": {
                      "prev_location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "within"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "geo_shape": {
                      "location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "within"
                      }
                    }
                  },
                  {
                    "geo_shape": {
                      "prev_location1": {
                        "indexed_shape": {
                              "index": "my_polygons",
                              "id": "mypoly_1",
                              "path": "polygon_shape"
                        },
                        "relation": "disjoint"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "dist_id": {
      "terms": {
        "field": "my_id",
        "size": 10000
      }
    }
  }

```

I get the expected results:

```auto
{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "dist_id" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        },
        {
          "key" : 2,
          "doc_count" : 1
        }
      ]
    }
  }
}

```

Could you provide an example that reproduces your issue?

---

<div class="post-metadata">

**Author:** ![vmanohar1](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vmanohar1](https://discuss.elastic.co/u/vmanohar1)\
**Post date:** [July 12, 2020, 11:54am UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/3 "2020-07-12T11:54:25Z")

</div>

It can be data issue itself.. Let me verify that for a few days..  
I had tried with fake small data and it works.. Thanks for replying. I will update in a few days.

---

<div class="post-metadata">

**Author:** ![vmanohar1](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vmanohar1](https://discuss.elastic.co/u/vmanohar1)\
**Post date:** [July 13, 2020, 3:06pm UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/4 "2020-07-13T15:06:11Z")

</div>

It seems to be data issue!! We are getting corrected data from the stream again..  
Thank you for reviewing this ticket.

---

<div class="post-metadata">

**Author:** ![vmanohar1](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vmanohar1](https://discuss.elastic.co/u/vmanohar1)\
**Post date:** [July 14, 2020, 6:27pm UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/5 "2020-07-14T18:27:49Z")

</div>

Verified that all works after correction in data feed.. Ticket can be closed. Thanks.

---

<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 11, 2020, 6:27pm UTC](https://discuss.elastic.co/t/geospatial-query-with-aggregations-does-not-return-any-results-es7-6/240762/6 "2020-08-11T18:27:50Z")

</div>

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