Skip to content

Commit

Permalink
Integrate advanced search fields in result list (RPB-38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Nov 23, 2023
1 parent 29b2b0e commit 2ffce85
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
54 changes: 33 additions & 21 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,34 @@ public Result index() {
public Result api() {
String format = "json";
ImmutableMap<String, String> searchSamples = ImmutableMap.<String, String>builder()
.put("Alles", controllers.routes.HomeController.search("*", "", "", 0, 10, format).toString())
.put("Alles", controllers.routes.HomeController
.search("*", "", "", "", "", "", "", "", 0, 10, format).toString())
.put("Alle Felder",
controllers.routes.HomeController.search("london", "", "", 0, 10, format).toString())
controllers.routes.HomeController
.search("london", "", "", "", "", "", "", "", 0, 10, format)
.toString())
.put("Feldsuche",
controllers.routes.HomeController.search("preferredName:Twain", "", "", 0, 10, format)
controllers.routes.HomeController.search("preferredName:Twain", "", "", "",
"", "", "", "", 0, 10, format)
.toString())
.put("Filter",
controllers.routes.HomeController
.search("preferredName:Twain", "type:Person", "", 0, 10, format).toString())
.search("preferredName:Twain", "", "", "", "", "", "type:Person",
"", 0, 10, format)
.toString())
.put("Paginierung",
controllers.routes.HomeController.search("london", "", "", 50, 100, format).toString())
controllers.routes.HomeController
.search("london", "", "", "", "", "", "", "", 50, 100, format)
.toString())
.put("Sortierung",
controllers.routes.HomeController
.search("scholl", "", "preferredName.keyword:asc", 0, 10, format).toString())
.search("scholl", "", "", "", "", "", "",
"preferredName.keyword:asc", 0, 10, format)
.toString())
.put("ASCII", controllers.routes.HomeController
.search("preferredName.ascii:Chor OR variantName.ascii:Chor", "", "", 0, 10, format).toString())
.search("preferredName.ascii:Chor OR variantName.ascii:Chor", "", "", "",
"", "", "", "", 0, 10, format)
.toString())
.build();
ImmutableMap<String, String> getSamples = ImmutableMap.of(//
"London", controllers.routes.HomeController.authorityDotFormat("4074335-4", "json").toString(), //
Expand Down Expand Up @@ -326,14 +338,16 @@ public Result advanced() {
return ok(views.html.advanced.render(allHits()));
}

public Result search(String q, String filter, String sort, int from, int size, String format) {
public Result search(String q, String name, String place, String subject, String publication,
String date, String filter, String sort, int from, int size, String format) {
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
if (responseFormat == null || Stream.of(RdfFormat.values()).map(RdfFormat::getParam)
.anyMatch(f -> f.equals(responseFormat.queryParamString))) {
return unsupportedMediaType(views.html.error.render(q,
String.format("Unsupported for search: format=%s, accept=%s", format, request().acceptedTypes())));
}
String queryString = (q == null || q.isEmpty()) ? "*" : q;
String queryString = buildQueryString(q, name, place, subject, publication, date);
queryString = (queryString == null || queryString.isEmpty()) ? "*" : queryString;
try {
SearchResponse response = index.query(queryString, filter, sort, from, size);
response().setHeader("Access-Control-Allow-Origin", "*");
Expand All @@ -346,7 +360,8 @@ public Result search(String q, String filter, String sort, int from, int size, S
}
switch (responseFormat) {
case HTML: {
return htmlSearch(q, filter, from, size, responseFormat.queryParamString, response);
return htmlSearch(q, name, place, subject, publication, date, filter, from, size,
responseFormat.queryParamString, response);
}
case JSON_LINES: {
response().setHeader("Content-Disposition",
Expand All @@ -364,14 +379,8 @@ public Result search(String q, String filter, String sort, int from, int size, S
}
}

public Result searchAdvanced(String name, String place, String subject, String publication, String date,
String filter, String sort, int from, int size, String format) {
String q = buildQueryString(name, place, subject, publication, date);
return search(q, filter, sort, from, size, format);
}

private String buildQueryString(String name, String place, String subject, String publication, String date) {
String q = "";
private String buildQueryString(String q, String name, String place, String subject,
String publication, String date) {
q += add(name, "preferredName", "variantName");
q += add(place, "placeOfBirth.label", "placeOfActivity.label", "placeOfDeath.label");
q += add(subject, "professionOrOccupation.label", "gndSubjectCategory.label");
Expand Down Expand Up @@ -426,9 +435,12 @@ public ByteString next() {
};
}

private Result htmlSearch(String q, String type, int from, int size, String format, SearchResponse response) {
return ok(views.html.search.render(q, type, from, size, returnAsJson(q, response),
response == null ? 0 : response.getHits().getTotalHits(), allHits()));
private Result htmlSearch(String q, String name, String place, String subject,
String publication, String date, String type, int from, int size, String format,
SearchResponse response) {
return ok(views.html.search.render(q, name, place, subject, publication, date, type, from,
size, returnAsJson(q, response),
response == null ? 0 : response.getHits().getTotalHits(), allHits()));
}

static Result withCallback(final String json) {
Expand Down
13 changes: 9 additions & 4 deletions app/models/AuthorityResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ private String addLiterals(String field, String result) {
String literalField = field + "AsLiteral";
for (Object literal : get(literalField)) {
String search = controllers.routes.HomeController
.search("", literalField + ":\"" + literal + "\"", "", 0, 10, "html").toString();
.search("", "", "", "", "", "", literalField + ":\"" + literal + "\"", "", 0,
10, "html")
.toString();
result = result + "&nbsp;" + "|" + "&nbsp;" + literal + "&nbsp;"
+ String.format(
"<a title='Weitere Einträge mit %s \"%s\" suchen' href='%s'>"
Expand All @@ -385,7 +387,9 @@ private List<String> typeLinks() {
.collect(Collectors.toList());
List<String> typeLinks = (subTypes.isEmpty() ? getType() : subTypes).stream()
.map(t -> String.format("<a href='%s'>%s</a>",
controllers.routes.HomeController.search("", "+(type:" + t + ")", "", 0, 10, "").toString(),
controllers.routes.HomeController
.search("", "", "", "", "", "", "+(type:" + t + ")", "", 0, 10, "")
.toString(),
models.GndOntology.label(t)))
.collect(Collectors.toList());
return typeLinks;
Expand Down Expand Up @@ -454,7 +458,7 @@ private String process(String field, String value, String label, int i, int size
boolean labelBasedFacet = facets.contains(field + ".label");
boolean qBasedSearch = facets.stream().noneMatch(s -> s.startsWith(field));
String search = controllers.routes.HomeController
.search(qBasedSearch ? field + ".id:\"" + value + "\"" : "",
.search(qBasedSearch ? field + ".id:\"" + value + "\"" : "", "", "", "", "", "",
labelBasedFacet ? field + ".label:\"" + label + "\""
: field + ".id:\"" + value + "\"", "", 0, 10, "html")
.toString();
Expand All @@ -470,7 +474,8 @@ private String process(String field, String value, String label, int i, int size
result = searchLink + "&nbsp;" + (linkableEntity ? entityLink : "");
} else if (field.endsWith("AsLiteral")) {
String search = controllers.routes.HomeController
.search("", field + ":\"" + value + "\"", "", 0, 10, "html").toString();
.search("", "", "", "", "", "", field + ":\"" + value + "\"", "", 0, 10, "html")
.toString();
result = result + "&nbsp;"
+ String.format(
"<a title='Weitere Einträge mit %s \"%s\" suchen' href='%s'>"
Expand Down
13 changes: 8 additions & 5 deletions app/views/search.scala.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@* Copyright 2015-2018 Fabian Steeg, hbz. Licensed under the EPL 2.0 *@

@(q: String, f: String, from: Int, size: Int, result: String, searchHits: Long, allHits: Long)
@(q: String, name: String = "", place: String = "", subject: String = "", publication: String = "", date: String = "", f: String, from: Int, size: Int, result: String, searchHits: Long, allHits: Long)

@import play.api.libs.json._
@import controllers.HomeController.CONFIG
Expand All @@ -10,25 +10,25 @@
<nav>
<ul class="pagination">
<li class="previous @if(from==0){disabled}">
<a href="@if(from==0){#} else {@routes.HomeController.search(q,f,"",from-size,size,"html")}">&larr;</a>
<a href="@if(from==0){#} else {@routes.HomeController.search(q,name,place,subject,publication,date,f,"",from-size,size,"html")}">&larr;</a>
</li>
@defining((((from+1)/size)+1,(if(searchHits%size==0) searchHits/size else searchHits/size+1).toInt)) { case (currentPage,lastPage) =>
@defining(Math.min(Math.max(1,currentPage-4)+9,lastPage)) { toPage =>
@for(i <- Math.max(1,toPage-9) to toPage){
<li @if(currentPage==i){class="active"}><a href="@routes.HomeController.search(q,f,"",(i*size)-size,size,"html")">@i</a></li>
<li @if(currentPage==i){class="active"}><a href="@routes.HomeController.search(q,name,place,subject,publication,date,f,"",(i*size)-size,size,"html")">@i</a></li>
}
}
}
<li class="next @if(from+size >= searchHits){disabled}">
<a href="@if(from+size >= allHits){#} else {@routes.HomeController.search(q,f,"",from+size,size,"html")}">&rarr;</a>
<a href="@if(from+size >= allHits){#} else {@routes.HomeController.search(q,name,place,subject,publication,date,f,"",from+size,size,"html")}">&rarr;</a>
</li>
</ul>
</nav>
}

@pageLink(num: Int)={
<li role="tab" @if(size==num){class="active" aria-selected="true"} else {aria-selected="false" tabindex="-1" aria-controls="@num"}>
<a href="@routes.HomeController.search(q,f,"",from,num,"html")">@num</a>
<a href="@routes.HomeController.search(q,name,place,subject,publication,date,f,"",from,num,"html")">@num</a>
</li>
}

Expand Down Expand Up @@ -171,6 +171,9 @@ <h5>@models.GndOntology.label("type")</h5>

@main(q, "RPPD - Ergebnisliste", allHits) {
<!-- <code><pre>@result</pre></code> -->
@if(Seq(name, place, subject, publication, date).exists(!_.isEmpty)){
@search_advanced("Suche aktualisieren", q=q, name=name, place=place, subject=subject, publication=publication, date=date)
}
@defining((Json.parse(result) \ "member").asOpt[Seq[JsValue]].getOrElse(Seq()).zipWithIndex) { hits =>
<div class="row" id="search-results">
<div class="col-md-@if(searchHits > 0){8}else{12}">
Expand Down
2 changes: 1 addition & 1 deletion app/views/search_advanced.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}
</script>

@helper.form(action = controllers.routes.HomeController.searchAdvanced(),
@helper.form(action = controllers.routes.HomeController.search(),
'id -> "nwbib-form", 'role -> "form", 'class -> "form-inline", 'autocomplete -> "off") {
@defining(TreeMap("name"->name,"place"->place,"publication"->publication,"subject"->subject,"date"->date).filter({case (k,v) => !v.isEmpty})) { fields =>
@for(((k,v),i)<- (if(fields.isEmpty) TreeMap("q" -> "") else fields).zipWithIndex){
Expand Down
3 changes: 1 addition & 2 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ GET /context.jsonld controllers.HomeController.context()
GET /dataset.jsonld controllers.HomeController.dataset(format="json")
GET /dataset controllers.HomeController.dataset(format?="")

GET /search controllers.HomeController.search(q ?= "", filter ?= "", sort ?= "", from: Int ?= 0, size: Int ?= 10, format ?= null)
GET /search controllers.HomeController.search(q ?= "", name ?= "", place ?= "", subject ?= "", publication ?= "", date ?= "", filter ?= "", sort ?= "", from: Int ?= 0, size: Int ?= 10, format ?= null)
GET /advanced controllers.HomeController.advanced()
GET /searchAdvanced controllers.HomeController.searchAdvanced(name ?= "", place ?= "", subject ?= "", publication ?= "", date ?= "", filter ?= "", sort ?= "", from: Int ?= 0, size: Int ?= 10, format ?= null)

GET /cgi-bin/wwwalleg/:name.pl controllers.HomeController.authorityPl(name, db ?= "rnam", index: Int ?= 1, zeilen: Int ?= 1, s1)

Expand Down
4 changes: 2 additions & 2 deletions public/stylesheets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ input#owner {

.search-field {
padding: 2px;
padding-right: 10px !important;
padding-left: 10px !important;
padding-right: 12px !important;
padding-left: 12px !important;
}

.pagination-and-facets {
Expand Down

0 comments on commit 2ffce85

Please sign in to comment.