Skip to content

Commit

Permalink
Use Aggregations object, don't parse full JSON response (see #207)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Feb 17, 2017
1 parent e8e31cb commit c452a34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
18 changes: 16 additions & 2 deletions web/app/controllers/resources/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Spliterators;
import java.util.UUID;
import java.util.function.Function;
Expand All @@ -23,6 +24,9 @@
import java.util.stream.StreamSupport;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -191,7 +195,17 @@ public static Promise<Result> aggregations(final String q, final String agent,
publisher, issued, medium, t, set);
Index queryResources =
index.queryResources(queryString, from, size, sort, owner);
return ok(queryResources.getAggregations());
Map<String, List<Map<String, Object>>> aggregations = new HashMap<>();
for (Entry<String, Aggregation> aggregation : queryResources
.getAggregations().asMap().entrySet()) {
Terms terms = (Terms) aggregation.getValue();
Stream<Map<String, Object>> buckets =
terms.getBuckets().stream().map((Bucket b) -> ImmutableMap.of(//
"key", b.getKeyAsString(), "doc_count", b.getDocCount()));
aggregations.put(aggregation.getKey(),
buckets.collect(Collectors.toList()));
}
return ok(Json.toJson(aggregations));
});
}

Expand Down Expand Up @@ -381,7 +395,7 @@ public static Promise<Result> facets(String q, String agent, String name,
JsonNode json = Json.parse(Helpers.contentAsString(result));
Stream<JsonNode> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
json.get(field).get("buckets").elements(), 0), false);
json.get(field).elements(), 0), false);
if (field.equals(ITEM_FIELD)) {
stream = preprocess(stream);
}
Expand Down
8 changes: 4 additions & 4 deletions web/app/controllers/resources/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

Expand Down Expand Up @@ -56,7 +57,7 @@ public class Index {

private JsonNode result;
private long total = 0;
private JsonNode aggregations;
private Aggregations aggregations;

/**
* Fields used when building query strings vis
Expand Down Expand Up @@ -155,8 +156,7 @@ public Index queryResources(String q, int from, int size, String sort,
SearchResponse response = requestBuilder.execute().actionGet();
SearchHits hits = response.getHits();
List<JsonNode> results = new ArrayList<>();
// TODO use Aggregation objects directly, don't parse into JSON
aggregations = Json.parse(response.toString()).get("aggregations");
aggregations = response.getAggregations();
for (SearchHit sh : hits.getHits()) {
results.add(Json.toJson(sh.getSource()));
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public JsonNode getResult() {
/**
* @return The aggregations for the query
*/
public JsonNode getAggregations() {
public Aggregations getAggregations() {
return aggregations;
}

Expand Down
30 changes: 17 additions & 13 deletions web/test/tests/ExternalIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;

import controllers.resources.Index;
import play.mvc.Http;
import play.test.Helpers;
Expand Down Expand Up @@ -45,17 +47,19 @@ public void testFacets() {
String queryString =
index.buildQueryString("köln", "", "", "", "", "", "", "", "");
Index queryResources = index.queryResources(queryString);
JsonNode facets = queryResources.getAggregations();
assertThat(facets.get("type").findValues("key").stream()
.map(e -> e.asText()).collect(Collectors.toList())).contains(
"bibliographicresource", "article", "book", "periodical",
"multivolumebook", "thesis", "miscellaneous", "proceedings",
"editedvolume", "biography", "festschrift", "newspaper",
"bibliography", "series", "officialpublication",
"referencesource", "publishedscore", "legislation", "image",
"game");
assertThat(facets.findValues("doc_count").stream().map(e -> e.intValue())
.collect(Collectors.toList())).excludes(0);
Aggregations facets = queryResources.getAggregations();
Terms terms = facets.get("type");
Stream<String> values =
terms.getBuckets().stream().map(Bucket::getKeyAsString);
Stream<Long> counts =
terms.getBuckets().stream().map(Bucket::getDocCount);
assertThat(values.collect(Collectors.toList())).contains(
"bibliographicresource", "article", "book", "periodical",
"multivolumebook", "thesis", "miscellaneous", "proceedings",
"editedvolume", "biography", "festschrift", "newspaper",
"bibliography", "series", "officialpublication", "referencesource",
"publishedscore", "legislation", "image", "game");
assertThat(counts.collect(Collectors.toList())).excludes(0);
});
}

Expand Down

0 comments on commit c452a34

Please sign in to comment.