Skip to content

Commit

Permalink
Remove unused parameters: details, word, corporation, raw (see #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Jan 26, 2017
1 parent 50e0c93 commit f17b09b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 127 deletions.
139 changes: 44 additions & 95 deletions app/controllers/resources/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public class Application extends Controller {
public static final String MEDIUM_FIELD = "medium.id";
/** The internal ES field for the item/exemplar facet. */
public static final String ITEM_FIELD = "exemplar.id";
/** The internal ES field for the coverage facet. */
public static final String COVERAGE_FIELD =
"@graph.http://purl.org/dc/elements/1.1/coverage.@value.raw";
/** The internal ES field for subjects. */
public static final String SUBJECT_FIELD = "subject.id";
/** The internal ES field for issued years. */
Expand All @@ -87,7 +84,7 @@ public class Application extends Controller {
private static final String[] FIELDS =
new String[] { "contribution.agent.label", "title", "subject.id", "isbn",
"publication.publishedBy", "publication.startDate", "medium.id",
"type", "collectedBy.id", "_all", "contribution.agent.label" };
"type", "collectedBy.id" };

/**
* @return The index page.
Expand Down Expand Up @@ -133,20 +130,15 @@ public static String currentUri() {
* @param owner Owner filter for resource queries
* @param t Type filter for resource queries
* @param sort Sorting order for results ("newest", "oldest", "" -> relevance)
* @param showDetails If true, render details
* @param set The set
* @param word A word, a concept from the hbz union catalog
* @param corporation A corporation associated with the resource
* @param raw A query string that's directly (unprocessed) passed to ES
* @param format The response format ('html' or 'json')
* @return The search results
*/
public static Promise<Result> query(final String q, final String person,
final String name, final String subject, final String id,
final String publisher, final String issued, final String medium,
final int from, final int size, final String owner, String t, String sort,
boolean showDetails, String set, String word, String corporation,
String raw, String format) {
String set, String format) {
addCorsHeader();
String uuid = session("uuid");
if (uuid == null)
Expand All @@ -160,7 +152,7 @@ public static Promise<Result> query(final String q, final String person,
Logger.debug("Not cached: {}, will cache for one hour", cacheId);
Promise<Result> result = Promise.promise(() -> {
String queryString = buildQueryString(q, person, name, subject, id,
publisher, issued, medium, t, set, word, corporation);
publisher, issued, medium, t, set);
Index queryResources =
new Index().queryResources(queryString, from, size, sort);
JsonNode json = queryResources.getResult();
Expand All @@ -171,15 +163,14 @@ public static Promise<Result> query(final String q, final String person,
responseFormat.equals(Accept.Format.HTML.queryParamString);
return htmlRequested ? ok(query.render(s, q, person, name, subject, id,
publisher, issued, medium, from, size, queryResources.getTotal(),
owner, t, sort, set, word, corporation, raw)) : prettyJsonOk(json);
owner, t, sort, set)) : prettyJsonOk(json);
});
cacheOnRedeem(cacheId, result, ONE_HOUR);
return result.recover((Throwable throwable) -> {
Logger.error("Could not query index", throwable);
flashError();
return internalServerError(query.render("[]", q, person, name, subject,
id, publisher, issued, medium, from, size, 0L, owner, t, sort, set,
word, corporation, raw));
id, publisher, issued, medium, from, size, 0L, owner, t, sort, set));
});
}

Expand All @@ -188,11 +179,10 @@ public static Promise<Result> aggregations(final String q,
final String person, final String name, final String subject,
final String id, final String publisher, final String issued,
final String medium, final int from, final int size, final String owner,
String t, String sort, boolean details, String set, String word,
String corporation, String raw, String format) {
String t, String sort, String set, String format) {
return Promise.promise(() -> {
String queryString = buildQueryString(q, person, name, subject, id,
publisher, issued, medium, t, set, word, corporation);
publisher, issued, medium, t, set);
Index queryResources =
new Index().queryResources(queryString, from, size, sort);
return ok(queryResources.getAggregations());
Expand Down Expand Up @@ -294,20 +284,16 @@ private static void uncache(List<String> ids) {
* @param field The facet field (the field to facet over)
* @param sort Sorting order for results ("newest", "oldest", "" -> relevance)
* @param set The set
* @param word A word, a concept from the hbz union catalog
* @param corporation A corporation associated with the resource
* @param raw A query string that's directly (unprocessed) passed to ES
* @return The search results
*/
public static Promise<Result> facets(String q, String person, String name,
String subject, String id, String publisher, String issued, String medium,
int from, int size, String owner, String t, String field, String sort,
String set, String word, String corporation, String raw) {
String set) {

String key =
String.format("facets.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s",
field, q, person, name, id, publisher, set, word, corporation, raw,
subject, issued, medium, owner, t);
String key = String.format("facets.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s",
field, q, person, name, id, publisher, set, subject, issued, medium,
owner, t);
Result cachedResult = (Result) Cache.get(key);
if (cachedResult != null) {
return Promise.promise(() -> cachedResult);
Expand Down Expand Up @@ -336,8 +322,8 @@ public static Promise<Result> facets(String q, String person, String name,
Comparator<Pair<JsonNode, String>> sorter = (p1, p2) -> {
String t1 = p1.getLeft().get("key").asText();
String t2 = p2.getLeft().get("key").asText();
boolean t1Current = current(subject, medium, owner, t, field, t1, raw);
boolean t2Current = current(subject, medium, owner, t, field, t2, raw);
boolean t1Current = current(subject, medium, owner, t, field, t1);
boolean t2Current = current(subject, medium, owner, t, field, t2);
if (t1Current == t2Current) {
if (!field.equals(ISSUED_FIELD)) {
Integer c1 = p1.getLeft().get("doc_count").asInt();
Expand All @@ -361,19 +347,16 @@ public static Promise<Result> facets(String q, String person, String name,
? t : queryParam(t, term);
String ownerQuery = !field.equals(ITEM_FIELD) //
? owner : withoutAndOperator(queryParam(owner, term));
String rawQuery = !field.equals(COVERAGE_FIELD) //
? raw : rawQueryParam(raw, term);
String subjectQuery = !field.equals(SUBJECT_FIELD) //
? subject : queryParam(subject, term);
String issuedQuery = !field.equals(ISSUED_FIELD) //
? issued : queryParam(issued, term);

boolean current = current(subject, medium, owner, t, field, term, raw);
boolean current = current(subject, medium, owner, t, field, term);

String routeUrl = routes.Application.query(q, person, name, subjectQuery,
id, publisher, issuedQuery, mediumQuery, from, size, ownerQuery,
typeQuery, sort(sort, subjectQuery), false, set, word, corporation,
rawQuery, null).url();
typeQuery, sort(sort, subjectQuery), set, null).url();

String result = String.format(
"<li " + (current ? "class=\"active\"" : "")
Expand All @@ -386,33 +369,32 @@ typeQuery, sort(sort, subjectQuery), false, set, word, corporation,
return result;
};

Promise<Result> promise = aggregations(q, person, name, subject, id,
publisher, issued, medium, from, size, owner, t, sort, false, set, word,
corporation, raw, "json").map(result -> {
JsonNode json = Json.parse(Helpers.contentAsString(result));
Stream<JsonNode> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
json.get(field).get("buckets").elements(), 0), false);
if (field.equals(ITEM_FIELD)) {
stream = preprocess(stream);
}
String labelKey = String.format(
"facets-labels.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s",
field, raw, q, person, name, id, publisher, set, word,
corporation, subject, issued, medium, raw,
field.equals(ITEM_FIELD) ? "" : owner, t);

@SuppressWarnings("unchecked")
List<Pair<JsonNode, String>> labelledFacets =
(List<Pair<JsonNode, String>>) Cache.get(labelKey);
if (labelledFacets == null) {
labelledFacets = stream.map(toLabel).filter(labelled)
.collect(Collectors.toList());
Cache.set(labelKey, labelledFacets, ONE_DAY);
}
return labelledFacets.stream().sorted(sorter).map(toHtml)
.collect(Collectors.toList());
}).map(lis -> ok(String.join("\n", lis)));
Promise<Result> promise =
aggregations(q, person, name, subject, id, publisher, issued, medium,
from, size, owner, t, sort, set, "json").map(result -> {
JsonNode json = Json.parse(Helpers.contentAsString(result));
Stream<JsonNode> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
json.get(field).get("buckets").elements(), 0), false);
if (field.equals(ITEM_FIELD)) {
stream = preprocess(stream);
}
String labelKey = String.format(
"facets-labels.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, q,
person, name, id, publisher, set, subject, issued, medium,
field.equals(ITEM_FIELD) ? "" : owner, t);

@SuppressWarnings("unchecked")
List<Pair<JsonNode, String>> labelledFacets =
(List<Pair<JsonNode, String>>) Cache.get(labelKey);
if (labelledFacets == null) {
labelledFacets = stream.map(toLabel).filter(labelled)
.collect(Collectors.toList());
Cache.set(labelKey, labelledFacets, ONE_DAY);
}
return labelledFacets.stream().sorted(sorter).map(toHtml)
.collect(Collectors.toList());
}).map(lis -> ok(String.join("\n", lis)));
promise.onRedeem(r -> Cache.set(key, r, ONE_DAY));
return promise;
}
Expand All @@ -422,11 +404,10 @@ private static String sort(String sort, String subjectQuery) {
}

private static boolean current(String subject, String medium, String owner,
String t, String field, String term, String raw) {
String t, String field, String term) {
return field.equals(MEDIUM_FIELD) && contains(medium, term)
|| field.equals(TYPE_FIELD) && contains(t, term)
|| field.equals(ITEM_FIELD) && contains(owner, term)
|| field.equals(COVERAGE_FIELD) && rawContains(raw, quotedEscaped(term))
|| field.equals(SUBJECT_FIELD) && contains(subject, term);
}

Expand All @@ -449,26 +430,6 @@ private static String withoutAndOperator(String currentParam) {
return currentParam.replace(",AND", "");
}

/**
* @param currentParam The current value of the query param
* @param term The term to create a query for
* @return The escaped Elasticsearch query string for the `raw` query param
*/
public static String rawQueryParam(String currentParam, String term) {
String rawPrefix =
Lobid.escapeUri(COVERAGE_FIELD.replace(".raw", "")) + ":";
if (currentParam.isEmpty()) {
return rawPrefix + "(+" + quotedEscaped(term) + ")";
} else if (rawContains(currentParam, quotedEscaped(term))) {
String removedTerm = currentParam.replace(rawPrefix, "")
.replace("+" + quotedEscaped(term), "")
.replaceAll("\\A\\+|\\+\\z", "").replaceAll("\\++", "+");
return removedTerm.trim().equals("()") ? "" : rawPrefix + removedTerm;
} else
return currentParam.substring(0, currentParam.length() - 1) + "+"
+ quotedEscaped(term) + ")";
}

/**
* @param q The current query string
* @param values The values corresponding to {@link #Application.FIELDS}
Expand All @@ -482,27 +443,15 @@ public static String buildQueryString(String q, String... values) {
if (!fieldValue.isEmpty()) {
String complexQ = " AND (";
for (String v : fieldValue.replace(",AND", "").split(",")) {
complexQ += " +" + fieldName + ":"
+ (fieldName.endsWith(".id") ? quotedEscaped(v) : v);
complexQ += " +" + fieldName + ":" + (fieldName.endsWith(".id")
? "\"" + Lobid.escapeUri(v) + "\"" : v);
}
newQ += complexQ + ")";
}
}
return newQ;
}

private static String quotedEscaped(String term) {
return "\"" + Lobid.escapeUri(term) + "\"";
}

private static boolean rawContains(String raw, String term) {
String[] split = raw.split(":");
String terms = split[split.length - 1];
terms =
terms.length() >= 2 ? terms.substring(1, terms.length() - 1) : terms;
return Arrays.asList(terms.split("\\+")).contains(term);
}

private static Stream<JsonNode> preprocess(Stream<JsonNode> stream) {
String captureItemUriWithoutSignature =
"(http://lobid\\.org/items/[^:]*?:[^:]*?:)[^\"]*";
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/resources/Lobid.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2014 Fabian Steeg, hbz. Licensed under the GPLv2 */
/* Copyright 2014-2017 Fabian Steeg, hbz. Licensed under the GPLv2 */

package controllers.resources;

Expand Down Expand Up @@ -226,8 +226,6 @@ public static String facetIcon(List<String> uris, String field) {
if ((uris.size() == 1 && isOrg(uris.get(0)))
|| field.equals(Application.ITEM_FIELD))
return "octicon octicon-home";
else if ((uris.size() == 1 && field.equals(Application.COVERAGE_FIELD)))
return "octicon octicon-milestone";
else if ((uris.size() == 1 && isGnd(uris.get(0)))
|| field.equals(Application.SUBJECT_FIELD))
return "octicon octicon-tag";
Expand Down
5 changes: 1 addition & 4 deletions app/views/TableRow.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2014-2015 Fabian Steeg, hbz. Licensed under the GPLv2 */
/* Copyright 2014-2017 Fabian Steeg, hbz. Licensed under the GPLv2 */

package views;

Expand All @@ -14,7 +14,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.html.HtmlEscapers;

import controllers.resources.Application;
import controllers.resources.Lobid;
import play.Logger;

Expand Down Expand Up @@ -68,8 +67,6 @@ private String label(JsonNode doc, String property, String param,
String term = value;
if (param.equals("q")) {
term = "\"" + value + "\"";
} else if (param.equals("raw")) {
term = Application.rawQueryParam("", value);
}
try {
term = URLEncoder.encode(term, "UTF-8");
Expand Down
20 changes: 10 additions & 10 deletions app/views/query.scala.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@* Copyright 2014-2017 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(result: String, q:String, person:String, name:String, subject:String, id:String, publisher:String, issued:String, medium: String, from: Int, size: Int, allHits: Long, owner: String, t: String, sortParam: String, set: String, word: String, corporation: String, raw: String)
@(result: String, q:String, person:String, name:String, subject:String, id:String, publisher:String, issued:String, medium: String, from: Int, size: Int, allHits: Long, owner: String, t: String, sortParam: String, set: String)

@import helper._
@import tags._
Expand All @@ -14,31 +14,31 @@
<nav>
<ul class="pagination">
<li class="previous @if(from==0){disabled}">
<a href="@if(from==0){#} else {@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from-size,size,owner,t,sortParam,set=set,word=word,corporation=corporation,raw=raw)}">&larr;</a>
<a href="@if(from==0){#} else {@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from-size,size,owner,t,sortParam,set=set)}">&larr;</a>
</li>
@defining((((from+1)/size)+1,(if(allHits%size==0) allHits/size else allHits/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="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,(i*size)-size,size,owner,t,sortParam,set=set,word=word,corporation=corporation,raw=raw)">@i</a></li>
<li @if(currentPage==i){class="active"}><a href="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,(i*size)-size,size,owner,t,sortParam,set=set)">@i</a></li>
}
}
}
<li class="next @if(from+size >= allHits){disabled}">
<a href="@if(from+size >= allHits){#} else {@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from+size,size,owner,t,sortParam,set=set,word=word,corporation=corporation,raw=raw)}">&rarr;</a>
<a href="@if(from+size >= allHits){#} else {@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from+size,size,owner,t,sortParam,set=set)}">&rarr;</a>
</li>
</ul>
</nav>
}

@sort(param: String, label: String)={
<li @if(sortParam==param){class="active"}>
<a href="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from,size,owner,t,param,set=set,word=word,corporation=corporation,raw=raw)">@label</a>
<a href="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from,size,owner,t,param,set=set)">@label</a>
</li>
}

@pageLink(num: Int)={
<li @if(size==num){class="active"}>
<a href="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from,num,owner,t,sortParam,set=set,word=word,corporation=corporation,raw=raw)">@num</a>
<a href="@resources.routes.Application.query(q,person,name,subject,id,publisher,issued,medium,from,num,owner,t,sortParam,set=set)">@num</a>
</li>
}

Expand All @@ -51,8 +51,8 @@
}

@main("lobid-resources - Ergebnisliste") {
@if(Seq(person, name, subject, id, publisher, issued, word, corporation).exists(!_.isEmpty)){
@tags.search_advanced("Suche aktualisieren", q, person, name, subject, id, publisher, issued, sortParam, word=word, corporation=corporation)
@if(Seq(person, name, subject, id, publisher, issued).exists(!_.isEmpty)){
@tags.search_advanced("Suche aktualisieren", q, person, name, subject, id, publisher, issued, sortParam)
} else {
@tags.search_form(q)
}
Expand Down Expand Up @@ -84,7 +84,7 @@
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
@allHits @if(!q.isEmpty && !q.contains(Lobid.escapeUri(Application.COVERAGE_FIELD.replace(".raw","")))){
@allHits @if(!q.isEmpty && !labelRaw.isEmpty) {
@defining("http://lobid.org/resources/" + q.substring(q.lastIndexOf("/")+1, q.length()-1)) { lobidUrl =>
@labelRaw in: <a href="@lobidUrl">@Html(Lobid.resourceLabel(lobidUrl))</a>}}else{Treffer},
zeige @(from+1) bis @(Math.min(from+hits.size,from+size)):
Expand Down Expand Up @@ -127,7 +127,7 @@
}
}
</div>
@if(allHits > 0) {@facets(q,person,name,subject,id,publisher,issued,medium,owner,t,sortParam,set,word,corporation,raw,from,size)}
@if(allHits > 0) {@facets(q,person,name,subject,id,publisher,issued,medium,owner,t,sortParam,set,from,size)}
</div>
}
@if(allHits > 0) {<p>@map_credits()</p>}
Expand Down
Loading

0 comments on commit f17b09b

Please sign in to comment.