From b63c5b9f0e400b1118f9863fb3946a5103d24b6a Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Tue, 6 Sep 2016 11:23:36 +0200 Subject: [PATCH] Sort result list by distance to user position (#261) --- app/controllers/Application.java | 33 ++++++++++++++++++++++++++------ app/views/main.scala.html | 30 +++++++++++++++++++++++++++++ conf/routes | 3 +++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/app/controllers/Application.java b/app/controllers/Application.java index ad697f1d..7e484ef2 100644 --- a/app/controllers/Application.java +++ b/app/controllers/Application.java @@ -12,12 +12,14 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.query.GeoPolygonQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.sort.GeoDistanceSortBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -140,9 +142,10 @@ public static Result search(String q, String location, int from, int size, final String responseFormat = Accept.formatFor(format, request().acceptedTypes()); try { - String cacheKey = - String.format("q=%s,location=%s,from=%s,size=%s,format=%s,lang=%s", q, - location, from, size, responseFormat, lang().code()); + String cacheKey = String.format( + "q=%s,location=%s,from=%s,size=%s,format=%s,lang=%s,position=%s", q, + location, from, size, responseFormat, lang().code(), + session("position")); Result cachedResult = (Result) Cache.get(cacheKey); if (cachedResult != null && responseFormat.equals("html")) { return cachedResult; @@ -304,13 +307,21 @@ private static String buildDistanceQuery(String q, int from, int size, } static SearchResponse executeQuery(int from, int size, QueryBuilder query) { - SearchRequestBuilder searchRequest = Index.CLIENT.prepareSearch(ES_NAME) - .setTypes(ES_TYPE).setSearchType(SearchType.QUERY_THEN_FETCH) - .setQuery(query).setFrom(from).setSize(size); + SearchRequestBuilder searchRequest = + Index.CLIENT.prepareSearch(ES_NAME).setTypes(ES_TYPE)// + .setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(query)// + .setFrom(from)// + .setSize(size); searchRequest = withAggregations(searchRequest, "type.raw", localizedLabel("classification.label.raw"), localizedLabel("fundertype.label.raw"), localizedLabel("stocksize.label.raw")); + String position = session("position"); + if (position != null) { + Logger.info("Sorting by distance to current position {}", position); + searchRequest.addSort(new GeoDistanceSortBuilder("location.geo") + .points(new GeoPoint(position))); + } return searchRequest.execute().actionGet(); } @@ -363,6 +374,16 @@ public static Promise get(String id, String format) { : notFound("Not found: " + id)); } + /** + * @param lat The position's latitude + * @param lon The position's longitude + * @return 200 OK, after storing the position in the session + */ + public static Result setPosition(String lat, String lon) { + session("position", lat + "," + lon); + return ok("Position set to " + session("position")); + } + private static Result resultFor(String id, JsonNode json, String format) { Map> results = new HashMap<>(); results.put("html", diff --git a/app/views/main.scala.html b/app/views/main.scala.html index 28291471..9feeb78f 100644 --- a/app/views/main.scala.html +++ b/app/views/main.scala.html @@ -61,5 +61,35 @@ @scripts + diff --git a/conf/routes b/conf/routes index 4f97cf41..246bef5b 100644 --- a/conf/routes +++ b/conf/routes @@ -23,5 +23,8 @@ GET /organisations/public/*file controllers.Assets.at(path="/pu # Indexing POST /organisations/index controllers.Index.start() +# Position +POST /organisations/position controllers.Application.setPosition(lat, lon) + # Transformation POST /organisations/transform controllers.Transformation.startTransformation()