Skip to content

Commit

Permalink
Set up advanced search for RPPD (RPB-38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Nov 23, 2023
1 parent 54862d5 commit 29b2b0e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ public Result gnd(String id) {
return ok(prettyJsonString(Json.parse(jsonLd))).as(config("index.content"));
}

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) {
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
if (responseFormat == null || Stream.of(RdfFormat.values()).map(RdfFormat::getParam)
Expand Down Expand Up @@ -360,6 +364,30 @@ 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 = "";
q += add(name, "preferredName", "variantName");
q += add(place, "placeOfBirth.label", "placeOfActivity.label", "placeOfDeath.label");
q += add(subject, "professionOrOccupation.label", "gndSubjectCategory.label");
q += add(publication, "publication");
q += add(date + "*", "dateOfBirth", "dateOfDeath");
return q;
}

private String add(String value, String... fields) {
if (!value.replace("*", "").isEmpty()) {
Stream<String> segments = Arrays.asList(fields).stream().map(s -> s + ":" + value);
return "+(" + segments.collect(Collectors.joining(" OR ")) + ") ";
}
return "";
}

private long allHits() {
return index.query("*").getHits().getTotalHits();
}
Expand Down
11 changes: 11 additions & 0 deletions app/views/advanced.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@* Copyright 2014 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(allHits: Long)

@import helper._
@import play.api.libs.json.Json
@import play.api.libs.json.JsValue

@main("", "RPPD - Erweiterte Suche", allHits) {
@search_advanced("Suchen", place="_", name="_", subject="_")
}
3 changes: 3 additions & 0 deletions app/views/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<li @if(title=="RPPD - Ergebnisliste"){class="active"}>
<a href="@routes.HomeController.search()">Suche</a>
</li>
<li @if(title=="RPPD - Erweiterte Suche"){class="active"}>
<a href="@routes.HomeController.advanced()">Erweiterte Suche</a>
</li>
@*
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Ergänzende Suche<b class="caret"></b></a>
Expand Down
95 changes: 95 additions & 0 deletions app/views/search_advanced.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@* Copyright 2014 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(label:String, q:String = "", place: String = "", name: String = "", subject: String = "", nwbibspatial: String = "", nwbibsubject: String = "", date: String = "", sortParam: String = "", publication: String = "")

@import scala.collection.immutable.TreeMap

<script>
function addSearchWidget(c){
var form = $("#nwbib-form");
var last = $(".search-field").last();
last.find(".rem").removeClass("disabled");
var add = last.clone().attr("id","search-field-" + c);
add.find(".rem").attr("onclick", "remSearchWidget("+(c)+")");
add.find("#nwbib-query-advanced").val("");
last.find("#and-search-field-"+(c-1)).show();
add.find("#and-search-field-"+(c-1)).attr("id", "and-search-field-"+c).hide();
add.find("#add-search-field-"+(c-1)).attr("id", "add-search-field-"+c)
last.find("#add-search-field-"+(c-1)).hide();
form.append(add);
$("#add-search-field-"+c).attr("onclick", "addSearchWidget("+(c+1)+")");
form.append($("#nwbib-form > #search-button"));
}

function remSearchWidget(c) {
$("#search-field-"+c).remove();
$("#search-button").show();
var rows = $(".search-field");
var last = rows.last();
last.find(".and").hide();
last.find(".add").show();
if(rows.size() == 1){
last.find(".rem").addClass("disabled");
}
}

function addParameters(){
var ps = {};
$(".search-field").each(function( index ) {
var param = $(this).find("#query-select").val();
var value = $(this).find("#nwbib-query-advanced").val();
ps[param] = (ps[param] ? ps[param] + " " : "") + value;
});
for (var key in ps) {
$("#nwbib-form").append("<input type='hidden' name='"+key+"' value='"+ps[key]+"'/>");
}
}
</script>

@helper.form(action = controllers.routes.HomeController.searchAdvanced(),
'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){
<div class="search-field row" id="search-field-@i">
<div class="form-group col col-md-3">
<select id="query-select" title="Suchtyp" class="form-control">
<option value="name" @if(k=="name"){selected="selected"}>Namen</option>
<option value="place" @if(k=="place"){selected="selected"}>Orte</option>
<option value="subject" @if(k=="subject"){selected="selected"}>Berufe, Fachgebiete</option>
<option value="publication" @if(k=="publication"){selected="selected"}>Werke</option>
<option value="date" @if(k=="date"){selected="selected"}>Lebensdaten, Jubiläen</option>
</select>
</div>
<div class="form-group col col-md-8">
<div class="input-group">
<input type="text" id="nwbib-query-advanced" title="Suchtext" class="form-control" placeholder='@if(v.startsWith("_")){@v.drop(1)}else{Suchanfrage}' value="@if(v.startsWith("_")){}else{@v}" onFocus="$('#search-button').show();"/>
<span class="input-group-btn">
<a href="#" title="Kriterium entfernen" class="btn btn-default rem @if(fields.isEmpty||fields.size==1){disabled}" onclick="remSearchWidget(@i)">
<span class="octicon octicon-x"></span>
</a>
</span>
</div>
</div>
<div class="form-group col col-md-1">
<a href="#" id="and-search-field-@i" class="btn btn-default add-and-button and disabled" title="UND">UND</a>
<a href="#" id="add-search-field-@i" class="btn btn-default add-and-button add" onclick="addSearchWidget(@(i+1))" title="Kriterium hinzufügen">
<span class="glyphicon glyphicon-plus-sign"></span></a>
</div>
<script>
$("#add-search-field-@(i-1)").hide();
</script>
</div>
}
<div class="row" id="search-button">
<div class="form-group col-md-2 col-md-offset-5">
<button type=submit class="btn btn-link" onclick="addParameters()" aria-label="@label">
<span class="glyphicon glyphicon-search"></span> @label</button>
</div>
</div>
<script>
$("#and-search-field-@(if(fields.isEmpty) 0 else fields.size-1)").hide();
$("#search-button").hide();
</script>
@if(!sortParam.isEmpty()){<input type="hidden" name="sort" value="@sortParam"/>}
}
}
2 changes: 2 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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 /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

0 comments on commit 29b2b0e

Please sign in to comment.