Commit 0ed5f159 authored by Maxym Borodenko's avatar Maxym Borodenko

getBoundingBox() method refactoring. Execute the ES query once instead of 4

parent 35d38d35
......@@ -31,16 +31,22 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.AndFilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.OrFilterBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.model.elastic.Geo;
import org.genesys2.server.model.filters.GenesysFilter;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
......@@ -154,26 +160,37 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Override
public String getBoundingBox(final AppliedFilters filters) {
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("west", String.valueOf(getGeo(filters, SortBuilders.fieldSort(FilterConstants.GEO_LATITUDE).order(SortOrder.ASC)).getLatitude()));
geoJson.put("south", String.valueOf(getGeo(filters, SortBuilders.fieldSort(FilterConstants.GEO_LONGITUDE).order(SortOrder.ASC)).getLongitude()));
geoJson.put("east", String.valueOf(getGeo(filters, SortBuilders.fieldSort(FilterConstants.GEO_LATITUDE).order(SortOrder.DESC)).getLatitude()));
geoJson.put("north",String.valueOf(getGeo(filters, SortBuilders.fieldSort(FilterConstants.GEO_LONGITUDE).order(SortOrder.DESC)).getLongitude()));
return geoJson.toString();
}
AndFilterBuilder filterBuilder = getFilterBuilder(filters);
final FilterAggregationBuilder builder = AggregationBuilders
.filter("agg").filter(filterBuilder)
.subAggregation(AggregationBuilders.min("min_longitude").field("geo.longitude"))
.subAggregation(AggregationBuilders.max("max_longitude").field("geo.longitude"))
.subAggregation(AggregationBuilders.min("min_latitude").field("geo.latitude"))
.subAggregation(AggregationBuilders.max("max_latitude").field("geo.latitude"));
private Geo getGeo(final AppliedFilters appliedFilters, final SortBuilder sortBuilder) {
AndFilterBuilder filterBuilder = getFilterBuilder(appliedFilters);
Pageable pageable = new PageRequest(0, 1);
SearchQuery searchQuery =
new NativeSearchQueryBuilder()
.withIndices(IndexAliasConstants.INDEXALIAS_PASSPORT_WRITE)
.withTypes(IndexAliasConstants.PASSPORT_TYPE)
.withSort(sortBuilder)
.withFilter(filterBuilder)
.withPageable(pageable)
.addAggregation(builder)
.build();
return elasticsearchTemplate.queryForList(searchQuery, AccessionDetails.class).get(0).getGeo();
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, SearchResponse::getAggregations);
Filter agg = aggregations.get("agg");
InternalMin minLatitude = agg.getAggregations().get("min_latitude");
InternalMin minLongitude = agg.getAggregations().get("min_longitude");
InternalMax maxLatitude = agg.getAggregations().get("max_latitude");
InternalMax maxLongitude = agg.getAggregations().get("max_longitude");
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("west", String.valueOf(minLatitude.getValue()));
geoJson.put("south", String.valueOf(minLongitude.getValue()));
geoJson.put("east", String.valueOf(maxLatitude.getValue()));
geoJson.put("north",String.valueOf(maxLongitude.getValue()));
return geoJson.toString();
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment