Skip to content

Commit

Permalink
Sort result list by distance to user position (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 7, 2016
1 parent 4547f7a commit b63c5b9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
33 changes: 27 additions & 6 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -363,6 +374,16 @@ public static Promise<Result> 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<String, Supplier<Result>> results = new HashMap<>();
results.put("html",
Expand Down
30 changes: 30 additions & 0 deletions app/views/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,35 @@
<script src='@controllers.routes.Assets.at("javascripts/jquery-1.10.2.min.js")'></script>
<script src='@controllers.routes.Assets.at("javascripts/bootstrap.min.js")'></script>
@scripts
<script>
window.onload = function() {
var options = {
maximumAge: 5 * 60 * 1000,
timeout: 10 * 1000
}
var success = function(position) {
console.log(position);
var url = '/organisations/position?lat=' + position.coords.latitude + '&lon=' + position.coords.longitude;
$.ajax({
url: url,
type: 'POST',
success: function(data, textStatus, jqXHR) {
console.log('Server response: ' + data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + ": " + errorThrown);
}
});
};
var error = function(error) {
console.log(error);
};
@if(play.mvc.Controller.session("position") == null) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, options);
}
}
};
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit b63c5b9

Please sign in to comment.