Skip to content

Commit

Permalink
Added new aggregation for observed on
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Sep 26, 2024
1 parent 5b66f68 commit c43de57
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/strandls/esmodule/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private ApiConstants() {
public static final String GEOHASH_AGGREGATION = "/geohash-aggregation";
public static final String TERMS_AGGREGATION = "/terms-aggregation";
public static final String AGGREGATION = "/aggregation";
public static final String MONTH_AGGREGATION = "/month-aggregation";
public static final String BOUNDS = "/bounds";
public static final String SEARCH = "/search";
public static final String DOWNLOAD = "/download";
Expand Down
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 @@ -21,5 +21,6 @@ private Constants() {
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";
public static final String GROUP_BY_OBSERVED = "group_by_observed";

}
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 @@ -411,6 +411,28 @@ public Response getAggregationPerDay(@PathParam("filter") String filter){
}
}

@GET
@Path(ApiConstants.MONTH_AGGREGATION + "/{user}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for List Page", notes = "Returns Aggregated values", response = List.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 getAggregationPerMonth(@PathParam("user") String user){

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

try {
response=elasticSearchService.aggregationByMonth(user);
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 @@ -213,10 +213,17 @@ MapDocument termsAggregation(String index, String type, String field, String sub
/**
*
* @param filter the index in which to search
* @return {@link String}
* @return {@link Map}
*/
Map<String, List<Map<String, Object>>> aggregationByDay(String filter) throws IOException;

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

/**
*
* @param index the index in which to search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,40 @@ public Map<String, List<Map<String, Object>>> aggregationByDay(String filter) th
return groupbyday;
}

@Override
public List<Map<String, Object>> aggregationByMonth(String user) throws IOException {

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", user);
boolQuery.filter(authorFilter);
AggregationBuilder aggregation = null;
aggregation = AggregationBuilders.dateHistogram("agg").field("from_date").dateHistogramInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
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);
List<Map<String, Object>> groupbymonth= new ArrayList();
Histogram dateHistogram = response.getAggregations().get("agg");

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
Map<String, Object> data = new HashMap<>();
data.put("month", entry.getKeyAsString().substring(5,8));
data.put("year", entry.getKeyAsString().substring(0,4));
data.put("value", entry.getDocCount());
groupbymonth.add(data);
}

return groupbymonth;
}

@Override
public AggregationResponse aggregation(String index, String type, MapSearchQuery searchQuery,
String geoAggregationField, String filter, String geoShapeFilterField) throws IOException {
Expand Down Expand Up @@ -620,6 +654,8 @@ public AggregationResponse aggregation(String index, String type, MapSearchQuery
.subAggregation(AggregationBuilders.terms(nestedFilter).field(nestedFilter).size(1000));
} else if (filter.equals(Constants.GROUP_BY_DAY)) {
aggregation = AggregationBuilders.dateHistogram("agg").field("created_on").dateHistogramInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
} else if (filter.equals(Constants.GROUP_BY_OBSERVED)) {
aggregation = AggregationBuilders.dateHistogram("agg").field("from_date").dateHistogramInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
}
else {
aggregation = AggregationBuilders.terms(filter).field(filter).size(1000);
Expand Down Expand Up @@ -886,7 +922,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.GROUP_BY_DAY)) {
|| filter.equals(Constants.IDENTIFIER_ID) || filter.equals(Constants.GROUP_BY_DAY) || filter.equals(Constants.GROUP_BY_OBSERVED)) {
groupMonth = new LinkedHashMap<Object, Long>();
} else {
groupMonth = new HashMap<Object, Long>();
Expand Down Expand Up @@ -915,7 +951,13 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
groupMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
} else {
} else if (filter.equals(Constants.GROUP_BY_OBSERVED)) {
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);

for (Terms.Bucket entry : frommonth.getBuckets()) {
Expand Down

0 comments on commit c43de57

Please sign in to comment.