Skip to content

Commit

Permalink
Added api endpoint for aggregation by day
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Sep 18, 2024
1 parent fdfd9fa commit 5b66f68
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/strandls/esmodule/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ private Constants() {
public static final String ACTIVITY_SCORE = "activity_score";
public static final String MVR_SCIENTIFIC_NAME = "max_voted_reco.scientific_name.keyword";
public static final String IDENTIFIER_ID = "all_reco_vote.authors_voted.id";
public static final String GROUP_BY_DAY = "group_by_day";

}
22 changes: 22 additions & 0 deletions src/main/java/com/strandls/esmodule/controllers/ESController.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,28 @@ public MapDocument termsAggregation(@PathParam("index") String index, @PathParam
}
}

@GET
@Path(ApiConstants.AGGREGATION + "/{filter}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for List Page", notes = "Returns Aggregated values", response = Map.class)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Location field not specified for bounds", response = String.class),
@ApiResponse(code = 500, message = "ERROR", response = String.class) })

public Response getAggregationPerDay(@PathParam("filter") String filter){

Map<String, List<Map<String, Object>>> response = null;

try {
response=elasticSearchService.aggregationByDay(filter);
return Response.status(Status.OK).entity(response).build();
} catch (Exception e) {
throw new WebApplicationException(
Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build());
}
}

@POST
@Path(ApiConstants.AGGREGATION + "/{index}/{type}/{filter}")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ MapResponse search(String index, String type, MapSearchQuery query, String geoAg
MapDocument termsAggregation(String index, String type, String field, String subField, Integer size,
String locationField, MapSearchQuery query) throws IOException;

/**
*
* @param filter the index in which to search
* @return {@link String}
*/
Map<String, List<Map<String, Object>>> aggregationByDay(String filter) throws IOException;

/**
*
* @param index the index in which to search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat.DateTime;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
Expand All @@ -64,6 +65,8 @@
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGrid;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.geogrid.ParsedGeoHashGrid;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.missing.Missing;
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
Expand Down Expand Up @@ -546,6 +549,48 @@ public MapResponse rangeSearch(String index, String type, List<MapRangeQuery> qu
return querySearch(index, boolQuery, searchParams, geoAggregationField, geoAggegationPrecision);
}

@Override
public Map<String, List<Map<String, Object>>> aggregationByDay(String filter) throws IOException {

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", filter);
boolQuery.filter(authorFilter);
AggregationBuilder aggregation = null;
aggregation = AggregationBuilders.dateHistogram("agg").field("created_on").dateHistogramInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
AggregationResponse aggregationResponse = new AggregationResponse();
if (aggregation == null)
return null;

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (boolQuery != null)
sourceBuilder.query(boolQuery);
sourceBuilder.aggregation(aggregation);

SearchRequest request = new SearchRequest("extended_observation");
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Map<String,List<Map<String, Object>>> groupbyday= new LinkedHashMap <String,List<Map<String, Object>>>();
Histogram dateHistogram = response.getAggregations().get("agg");

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
String year = entry.getKeyAsString().substring(0,4);
List<Map<String, Object>> yeardata;
if(groupbyday.containsKey(year)) {
yeardata = groupbyday.get(year);
} else {
yeardata = new ArrayList<>();
}

Map<String, Object> data = new HashMap<>();
data.put("date", entry.getKeyAsString());
data.put("value", entry.getDocCount());
yeardata.add(data);
groupbyday.put(year, yeardata);
}

return groupbyday;
}

@Override
public AggregationResponse aggregation(String index, String type, MapSearchQuery searchQuery,
String geoAggregationField, String filter, String geoShapeFilterField) throws IOException {
Expand Down Expand Up @@ -573,7 +618,10 @@ public AggregationResponse aggregation(String index, String type, MapSearchQuery
String nestedFilter = filter.replace("nested.", "");
aggregation = AggregationBuilders.nested(nestedFiled, nestedFiled)
.subAggregation(AggregationBuilders.terms(nestedFilter).field(nestedFilter).size(1000));
} else {
} else if (filter.equals(Constants.GROUP_BY_DAY)) {
aggregation = AggregationBuilders.dateHistogram("agg").field("created_on").dateHistogramInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
}
else {
aggregation = AggregationBuilders.terms(filter).field(filter).size(1000);
}

Expand Down Expand Up @@ -838,7 +886,7 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
Map<Object, Long> groupMonth;

if (filter.equals(Constants.MVR_SCIENTIFIC_NAME) || filter.equals(Constants.AUTHOR_ID)
|| filter.equals(Constants.IDENTIFIER_ID)) {
|| filter.equals(Constants.IDENTIFIER_ID) || filter.equals(Constants.GROUP_BY_DAY)) {
groupMonth = new LinkedHashMap<Object, Long>();
} else {
groupMonth = new HashMap<Object, Long>();
Expand All @@ -861,6 +909,12 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
for (Terms.Bucket entry : termResp.getBuckets()) {
groupMonth.put(entry.getKey(), entry.getDocCount());
}
} else if (filter.equals(Constants.GROUP_BY_DAY)) {
Histogram dateHistogram = response.getAggregations().get("agg");

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
groupMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
} else {
Terms frommonth = response.getAggregations().get(filter);

Expand Down

0 comments on commit 5b66f68

Please sign in to comment.