Commit eeef838b authored by Maxym Borodenko's avatar Maxym Borodenko

Added ES query for the boundingbox instead of the DB

parent 1115a13a
......@@ -43,4 +43,6 @@ public interface ElasticService {
List<String> autocompleteSearch(String query) throws SearchException;
void regenerateAccessionSequentialNumber();
String getBoundingBox(AppliedFilters filters);
}
......@@ -41,6 +41,4 @@ public interface GenesysFilterService {
void listGeoTile(boolean distinct, AppliedFilters filters, Integer limit, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler);
void listGeoForBoundingBox(AppliedFilters filters, RowCallbackHandler rowHandler);
}
......@@ -68,15 +68,6 @@ public interface MappingService {
*/
byte[] getTile(AppliedFilters filters, int zoom, int xtile, int ytile);
/**
* Gets coordinates of bounding box
*
* @param filters
* the filters
* @return coordinates
*/
String getBoundingBox(AppliedFilters filters);
/**
* Coordinate utilities
*/
......
......@@ -26,6 +26,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
......@@ -38,6 +40,7 @@ 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;
......@@ -74,6 +77,8 @@ import org.springframework.stereotype.Service;
@Service
public class ElasticsearchSearchServiceImpl implements ElasticService, InitializingBean {
private final ObjectMapper mapper = new ObjectMapper();
private static final Log LOG = LogFactory.getLog(ElasticsearchSearchServiceImpl.class);
@Autowired
......@@ -147,6 +152,30 @@ 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();
}
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)
.build();
return elasticsearchTemplate.queryForList(searchQuery, AccessionDetails.class).get(0).getGeo();
}
@Override
public TermResult termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException {
......
......@@ -426,28 +426,4 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}, rowHandler);
}
@Override
public void listGeoForBoundingBox(final AppliedFilters filters, final RowCallbackHandler rowHandler) {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accessiongeo", "geo");
if (!filters.isEmpty()) {
// Join accession table when we have filters or looking at details
directQuery.innerJoin("accession", "a", "a.id=geo.accessionId");
}
if (!filters.isEmpty()) {
directQuery.join(filters);
directQuery.filter(filters, methodId -> methodRepository.findOne(methodId));
}
this.jdbcTemplate.query(connection -> {
final PreparedStatement stmt = connection.prepareStatement(
directQuery.getQuery("Max(geo.longitude), Min(geo.longitude), Max(geo.latitude), Min(geo.latitude)"));
// Set mysql JConnector to stream results
stmt.setFetchSize(Integer.MIN_VALUE);
new ArgumentPreparedStatementSetter(directQuery.getParameters()).setValues(stmt);
return stmt;
}, rowHandler);
}
}
......@@ -222,19 +222,4 @@ public class MappingServiceImpl implements MappingService {
throw new RuntimeException("Could not render image", e);
}
}
public String getBoundingBox(final AppliedFilters filters) {
final String[] result = {""};
filterService.listGeoForBoundingBox(filters, resultSet -> {
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("west", String.valueOf(resultSet.getDouble("Min(geo.latitude)")));
geoJson.put("south", String.valueOf(resultSet.getDouble("Min(geo.longitude)")));
geoJson.put("east", String.valueOf(resultSet.getDouble("Max(geo.latitude)")));
geoJson.put("north", String.valueOf(resultSet.getDouble("Max(geo.longitude)")));
result[0] = geoJson.toString();
});
return result[0];
}
}
......@@ -811,7 +811,7 @@ public class ExplorerController extends BaseController implements InitializingBe
public String getBoundingBox(@RequestParam(value = "filter", required = true) String jsonFilter) throws IOException {
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
return mappingService.getBoundingBox(appliedFilters);
return elasticService.getBoundingBox(appliedFilters);
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
......
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