Common points in two geo_bounding_box

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.

Have you tried setting the parameter minimum_should_match to 2?

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?

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

# 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
          ]
        }
      }
    ]
  }
}

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
}

yes but not succeeded

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

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.

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

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

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.

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.

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

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

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