Commit d9bbb314 authored by Alexander Prendetskiy's avatar Alexander Prendetskiy Committed by Matija Obreza

Fixed Genesys /search functionality

parent 105ddcfb
......@@ -22,6 +22,7 @@ import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.ClassPK;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.ActivityPost;
......@@ -35,6 +36,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
......@@ -49,7 +53,7 @@ public class SearchController {
public static final Logger LOG = LoggerFactory.getLogger(SearchController.class);
@Autowired
ElasticsearchService searchService;
ElasticsearchService elasticService;
@Autowired
private ContentService contentService;
......@@ -72,7 +76,7 @@ public class SearchController {
if (!StringUtils.isBlank(searchQuery)) {
try {
List<Accession> x = searchService.search(null, searchQuery, Accession.class);
List<Accession> x = elasticService.search(null, searchQuery, Accession.class);
model.addAttribute("pagedData", x);
LOG.info("Searching for: {} returns {}", searchQuery, x.size());
} catch (Throwable e) {
......@@ -158,26 +162,32 @@ public class SearchController {
try {
// FIXME these are note Page<?> but List<?>
if ("accession".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, searchQuery, Accession.class));
model.addAttribute("pagedData", convertToPage(elasticService.search(null, searchQuery, Accession.class), page));
} else if ("institute".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, searchQuery, FaoInstitute.class));
model.addAttribute("pagedData", convertToPage(elasticService.search(null, searchQuery, FaoInstitute.class), page));
} else if ("country".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, searchQuery, Country.class));
model.addAttribute("pagedData", convertToPage(elasticService.search(null, searchQuery, Country.class), page));
} else if ("article".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, searchQuery, Article.class));
model.addAttribute("pagedData", convertToPage(elasticService.search(null, searchQuery, Article.class), page));
} else if ("activitypost".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, searchQuery, ActivityPost.class));
model.addAttribute("pagedData", convertToPage(elasticService.search(null, searchQuery, ActivityPost.class), page));
} else {
LOG.error("Unsupported full-text searchfor section: {}", section);
throw new UnsupportedOperationException("No such search section");
}
} catch (Throwable e) {
LOG.info("Searching for: {} failed with error {}", searchQuery, e.getMessage());
LOG.error(e.getMessage());
LOG.error(e.getMessage(), e);
model.addAttribute("error", e);
}
}
return "/search/index2";
}
private <T extends BasicModel> Page<T> convertToPage(List<T> results, int page) {
int total = results == null ? 0 : results.size();
return new PageImpl<>(results, new PageRequest(page - 1, Math.max(20, total)), total);
}
}
......@@ -20,7 +20,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.elasticsearch.index.query.QueryBuilder;
import org.genesys.blocks.model.BasicModel;
......
......@@ -585,15 +585,14 @@ public class ElasticsearchServiceImpl implements ElasticsearchService, Initializ
SearchQuery query = new NativeSearchQueryBuilder()
/*@formatter:off*/
.withIndices(toIndexName(clazz))
.withIndices(indexName)
.withTypes(COMMON_TYPE_NAME)
.withFields("id", "_class")
.withSourceFilter(DEFAULT_SOURCE_FILTER)
// .withSourceFilter(new FetchSourceFilter(new String[] { "*"}, new String[] {}))
.withQuery(theQuery)
.withPageable(pageable)
.build();
/*@formatter:on*/
if (!indexedEntities.contains(clazz)) {
throw new RuntimeException("Class is not indexed: " + clazz);
}
......
......@@ -93,8 +93,8 @@
<td class="idx-col"><p><c:out value="${status.count + pagedData.size * pagedData.number}" /></p></td>
<td class="sel" x-aid="${unit.id}"></td>
<td><p><local:accession-details-link accession="${unit}" /></p></td>
<td><p><c:out value="${unit.taxonomy.sciName}" /></p></td>
<td class="notimportant"><p><c:out value="${jspHelper.getCountry(unit.orgCty.iso3).getName(pageContext.response.locale)}" /></p></td>
<td><p><c:out value="${unit.taxonomy.taxonName}" /></p></td>
<td class="notimportant"><p><c:out value="${jspHelper.getCountry(unit.countryOfOrigin.code3).getName(pageContext.response.locale)}" /></p></td>
<td class="notimportant"><p><spring:message code="accession.sampleStatus.${unit.sampStat}" /></p></td>
<td class="notimportant"><p><a href="<c:url value="/wiews/${unit.institute.code}" />"><c:out value="${unit.institute.code}" /></a></p></td>
</c:when>
......@@ -139,4 +139,4 @@
</c:otherwise>
</c:choose>
</body>
</html>
\ No newline at end of file
</html>
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