Skip to content

Commit

Permalink
Add terms aggregation for type field to JSON search responses
Browse files Browse the repository at this point in the history
See #8
  • Loading branch information
fsteeg committed Aug 15, 2017
1 parent 2aacd81 commit 30e2164
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

import org.apache.jena.atlas.web.HttpException;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
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 All @@ -44,6 +50,8 @@
*/
public class HomeController extends Controller {

private static final String TYPE = "type";

@Inject
Environment env;

Expand Down Expand Up @@ -125,8 +133,10 @@ public Result gnd(String id) {
}

public Result search(String q, int from, int size, String format) {
SearchResponse response = index.client().prepareSearch(config("index.name"))
.setQuery(QueryBuilders.queryStringQuery(q)).setFrom(from).setSize(size).get();
SearchRequestBuilder requestBuilder = index.client().prepareSearch(config("index.name"))
.setQuery(QueryBuilders.queryStringQuery(q)).setFrom(from).setSize(size);
requestBuilder.addAggregation(AggregationBuilders.terms(TYPE).field(TYPE + ".raw").size(1000));
SearchResponse response = requestBuilder.get();
response().setHeader("Access-Control-Allow-Origin", "*");
return format.equals("html") ? htmlSearch(q, from, size, format, response)
: ok(returnAsJson(response)).as(config("index.content"));
Expand Down Expand Up @@ -156,10 +166,13 @@ private static String returnAsJson(SearchResponse queryResponse) {
object.put("id", "http://" + request().host() + request().uri());
object.put("totalItems", queryResponse.getHits().getTotalHits());
object.set("member", Json.toJson(hits));
JsonNode aggregations = Json.parse(queryResponse.toString()).get("aggregations");
if (aggregations != null) {
object.set("aggregation", aggregations);
}
Aggregation aggregation = queryResponse.getAggregations().get(TYPE);
Terms terms = (Terms) aggregation;
Stream<Map<String, Object>> buckets = terms.getBuckets().stream().map((Bucket b) -> ImmutableMap.of(//
"key", b.getKeyAsString(), "doc_count", b.getDocCount()));
object.set("aggregation",
Json.toJson(ImmutableMap.of(TYPE, Json.toJson(buckets.collect(Collectors.toList())))));

return prettyJsonString(object);
}

Expand Down

0 comments on commit 30e2164

Please sign in to comment.