Commit 09c5de62 authored by Maxym Borodenko's avatar Maxym Borodenko

Use ES for text queries

parent 134b9d79
......@@ -25,6 +25,8 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
/**
* The Interface ElasticsearchService.
......@@ -199,6 +201,8 @@ public interface ElasticsearchService {
<T extends BasicModel> List<T> find(Class<T> clazz, BasicModelFilter<?, ?> filter);
<T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, String text, Pageable page);
/**
* Wrapper for search results
*/
......
......@@ -46,6 +46,9 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
private static final long serialVersionUID = -1441103961567816877L;
/** Any text. */
public String _text;
/** The historic. */
public Boolean historic;
......
......@@ -31,6 +31,7 @@ import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.service.DatasetService;
......@@ -368,6 +369,10 @@ public class AccessionServiceImpl implements AccessionService {
@Override
// @Cacheable(value = "apiResponses.accessionApi1.list", unless = "#result == null", keyGenerator = "shortFilterKeyGenerator")
public Page<Accession> list(AccessionFilter filter, Pageable page) {
if (StringUtils.isNotBlank(filter._text)) {
return elasticsearchService.findAll(Accession.class, filter, filter._text, page);
}
List<Accession> content = accessionRepository.findAll(filter, page);
long total = countAccessions(filter);
......
......@@ -54,6 +54,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.action.admin.indices.alias.delete.AliasesNotFoundException;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
......@@ -73,6 +74,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.UnmappedTerms;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.Publishable;
......@@ -87,8 +89,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.AliasQuery;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
......@@ -889,4 +894,43 @@ public class ElasticsearchServiceImpl implements ElasticsearchService, Initializ
return results;
}
@Override
public <T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, String text, Pageable page) {
String indexName = toIndexName(clazz) + INDEX_READ;
SearchRequestBuilder esQuery = client.prepareSearch(indexName).setTypes(COMMON_TYPE_NAME).setQuery(toFullTextEsQuery(filter, text))
.setFrom(page.getOffset()).setSize(page.getPageSize());
for (Sort.Order order : page.getSort()) {
esQuery.addSort(SortBuilders.fieldSort(order.getProperty()).order(SortOrder.valueOf(order.getDirection().name())));
}
SearchResponse searchResponse = esQuery.get();
List<T> content = new ArrayList<>();
searchResponse.getHits().forEach(hit -> {
LOG.trace("Mapping {} id={}", clazz, hit.getId());
T x = loadEntity(clazz, Long.parseLong(hit.getId()));
if (x != null) {
LOG.trace("Adding to results: {}", x);
content.add(x);
} else {
LOG.trace("Got null");
}
});
return new PageImpl<>(content, page, searchResponse.getHits().getTotalHits());
}
private QueryBuilder toFullTextEsQuery(BasicModelFilter<?, ?> filters, String text) {
ElasticQueryBuilder esQb = new ElasticQueryBuilder();
if (filters != null) {
filters.buildPredicate().accept(esQb, null);
}
// performing full text queries, including fuzzy matching and phrase or proximity queries
QueryBuilder esQuery = ((BoolQueryBuilder) esQb.getQuery()).filter(QueryBuilders.matchQuery("_all", text));
try {
LOG.trace("Converted {} to\ncurl -XGET 'localhost:9200/accession_read/_search?pretty' -d '{ \"query\": \n{} }'", new ObjectMapper().writeValueAsString(filters),
esQuery);
} catch (JsonProcessingException ignored) {}
return esQuery;
}
}
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