Skip to content

Commit

Permalink
If we have the user's position, show map pins for current page only
Browse files Browse the repository at this point in the history
See #261
  • Loading branch information
fsteeg committed Sep 16, 2016
1 parent 227d63d commit bd16061
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
17 changes: 13 additions & 4 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
import org.elasticsearch.search.sort.GeoDistanceSortBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -312,7 +313,7 @@ static SearchResponse executeQuery(int from, int size, QueryBuilder query) {
.setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(query)//
.setFrom(from)//
.setSize(size);
searchRequest = withAggregations(searchRequest, "type.raw",
searchRequest = withAggregations(from, size, searchRequest, "type.raw",
localizedLabel("classification.label.raw"),
localizedLabel("fundertype.label.raw"),
localizedLabel("stocksize.label.raw"));
Expand All @@ -325,15 +326,23 @@ static SearchResponse executeQuery(int from, int size, QueryBuilder query) {
return searchRequest.execute().actionGet();
}

private static SearchRequestBuilder withAggregations(
private static SearchRequestBuilder withAggregations(int from, int size,
final SearchRequestBuilder searchRequest, String... fields) {
Arrays.asList(fields).forEach(field -> {
searchRequest
.addAggregation(AggregationBuilders.terms(field.replace(".raw", ""))
.field(field).size(Integer.MAX_VALUE));
});
searchRequest.addAggregation(AggregationBuilders.terms("location.geo")
.script(new Script("location-aggregation")).size(Integer.MAX_VALUE));
TopHitsBuilder topHitsBuilder = AggregationBuilders.topHits("location.geo")
.addScriptField("pin", new Script("location-aggregation"))
.setSize(Integer.MAX_VALUE);
String position = session("position");
if (position != null) {
topHitsBuilder.setFrom(from).setSize(size)
.addSort(new GeoDistanceSortBuilder("location.geo")
.points(new GeoPoint(position)));
}
searchRequest.addAggregation(topHitsBuilder);
return searchRequest;
}

Expand Down
11 changes: 5 additions & 6 deletions app/views/facet_map.scala.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ window.onload = addMarkerLayer;
function addMarkerLayer(){
var addedLocations = [];
var latLngObjects = [];
@for(bucket <- (Json.parse(queryMetadata) \ "aggregations" \ "location.geo" \ "buckets").as[Seq[JsObject]];
Array(isil, latLon, name, classification, _*) = (bucket \ "key").as[String].split(";;;");
if isil != "null" && latLon != "null" && name != "null" && classification != "null";
freq = (bucket \ "doc_count").as[JsNumber]) {
@for(pin <- (Json.parse(queryMetadata) \\ "pin");
Array(isil, latLon, name, classification, _*) = pin.as[JsArray].value.head.as[String].split(";;;");
if isil != "null" && latLon != "null" && name != "null" && classification != "null") {
var iconLabel = '@Application.CONFIG.getObject("organisation.icons").getOrDefault(classification, ConfigValueFactory.fromAnyRef("library")).unwrapped()';
addMarker('/organisations/@isil', '@latLon', '@freq', '@name', iconLabel);
addMarker('/organisations/@isil', '@latLon', '@name', iconLabel);
}
map.addLayer(markers);

Expand All @@ -87,7 +86,7 @@ function addMarkerLayer(){
map.fitBounds(latLngObjects, {reset: true});
}

function addMarker(link, latLon, freq, name, iconLabel){
function addMarker(link, latLon, name, iconLabel){
if(addedLocations.indexOf(latLon) == -1) {
var lat = latLon.split(",")[0];
var lon = latLon.split(",")[1];
Expand Down

0 comments on commit bd16061

Please sign in to comment.