Commit 32d62f60 authored by Matija Obreza's avatar Matija Obreza
Browse files

OpenSearch: Suggest accession names

parent 323bbc63
......@@ -17,6 +17,7 @@
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;
......@@ -43,4 +44,6 @@ public interface ElasticService {
TermResult termStatisticsAuto(AppliedFilters appliedFilters, String term, int size) throws SearchException;
List<String> autocompleteSearch(String query) throws SearchException;
}
......@@ -12,6 +12,7 @@ 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.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
......@@ -36,6 +37,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
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;
......@@ -80,8 +82,8 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Override
public Page<AccessionDetails> search(String query, Pageable pageable) throws SearchException {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(org.elasticsearch.index.query.QueryBuilders.queryString(query))
.withPageable(pageable).build();
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(org.elasticsearch.index.query.QueryBuilders.queryString(query).defaultOperator(Operator.AND)).withPageable(pageable).build();
try {
Page<AccessionDetails> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
......@@ -91,12 +93,30 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
}
}
@Override
public List<String> autocompleteSearch(String query) throws SearchException {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(org.elasticsearch.index.query.QueryBuilders.queryString("acceNumb:(" + query + "*)").defaultOperator(Operator.AND))
.withSort(SortBuilders.fieldSort(FilterConstants.ACCENUMB).order(SortOrder.ASC)).withPageable(new PageRequest(0, 10)).build();
try {
Page<AccessionDetails> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
List<String> sugg = new ArrayList<String>(10);
for (AccessionDetails ad : sampleEntities) {
sugg.add(ad.getAcceNumb() + ", " + ad.getTaxonomy().getGenus() + ", " + ad.getInstitute().getCode());
}
return sugg;
} catch (Throwable e) {
throw new SearchException(e.getMessage(), e);
}
}
@Override
public Page<AccessionDetails> filter(AppliedFilters appliedFilters, Pageable pageable) throws SearchException {
AndFilterBuilder filterBuilder = getFilterBuilder(appliedFilters);
SortBuilder sortBuilder = SortBuilders.fieldSort("acceNumb").order(SortOrder.ASC);
SortBuilder sortBuilder = SortBuilders.fieldSort(FilterConstants.ACCENUMB).order(SortOrder.ASC);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filterBuilder).withSort(sortBuilder).withPageable(pageable).build();
try {
......
......@@ -17,7 +17,10 @@
package org.genesys2.server.servlet.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -33,6 +36,7 @@ import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
// @Scope("request")
......@@ -73,6 +77,34 @@ public class SearchController {
return "/search/opensearch";
}
/**
* OpenSearch autocompleter
*
* @param searchQuery
* @return
* @throws IOException
*/
@RequestMapping(value = "/acn/opensearch-gossip", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
List<Object> openSearchAutocomplete(ModelMap model, @RequestParam(required = false, value = "q") String searchQuery) throws IOException {
List<Object> res = new ArrayList<Object>();
res.add(searchQuery);
if (!StringUtils.isBlank(searchQuery)) {
try {
res.add(searchService.autocompleteSearch(searchQuery));
} catch (SearchException e) {
res.add(ArrayUtils.EMPTY_STRING_ARRAY);
}
} else {
res.add(ArrayUtils.EMPTY_STRING_ARRAY);
}
res.add(ArrayUtils.EMPTY_STRING_ARRAY);
res.add(ArrayUtils.EMPTY_STRING_ARRAY);
return res;
}
@RequestMapping("/acn/search")
public String findAccession(ModelMap model, @RequestParam(required = false, value = "q") String searchQuery,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
......
......@@ -12,6 +12,7 @@
<Contact>helpdesk@genesys-pgr.org</Contact>
<Query role="example" searchTerms="IRGC 1001" />
<Url type="application/rss+xml" template="<c:out value="${baseUrl}" /><c:url value="/acn/opensearch" />?q={searchTerms}&amp;page={startPage?}"/>
<Url type="application/x-suggestions+json" template="<c:out value="${baseUrl}" /><c:url value="/acn/opensearch-gossip" />?q={searchTerms}"/>
<Url type="text/html" template="<c:out value="${baseUrl}" /><c:url value="/acn/search" />?q={searchTerms}&amp;page={startPage?}"/>
<Attribution>See <c:out value="${baseUrl}" /><c:url value="/content/terms" /></Attribution>
......
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