Skip to content

Commit

Permalink
Merge branch '207-owners' of https://github.com/hbz/lobid-resources
Browse files Browse the repository at this point in the history
Resolves #207

See owner facet count: http://lobid.org/resources/search?q=bonn
  • Loading branch information
fsteeg authored and sol committed Feb 14, 2017
2 parents 2fdd52a + 2d6b3ec commit aa19036
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 40 deletions.
46 changes: 12 additions & 34 deletions web/app/controllers/resources/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.apache.commons.lang3.tuple.Pair;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.bucket.children.InternalChildren;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;

Expand Down Expand Up @@ -71,8 +72,8 @@ public class Application extends Controller {
public static final String TYPE_FIELD = "type";
/** The internal ES field for the medium facet. */
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 aggregation name for the owner facet. */
public static final String OWNER_AGGREGATION = "owner";
/** The internal ES field for subjects. */
public static final String SUBJECT_FIELD = "subject.id";
/** The internal ES field for issued years. */
Expand Down Expand Up @@ -208,9 +209,14 @@ public static Promise<Result> aggregations(final String q, final String agent,
Index queryResources =
index.queryResources(queryString, from, size, sort, owner);
Map<String, List<Map<String, Object>>> aggregations = new HashMap<>();
Terms terms;
for (final Entry<String, Aggregation> aggregation : queryResources
.getAggregations().asMap().entrySet()) {
Terms terms = (Terms) aggregation.getValue();
Aggregation value = aggregation.getValue();
terms = (Terms) (value instanceof InternalChildren
? ((InternalChildren) value).getAggregations()
.get(OWNER_AGGREGATION)
: value);
Stream<Map<String, Object>> buckets =
terms.getBuckets().stream().map((Bucket b) -> ImmutableMap.of(//
"key", b.getKeyAsString(), "doc_count", b.getDocCount()));
Expand Down Expand Up @@ -380,7 +386,7 @@ public static Promise<Result> facets(String q, String agent, String name,
? medium : queryParam(medium, term);
String typeQuery = !field.equals(TYPE_FIELD) //
? t : queryParam(t, term);
String ownerQuery = !field.equals(ITEM_FIELD) //
String ownerQuery = !field.equals(OWNER_AGGREGATION) //
? owner : withoutAndOperator(queryParam(owner, term));
String subjectQuery = !field.equals(SUBJECT_FIELD) //
? subject : queryParam(subject, term);
Expand Down Expand Up @@ -411,13 +417,10 @@ public static Promise<Result> facets(String q, String agent, String name,
Stream<JsonNode> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
json.get(field).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,
agent, name, id, publisher, set, subject, issued, medium,
field.equals(ITEM_FIELD) ? "" : owner, t);
field.equals(OWNER_AGGREGATION) ? "" : owner, t);

@SuppressWarnings("unchecked")
List<Pair<JsonNode, String>> labelledFacets =
Expand All @@ -442,7 +445,7 @@ private static boolean current(String subject, String medium, String owner,
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(OWNER_AGGREGATION) && contains(owner, term)
|| field.equals(SUBJECT_FIELD) && contains(subject, term);
}

Expand All @@ -465,31 +468,6 @@ private static String withoutAndOperator(String currentParam) {
return currentParam.replace(",AND", "");
}

private static Stream<JsonNode> preprocess(Stream<JsonNode> stream) {
String captureItemUriWithoutSignature =
"(http://lobid\\.org/items/[^:]*?:[^:]*?:)[^\"]*";
List<String> itemUrisWithoutSignatures = stream
.map(json -> json.get("key").asText()
.replaceAll(captureItemUriWithoutSignature, "$1"))
.distinct().collect(Collectors.toList());
return count(itemUrisWithoutSignatures).entrySet().stream()
.map(entry -> Json.toJson(ImmutableMap.of(//
"key", Lobid.ORGS_BETA_ROOT + entry.getKey(), //
"doc_count", entry.getValue())));
}

private static Map<String, Integer> count(List<String> itemUris) {
String captureIsilOrgIdentifier =
"http://lobid\\.org/items/[^:]*?:([^:]*?):[^\"]*";
Map<String, Integer> map = new HashMap<>();
for (String term : itemUris) {
String isil = term.replaceAll(captureIsilOrgIdentifier, "$1");
if (!isil.trim().isEmpty())
map.put(isil, map.get(isil) == null ? 1 : map.get(isil) + 1);
}
return map;
}

/**
* @param id The resource ID
* @return True, if the resource with given ID is starred by the user
Expand Down
14 changes: 10 additions & 4 deletions web/app/controllers/resources/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.children.ChildrenBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

Expand Down Expand Up @@ -151,7 +152,7 @@ public Index queryResources(String q, int from, int size, String sort,
.order(sort.equals("newest") ? SortOrder.DESC : SortOrder.ASC));
}
requestBuilder = withAggregations(requestBuilder, "publication.startDate",
"subject.id", "type", "medium.id", "exemplar.id");
"subject.id", "type", "medium.id");
SearchResponse response = requestBuilder.execute().actionGet();
SearchHits hits = response.getHits();
List<JsonNode> results = new ArrayList<>();
Expand Down Expand Up @@ -234,12 +235,17 @@ public long getTotal() {

private static SearchRequestBuilder withAggregations(
final SearchRequestBuilder searchRequest, String... fields) {
int defaultSize = 100;
Arrays.asList(fields).forEach(field -> {
boolean many =
field.equals("publication.startDate") || field.equals("exemplar.id");
boolean many = field.equals("publication.startDate");
searchRequest.addAggregation(AggregationBuilders.terms(field).field(field)
.size(many ? 1000 : 100));
.size(many ? 1000 : defaultSize));
});
String field = Application.OWNER_AGGREGATION;
ChildrenBuilder ownerAggregation = AggregationBuilders.children(field)
.childType(TYPE_ITEM).subAggregation(AggregationBuilders.terms(field)
.field("owner.id").size(defaultSize));
searchRequest.addAggregation(ownerAggregation);
return searchRequest;
}

Expand Down
2 changes: 1 addition & 1 deletion web/app/controllers/resources/Lobid.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ else if (uris.size() == 1 && isGnd(uris.get(0)))
*/
public static String facetIcon(List<String> uris, String field) {
if ((uris.size() == 1 && isOrg(uris.get(0)))
|| field.equals(Application.ITEM_FIELD))
|| field.equals(Application.OWNER_AGGREGATION))
return "octicon octicon-home";
else if ((uris.size() == 1 && isGnd(uris.get(0)))
|| field.equals(Application.SUBJECT_FIELD))
Expand Down
2 changes: 1 addition & 1 deletion web/app/views/tags/facets.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ <h5>@facetToggle("issued-facet-all"){Erscheinungsjahr} @facetLabel(issued, issue
<div class="row facet"><div class="col-md-12">@facets("Schlagwörter", Application.SUBJECT_FIELD, if(!urisOnly(subject).isEmpty) urisOnly(subject) else "", "dropup")</div></div>
<div class="row facet"><div class="col-md-12">@facets("Medientypen", Application.MEDIUM_FIELD, medium, "dropup")</div></div>
<div class="row facet"><div class="col-md-12">@facets("Publikationstypen", Application.TYPE_FIELD, t, "dropup")</div></div>
<div class="row facet"><div class="col-md-12">@facets("Bestand in Bibliotheken", Application.ITEM_FIELD, owner, "dropup")</div></div>
<div class="row facet"><div class="col-md-12">@facets("Bestand in Bibliotheken", Application.OWNER_AGGREGATION, owner, "dropup")</div></div>
</div>

0 comments on commit aa19036

Please sign in to comment.