Commit 6447f83d authored by Matija Obreza's avatar Matija Obreza

Term statistics with TermFacet

parent 28b20edb
......@@ -17,12 +17,14 @@
package org.genesys2.server.service;
import java.util.Collection;
import java.util.List;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.facet.result.Term;
public interface ElasticService {
......@@ -40,5 +42,6 @@ public interface ElasticService {
Page<AccessionDetails> filter(AppliedFilters appliedFilters, Pageable pageable) throws SearchException;
List<Term> termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException;
}
......@@ -41,6 +41,11 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.FacetedPage;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
import org.springframework.data.elasticsearch.core.facet.request.TermFacetRequestBuilder;
import org.springframework.data.elasticsearch.core.facet.result.Term;
import org.springframework.data.elasticsearch.core.facet.result.TermResult;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
......@@ -94,6 +99,39 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Override
public Page<AccessionDetails> filter(AppliedFilters appliedFilters, Pageable pageable) throws SearchException {
AndFilterBuilder filterBuilder = getFilterBuilder(appliedFilters);
SortBuilder sortBuilder = SortBuilders.fieldSort("acceNumb").order(SortOrder.ASC);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filterBuilder).withSort(sortBuilder).withPageable(pageable).build();
try {
Page<AccessionDetails> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
return sampleEntities;
} catch (Throwable e) {
throw new SearchException(e.getMessage(), e);
}
}
@Override
public List<Term> termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException {
AndFilterBuilder filterBuilder = getFilterBuilder(appliedFilters);
FacetRequest termFacetRequest = new TermFacetRequestBuilder("f").applyQueryFilter().fields(term).size(10).build();
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filterBuilder).withFacet(termFacetRequest).build();
try {
FacetedPage<AccessionDetails> page = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
TermResult facet = (TermResult) page.getFacet("f");
return facet.getTerms();
} catch (Throwable e) {
throw new SearchException(e.getMessage(), e);
}
}
private AndFilterBuilder getFilterBuilder(AppliedFilters appliedFilters) {
AndFilterBuilder filterBuilder = FilterBuilders.andFilter();
for (AppliedFilter appliedFilter : appliedFilters) {
......@@ -217,16 +255,7 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
filterBuilder.add(orFilter);
}
SortBuilder sortBuilder = SortBuilders.fieldSort("acceNumb").order(SortOrder.ASC);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filterBuilder).withSort(sortBuilder).withPageable(pageable).build();
try {
Page<AccessionDetails> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
return sampleEntities;
} catch (Throwable e) {
throw new SearchException(e.getMessage(), e);
}
return filterBuilder;
}
@Override
......
......@@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
......@@ -60,6 +61,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.facet.result.Term;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
......@@ -321,6 +323,19 @@ public class ElasticsearchTest {
}
}
@Test
public void testStorageStats() throws JsonProcessingException, SearchException {
AppliedFilters filters = new AppliedFilters();
filters.add(new AppliedFilter().setFilterName(FilterConstants.STORAGE).addFilterValue(new FilterHandler.LiteralValueFilter(30))
.addFilterValue(new FilterHandler.LiteralValueFilter(40)));
LOG.info(filters);
List<Term> terms = elasticService.termStatistics(filters, FilterConstants.STORAGE, 10);
for (Term t : terms) {
LOG.info(t.getTerm() + " = " + t.getCount());
}
}
@Test
public void testAvailable() throws JsonProcessingException, SearchException {
AppliedFilters filters = new AppliedFilters();
......
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