How to get intersecting points from a form in ElasticSearch

I saved the route in ElasticSearch as a polygon. Now I have a circle (point and radius), I can check that the points of the circle intersect the polygon or not (below is the code that I used).

Question: How can I get points in a route that intersects a circle?

Route and circle

 public Boolean isMatchingDoc(Long elasticDocId, Double latitude, Double longitude, Long radius) { Coordinate origin = new Coordinate(latitude, longitude); ShapeBuilder circleShapeBuilder = ShapeBuilder.newCircleBuilder().center(origin).radius(radius, DistanceUnit.METERS); GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("route", circleShapeBuilder); SearchRequestBuilder finalQuery = client.prepareSearch(INDEX).setTypes(TYPE) .setQuery(QueryBuilders.termQuery("_id", elasticDocId)).setPostFilter(geoShapeQueryBuilder); SearchResponse searchResponse = finalQuery.execute().actionGet(); SearchHits searchHits = searchResponse.getHits(); if (searchHits.getTotalHits() > 0) { return true; } return false; } 
+7
java elasticsearch geo
source share
1 answer

I think you know that with elasticsearch you can query the polygons that intersect a given circle? See https://www.elastic.co/guide/en/elasticsearch/guide/current/querying-geo-shapes.html .

There are two reasons why this may not help you:

  • Your routes are not polygons, but lines.
  • You want to know the exact intersection point if I read your question correctly.

Elasticsearch probably cannot solve this problem conveniently for you. Perhaps it will be possible to decide if you save all your segments separately, and not in one huge polygon for one route. Each line segment must have an attribute that refers to the route to which it belongs. Is this approach right for you?

In any case, I would recommend that you study the topic of "spatial databases": Spatial databases are optimized for indexing and searching in geometric space. Well-known databases such as PostgreSQL and MongoDB contain plugins / extensions for spatial indexing. I'm not sure what to recommend, but the MongoDB geospatial API looks promising, for example, since it allows you to request intersection - and it supports lines as well as polygons.

0
source share

All Articles