Commit 13103a2f authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '628-geo-bounds-with-es' into 'main'

Resolve "Geo bounds with ES"

Closes #628

See merge request genesys-pgr/genesys-server!687
parents d8390510 0f4d1979
......@@ -269,6 +269,8 @@ public interface ElasticsearchService {
*/
<T extends EmptyModel> Page<T> findAll(Class<T> clazz, EmptyModelFilter<?, ?> filter, Predicate predicate, Pageable page, IEntityLoader<T> entityLoader, String... boostFields) throws SearchException;
Number[][] getAccessionGeoBounds(EmptyModelFilter<?, ?> filter) throws SearchException;
public static interface IEntityLoader<T> {
List<T> loadEntities(List<Long> entityIds);
}
......
......@@ -28,6 +28,7 @@ import java.util.UUID;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -635,7 +636,20 @@ public class AccessionServiceImpl implements AccessionService {
public Number[][] getGeoBounds(AccessionFilter filter) {
if (filter.isFulltextQuery()) {
return AccessionService.DEFAULT_GEOBOUNDS;
try {
Number[][] geoBounds = null;
if (elasticsearchService != null) {
geoBounds = elasticsearchService.getAccessionGeoBounds(filter);
}
if (geoBounds == null || geoBounds.length == 0) {
return AccessionService.DEFAULT_GEOBOUNDS;
} else {
return geoBounds;
}
} catch (SearchException e) {
LOG.error("Exception in getting geo bounds", e);
return AccessionService.DEFAULT_GEOBOUNDS;
}
}
final QAccession accession = QAccession.accession;
......@@ -750,7 +764,7 @@ public class AccessionServiceImpl implements AccessionService {
// Build sub-select for accession ids matching the filter
var accessionIdSelect = jpaQueryFactory.from(qAccession).select(qAccession.id);
filter.buildJpaQuery(accessionIdSelect, qAccession);
// fetch distinct list of tile indexes with subselect
var tileIndexes3d = jpaQueryFactory.from(qAid).select(qAid.tileIndex3min).distinct().where(qAid.id.in(accessionIdSelect).and(qAid.tileIndex3min.isNotNull())).fetch();
return new HashSet<>(tileIndexes3d);
......
......@@ -87,6 +87,8 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.UnmappedTerms;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.min.Min;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
......@@ -1207,6 +1209,35 @@ public class ElasticsearchServiceImpl implements ElasticsearchService, Initializ
return findAll(clazz, toEsQuery(clazz, filter, predicate), page, entityLoader);
}
@Override
public Number[][] getAccessionGeoBounds(EmptyModelFilter<?, ?> filter) throws SearchException {
String indexName = toIndexName(Accession.class) + INDEX_READ;
QueryBuilder queryBuilder = toEsQuery(Accession.class, filter);
SearchSourceBuilder sourceBuilder = makeSearchSourceBuilder(Accession.class, queryBuilder);
var maxLatitude = AggregationBuilders.max("max_latitude").field("latitude");
var minLatitude = AggregationBuilders.min("min_latitude").field("latitude");
var maxLongitude = AggregationBuilders.max("max_longitude").field("longitude");
var minLongitude = AggregationBuilders.min("min_longitude").field("longitude");
sourceBuilder.aggregation(maxLatitude);
sourceBuilder.aggregation(minLatitude);
sourceBuilder.aggregation(maxLongitude);
sourceBuilder.aggregation(minLongitude);
SearchRequest searchRequest = new SearchRequest(indexName).types(COMMON_TYPE_NAME).source(sourceBuilder);
SearchResponse response = search(searchRequest, RequestOptions.DEFAULT);
Max maxLatitudeResult = response.getAggregations().get("max_latitude");
Min minLatitudeResult = response.getAggregations().get("min_latitude");
Max maxLongitudeResult = response.getAggregations().get("max_longitude");
Min minLongitudeResult = response.getAggregations().get("min_longitude");
return new Number[][] { new Number[] { minLatitudeResult.getValue(), maxLongitudeResult.getValue() }, new Number[] { maxLatitudeResult.getValue(), minLongitudeResult.getValue() } };
}
@Override
public Map<String, List<? extends EmptyModel>> fullTextSearch(String text) throws SearchException {
final String TOP_HITS_AGG = "top_five_hits";
......
......@@ -358,6 +358,19 @@ public class ElasticQueryBuilderTest extends AbstractElasticServiceTest {
assertThat(search.size(), is(6));
}
@Test
public void testGetBounds() throws SearchException, JsonProcessingException {
AccessionFilter af = new AccessionFilter();
af.createdDate().le = new Date();
var bounds = elasticsearchService.getAccessionGeoBounds(af);
assertThat(bounds, is(notNullValue()));
assertThat(bounds[0][0], is(30.));
assertThat(bounds[0][1], is(15.));
assertThat(bounds[1][0], is(30.));
assertThat(bounds[1][1], is(15.));
}
@Test
public void testUUID() throws SearchException, JsonProcessingException {
......
Supports Markdown
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