# Common points in two geo\_bounding\_box

**URL:** <https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387>\
**Category:** Elasticsearch\
**Created:** [February 1, 2023, 5:45am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387 "2023-02-01T05:45:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 1, 2023, 5:45am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/1 "2023-02-01T05:45:52Z")

</div>

I have two geo\_bounding\_box within these two shapes many points exist. i want only those points that are common in both geo\_bounding\_box.  
{  
"bool":{  
"should":[  
{  
"geo\_bounding\_box":{  
"geo\_coords":{  
"top\_left": {  
"lat": 33.25846,  
"lon": 70.01472  
},  
"bottom\_right": {  
"lat": 33.17837,  
"lon": 70.08681  
}  
}  
}  
},  
{  
"geo\_bounding\_box":{  
"geo\_coords":{  
"top\_left": {  
"lat": 33.27802,  
"lon": 70.09298  
},  
"bottom\_right": {  
"lat": 33.25311,  
"lon": 70.12799  
}  
}  
}  
}  
]  
}  
}

I have tried with must and match\_all but not succeeded.

---

<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 1, 2023, 6:13am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/2 "2023-02-01T06:13:34Z")

</div>

Have you tried setting the parameter `minimum_should_match` to 2?

> **[Boolean query | Elasticsearch Guide \[8.6\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/query-dsl-bool-query.html#bool-min-should-match)**

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 1, 2023, 6:40am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/3 "2023-02-01T06:40:39Z")

</div>

Hi @maya_khan Welcome to the community...

Can you provide a little more context?

What Version are you on?

Are your Points in an Index and you are looking for the points that interest these BBOXs?

can you give an example of your points?

Can you show the whole query, not just parts?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 1, 2023, 7:03am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/4 "2023-02-01T07:03:32Z")

</div>

Here is my working Example  
I used the Map  
Loaded some points in and Index  
Drew some Boundaries then Clean Up the Query  
If you want the points that are only in BOTH then `must`

```auto
# Put In some Points
DELETE /city

PUT /city
{
  "mappings": {
    "properties": {
      "name" : {"type" : "text"},
      "location": {
        "type": "geo_point"
      }
    }
  }
}

POST /city/_doc/
{
  "name": "stamford",
  "location": [-73.554212, 41.053923]
}

POST /city/_doc
{
  "name": "greenwich",
  "location": [-74.624998,42.047451]
}

POST /city/_doc
{
  "name": "armonk",
  "location": [-75.708180,43.126757]
}

POST /city/_doc
{
  "name": "north stamford",
  "location": [-76.572866,44.142307]
}

POST /city/_doc
{
  "name": "greenwich2",
  "location": [-73.681591,41.098201]
}

POST /city/_doc
{
  "name": "rye brook",
  "location": [-73.683025,41.017221]
}

GET city/_search

# Search with t Polygons
GET /city/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "location"
                }
              },
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "coordinates": [
                        [
                          [
                            -75.15639,
                            42.35149
                          ],
                          [
                            -75.15639,
                            40.53426
                          ],
                          [
                            -72.92849,
                            40.53426
                          ],
                          [
                            -72.92849,
                            42.35149
                          ],
                          [
                            -75.15639,
                            42.35149
                          ]
                        ]
                      ],
                      "type": "Polygon"
                    }
                  }
                }
              },
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "coordinates": [
                        [
                          [
                            -76.39068,
                            43.48569
                          ],
                          [
                            -76.39068,
                            41.76891
                          ],
                          [
                            -74.17725,
                            41.76891
                          ],
                          [
                            -74.17725,
                            43.48569
                          ],
                          [
                            -76.39068,
                            43.48569
                          ]
                        ]
                      ],
                      "type": "Polygon"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

# Results the single point in both

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "city",
        "_id": "se26C4YBNTELl2kaLo-a",
        "_score": 0,
        "_source": {
          "name": "greenwich",
          "location": [
            -74.624998,
            42.047451
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 1, 2023, 7:56am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/5 "2023-02-01T07:56:53Z")

</div>

Thanks i am using elastic version 7.17. i have many geo points containing information about persons in these two geo\_bounding\_box but i want only those persons that are common in these two polygons.  
i want such type of thing  
{  
"query": {  
"bool": {  
"filter": [  
{  
"bool": {  
"must": [  
{  
"exists": {  
"field": "location"  
}  
},  
{  
"geo\_shape": {  
"ignore\_unmapped": true,  
"location": {  
"relation": "INTERSECTS",  
"geo\_bounding\_box": {  
"top\_left": {  
"lat": -75.15639,  
"lon": 42.35149  
},  
"bottom\_right": {  
"lat": -72.92849,  
"lon": 40.53426  
}  
,  
"type": "Polygon"  
}  
}  
}  
},  
{  
"geo\_shape": {  
"ignore\_unmapped": true,  
"location": {  
"relation": "INTERSECTS",  
"geo\_bounding\_box": {  
"top\_left": {  
"lat": -76.39068,  
"lon": 43.48569  
},  
"bottom\_right": {  
"lat": -74.17725,  
"lon": 43.48569  
},  
"type": "Polygon"  
}  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}

but it gives error  
{  
"error": {  
"root\_cause": [  
{  
"type": "parsing\_exception",  
"reason": "query does not support [geo\_bounding\_box]",  
"line": 18,  
"col": 41  
}  
],  
"type": "x\_content\_parse\_exception",  
"reason": "[18:41] [bool] failed to parse field [filter]",  
"caused\_by": {  
"type": "x\_content\_parse\_exception",  
"reason": "[18:41] [bool] failed to parse field [must]",  
"caused\_by": {  
"type": "parsing\_exception",  
"reason": "query does not support [geo\_bounding\_box]",  
"line": 18,  
"col": 41  
}  
}  
},  
"status": 400  
}

---

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 1, 2023, 7:57am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/6 "2023-02-01T07:57:46Z")

</div>

yes but not succeeded

---

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 1, 2023, 11:31am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/7 "2023-02-01T11:31:55Z")

</div>

my two polygon shapes are far from each other not intersecting but i want the common points in both of them, eg name 'nelson' exist in both polygons so my result would only be geopoint whose name is 'nelson'  
thanks for quick response

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 1, 2023, 8:38pm UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/8 "2023-02-01T20:38:06Z")

</div>

> [@maya\_khan](#):
>
> my two polygon shapes are far from each other not intersecting but i want the common points in both of them, eg name 'nelson' exist in both polygons so my result would only be geopoint whose name is 'nelson'

I think this is the key probably because any 1 document will never be in 2 distinct / separate boxes... so there never will be a match, see below.

> [@Ignacio\_Vera](#):
>
> Have you tried setting the parameter `minimum_should_match` to 2?

I tried that but it does not work because any one point is not in both boxes... interesting....

I am not clear on how to do this because when the search executes it executes against a single document at a time and that single document will not be in 2 different disconnected boxes.

1 field like the name is in both but the location for any one document is not in both boxes... so I am not clear on how to do this... I will look at bit more... but I am at a loss at the moment

Here are my current samples  
`stamford` is in both boxes  
`armonk` is only in 1 box

`# "minimum_should_match" : 1 give both and even armonk`  
`# "minimum_should_match" : 2 does not give either `

```auto
DELETE /city

PUT /city
{
  "mappings": {
    "properties": {
      "name" : {"type" : "keyword"},
      "location": {
        "type": "geo_point"
      }
    }
  }
}

POST /city/_doc/
{
  "name": "stamford",
  "location": [-73.554212, 41.053923]
}

POST /city/_doc
{
  "name": "greenwich",
  "location": [-74.624998,42.047451]
}

POST /city/_doc
{
  "name": "armonk",
  "location": [-75.708180,43.126757]
}

POST /city/_doc
{
  "name": "stamford",
  "location": [-75.718180,43.136757]
}

POST /city/_doc
{
  "name": "north stamford",
  "location": [-76.572866,44.142307]
}

POST /city/_doc
{
  "name": "greenwich2",
  "location": [-73.681591,41.098201]
}

POST /city/_doc
{
  "name": "rye brook",
  "location": [-73.683025,41.017221]
}

# Here is the should but results return for stamford (in both) armonk only 1 
# "minimum_should_match" : 1 give both and even armonk
# "minimum_should_match" : 2 does not give either  

GET /city/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "location"
                }
              },
              {
                "term": {
                  "name": {
                    "value": "stamford" <!-- test with armonk too
                  }
                }
              }
            ],
            "should": [
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "coordinates": [
                        [
                          [
                            -75.15639,
                            42.35149
                          ],
                          [
                            -75.15639,
                            40.53426
                          ],
                          [
                            -72.92849,
                            40.53426
                          ],
                          [
                            -72.92849,
                            42.35149
                          ],
                          [
                            -75.15639,
                            42.35149
                          ]
                        ]
                      ],
                      "type": "Polygon"
                    }
                  }
                }
              },
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "type": "Polygon",
                      "coordinates": [
                        [
                          [
                            -75.80553,
                            43.20558
                          ],
                          [
                            -75.80553,
                            43.03916
                          ],
                          [
                            -75.55147,
                            43.03916
                          ],
                          [
                            -75.55147,
                            43.20558
                          ],
                          [
                            -75.80553,
                            43.20558
                          ]
                        ]
                      ]
                    }
                  }
                }
              }
            ],
            "minimum_should_match" : 1
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 2, 2023, 4:34am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/9 "2023-02-02T04:34:05Z")

</div>

Actually i am working on vehicle traveling system. i have created two fences on map which will give me data in form of 4 points coordinates or geo\_bounding\_box. i have data of complete vehicle movement now i have to extract the information of common cars that visited two different locations in same day. i hope u get my point now.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 2, 2023, 4:56am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/10 "2023-02-02T04:56:23Z")

</div>

I do... My point is that I do not know how to solve that in a single query.

I think my example is the same basic concept ... Which I do not have a solution for at this time.

I suspect you have a time component, but the single query as well... But still I don't know how to solve in a single query at this point.

Perhaps try there is another way to solve by passing the point into an index with all the fences and the get all the fences they belong to... Just be thinking but in the form we are looking at, I don't have a solution

Perhaps @Ignacio_Vera will have an idea.

---

<div class="post-metadata">

**Author:** ![maya\_khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maya_khan/32/116629_2.png) [@maya\_khan](https://discuss.elastic.co/u/maya_khan)\
**Post date:** [February 2, 2023, 7:00am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/11 "2023-02-02T07:00:17Z")

</div>

hello,  
thanks for your reply and quick response plz just check my scenario.

```auto
PUT vts
PUT vts/_mappings
 {
    "properties": {
      "location": {
        "type": "geo_point"
      },
      "vehicleid": {
        "type": "text"
      },
      "datetime": {
        "type": "date"
      }
    }
  }
  
  -----------------------------
 POST vts/_doc
  {
  "vehicleid": "idj 123",
  "location": [72.06059, 30.21495],
  "datetime": "2023-01-01T10:23:00"
}

 POST vts/_doc
  {
  "vehicleid": "idj 123",
  "location": [72.11071, 30.26949],
  "datetime": "2023-01-01T10:25:00"
}

POST vts/_doc
  {
  "vehicleid": "idj 456",
  "location": [72.07397, 30.23896],
  "datetime": "2023-01-01T10:25:00"
}

------------------------------
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "location"
                }
              }
            ],
            "should": [
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "coordinates": [
                        [
                          [
          72.06568,
          30.24528
        ],
        [
          72.04422,
          30.21517
        ],
        [
          72.06757,
          30.2036
        ],
        [
          72.09486,
          30.24009
        ],
        [
          72.06568,
          30.24528
        ]
                        ]
                      ],
                      "type": "Polygon"
                    }
                  }
                }
              },
              {
                "geo_shape": {
                  "ignore_unmapped": true,
                  "location": {
                    "relation": "INTERSECTS",
                    "shape": {
                      "type": "Polygon",
                      "coordinates": [
                        [
                           [
          72.10207,
          30.27938
        ],
        [
          72.09211,
          30.26752
        ],
        [
          72.10928,
          30.25951
        ],
        [
          72.12318,
          30.27448
        ],
        [
          72.10207,
          30.27938
        ]
                        ]
                      ]
                    }
                  }
                }
              }
            ],
            "minimum_should_match" : 1
          }
        }
      ]
    }
  }
}

```

the result should only give doc with "vehicleid": "idj 123" as it is present in both polygons . but now it is giving all documents.  
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:** [March 2, 2023, 7:00am UTC](https://discuss.elastic.co/t/common-points-in-two-geo-bounding-box/324387/12 "2023-03-02T07:00:46Z")

</div>

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