Maybe this can help. However, the project is configured in kilometers. You can change them in CityDAO.java
public List<City> findCityInRange(GeoPoint geoPoint, double distance) { List<City> cities = new ArrayList<City>(); QueryBuilder queryBuilder = geoDistanceQuery("geoPoint") .point(geoPoint.getLat(), geoPoint.getLon()) //.distance(distance, DistanceUnit.KILOMETERS) original .distance(distance, DistanceUnit.MILES) .optimizeBbox("memory") .geoDistance(GeoDistance.ARC); SearchRequestBuilder builder = esClient.getClient() .prepareSearch(INDEX) .setTypes("city") .setSearchType(SearchType.QUERY_THEN_FETCH) .setScroll(new TimeValue(60000)) .setSize(100).setExplain(true) .setPostFilter(queryBuilder) .addSort(SortBuilders.geoDistanceSort("geoPoint") .order(SortOrder.ASC) .point(geoPoint.getLat(), geoPoint.getLon()) //.unit(DistanceUnit.KILOMETERS)); Original .unit(DistanceUnit.MILES)); SearchResponse response = builder .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); scroll: while (true) { for (SearchHit hit : hits) { Map<String, Object> result = hit.getSource(); cities.add(mapper.convertValue(result, City.class)); } response = esClient.getClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet(); if (response.getHits().getHits().length == 0) { break scroll; } } return cities; }
The file "LocationFinder \ src \ main \ resources \ json \ cities.json" contains all cities from Belgium. You can also delete or create entries if you wish. As long as you do not change the names and / or structure, no code changes are required.
Be sure to read README https://github.com/GlennVanSchil/LocationFinder
Glenn van schil
source share