Commit 108a265f authored by Jesper Zedlitz's avatar Jesper Zedlitz

Merge branch 'java' of gitlab.genealogy.net:project/des into java

parents 2853c1cf 2371a4d7
package net.genealogy.des.controller;
import com.google.common.collect.Lists;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import net.genealogy.ElasticsearchUtils;
import net.genealogy.des.model.AreaData;
......@@ -170,6 +169,77 @@ public class SearchController {
return "search/index";
}
private void addMatchQuery(BoolQueryBuilder query, HttpServletRequest request, String parameterName) {
if (StringUtils.isNotBlank(request.getParameter(parameterName))) {
query.must(QueryBuilders.matchQuery(parameterName, request.getParameter(parameterName)));
}
}
@GetMapping("/search/addressbook")
String index(HttpServletRequest request, Model model) {
String sort = Objects.toString(request.getParameter("sort"), "lastname");
if (sort.endsWith("Sort")) {
sort = sort.replaceAll("Sort", "");
}
String orderString = Objects.toString(request.getParameter("order"), "asc");
int max = Math.max(50, Math.min(500, NumberUtils.toInt(request.getParameter("max"))));
final BoolQueryBuilder query = new BoolQueryBuilder();
addMatchQuery(query, request, "lastname");
addMatchQuery(query, request, "firstname");
addMatchQuery(query, request, "value01");
addMatchQuery(query, request, "value03");
addMatchQuery(query, request, "value04");
if (!query.hasClauses()) {
return "search/addressbook";
}
final SearchRequestBuilder searchRequest = searchService.getClient().prepareSearch(SearchService.INDEX);
searchRequest.setTypes(EntrySearchService.TYPE);
searchRequest.setQuery(query);
// Check how many results exist for the query.
final CountResponse countResponse = searchService.getClient().prepareCount(SearchService.INDEX)
.setTypes(EntrySearchService.TYPE)
.setQuery(query)
.execute()
.actionGet(TIMEOUT);
if (countResponse.getCount() < THRESHOLD_FOR_SORT) {
SortOrder order = "desc".equals(orderString) ? SortOrder.DESC : SortOrder.ASC;
if ("lastname".equals(sort)) {
searchRequest.addSort(SortBuilders.fieldSort("lastname.untouched").order(order).missing("_last"));
searchRequest.addSort(SortBuilders.fieldSort("firstname.untouched").order(order).missing("_last"));
} else if ("firstname".equals(sort)) {
searchRequest.addSort(SortBuilders.fieldSort("firstname.untouched").order(order).missing("_last"));
searchRequest.addSort(SortBuilders.fieldSort("lastname.untouched").order(order).missing("_last"));
} else {
if ("dateCreated".equals(sort)) {
searchRequest.addSort(SortBuilders.fieldSort("${params.sort}").order(order).missing("_last"));
} else {
searchRequest.addSort(SortBuilders.fieldSort("${params.sort}.untouched").order(order).missing("_last"));
}
}
} else {
model.addAttribute("message", messageSource.getMessage("search.sort.disabled", new Object[]{countResponse.getCount()}, request.getLocale()));
}
searchRequest.setFrom(NumberUtils.toInt(request.getParameter("offset")));
searchRequest.setSize(max);
SearchResponse response = searchRequest.execute().actionGet(TIMEOUT);
model.addAttribute("result", response);
return "search/addressbook";
}
@GetMapping("/search/show/{id}")
String show(@PathVariable long id, Model model) {
final Optional<Entry> oEntry = entryRepository.findById(id);
......@@ -330,12 +400,12 @@ public class SearchController {
@PostMapping(value = "/search/metaAll", produces = "application/xml")
@ResponseBody
String metaAll( @RequestParam(required = false) String lastname,
@RequestParam(required = false) String placename,
@RequestParam(required = false) String placeid,
@RequestParam(required = false) String since) {
String metaAll(@RequestParam(required = false) String lastname,
@RequestParam(required = false) String placename,
@RequestParam(required = false) String placeid,
@RequestParam(required = false) String since) {
if ( StringUtils.isBlank(lastname) || StringUtils.isNotBlank(placename) || StringUtils.isNotBlank(placeid)) {
if (StringUtils.isBlank(lastname) || StringUtils.isNotBlank(placename) || StringUtils.isNotBlank(placeid)) {
// We can only perform the search iff a last name is given.
return "<result><database><name>Daten-Eingabe-System (DES)</name></url>https://des.genealogy.net/search</url></database></result>";
}
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
th:replace="~{/layout :: layout(title= #{search.title('Adressbücher')} , content=~{::section})}"
xmlns:des="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Search in WW1 casualty lists</title>
</head>
<body>
<section>
<form>
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td class="name">
<label for="lastname" th:text="#{entry.lastname}">Last name</label>
</td>
<td class="value">
<input type="text" id="lastname" name="lastname"/>
</td>
</tr>
<tr class="prop">
<td class="name">
<label for="firstname" th:text="#{entry.firstname}">Given name</label>
</td>
<td class="value">
<input type="text" id="firstname" name="firstname"/>
</td>
</tr>
<tr class="prop">
<td class="name">
<label for="value01" th:text="#{occupation}">Occupation</label>
</td>
<td class="value">
<input type="text" id="value01" name="value01"/>
</td>
</tr>
</tbody>
</table>
</div>
<div class="buttons">
<span class="button">
<input type="submit" name="search" th:value="#{search}" value="Search"/>
</span>
</div>
</form>
<div id="results" th:if="${result}">
<h2 th:text="#{search.results}">Searchresults</h2>
<div th:if="${result.hits.totalHits > 0}">
Es wurden <span th:text="${result.hits.totalHits}">5</span> Einträge gefunden.
<table class="table table-striped">
<thead>
<tr>
<th>&#160;</th>
<th property="lastname" th:text="#{entry.lastname}">Last name</th>
<th property="firstname" th:text="#{entry.firstname}">First name</th>
<th>Beruf</th>
<th>Ortsname</th>
<th>Ortskennung</th>
<th>Adresse</th>
<th th:text="#{entry.dateCreated.label}">Date created</th>
</tr>
</thead>
<tbody>
<tr th:each="entry : ${result.hits}">
<td>
<a th:href="@{/search/show/{id}(id=${entry.id})}" href="show.html"><img
th:src="@{/images/zoom.png}"
src="../static/images/zoom.png"
alt="show" width="16"
height="16"/></a>
<a sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{/admin/edit/{id}(id=${entry.id})}"
href="../admin/edit.html"><img
th:src="@{/images/page_white_edit.png}"
src="../static/images/page_white_edit.png"
alt="show"
width="16" height="16"/></a>
</td>
<td>
<a th:href="@{/search/show/{id}(id=${entry.id})}" href="show.html"
th:text="${entry.source['lastname']}">Mustermann</a>
</td>
<td th:text="${entry.source['firstname']}">Max</td>
<td th:text="${entry.source['value01']}">Tischler</td>
<td th:text="${entry.source['value03']}">Schönberg</td>
<td th:text="${entry.source['value04']}">SCHERJO54EJ</td>
<td th:text="${entry.source['value02']}">Friedhofsweg 3</td>
<td th:text="${entry.source['dateCreated'].substring(0, 10)}">2021-03-14</td>
</tr>
</tbody>
</table>
<div class="paginateButtons">
<th:paginate total="${result.hits.totalHits}" params="${params}"/>
</div>
</div>
<p th:if="${result.hits.totalHits == 0}" th:text="#{search.no_results}">No matching entries found.</p>
</div>
</section>
</body>
</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