Skip to content

Commit

Permalink
Added new update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Jan 3, 2025
1 parent a11a5b7 commit e760965
Show file tree
Hide file tree
Showing 4 changed files with 49 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 @@ -43,6 +43,7 @@ private ApiConstants() {
public static final String GETTOPUSERS = "/leaderboard";
public static final String GETUSERSCORE = "/userscore";
public static final String USERIBP = "/userIbp";
public static final String UPDATE = "/update";
public static final String REINDEX = "/reindex";
public static final String FILTERAUTOCOMPLETE = "/filterautocomplete";
public static final String LIST = "/list";
Expand Down
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 @@ -187,6 +187,28 @@ public MapQueryResponse update(@PathParam("index") String index, @PathParam("typ
}
}

@PUT
@Path(ApiConstants.UPDATE + "/{type}/{id}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for temporal distribution-month observed in user page", notes = "Return observed on data grouped by month into intervals of 50 years, filtered by userId", response = String.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Exception", response = String.class),
@ApiResponse(code = 500, message = "ERROR", response = String.class) })

public String updateEsField(@PathParam("type") String type, @PathParam("id") String id,
@ApiParam(name = "content") String content) {

String response = null;

try {
response = elasticSearchService.esUpdate(type, id, content);
return response;
} catch (Exception e) {
throw new WebApplicationException(
Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build());
}
}

@DELETE
@Path(ApiConstants.DATA + "/{index}/{type}/{documentId}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ public interface ElasticSearchService {
MapQueryResponse update(String index, String type, String documentId, Map<String, Object> document)
throws IOException;

/**
* Updates a document in es if it exists
*
* @param content
*
* @param index the index of the document
* @param type the type of the document
* @param documentId unique id of the document
* @param document the document in the form of key-value pairs to be updated
* @return {@link MapQueryResponse} containing the status of the operation
* @throws IOException throws {@link IOException}
*/
String esUpdate(String type, String id, String content) throws IOException;

/**
* Deletes a document in es if it exists
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ public MapQueryResponse update(String index, String type, String documentId, Map
return new MapQueryResponse(queryStatus, failureReason);
}

@Override
public String esUpdate(String type, String id, String content) throws IOException {

// Field: Create an UpdateRequest"
UpdateRequest updateRequest = new UpdateRequest(type, id).doc(content, XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
return updateResponse.toString();
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -1010,15 +1019,15 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
fromMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
Map<String, Object> afterKey = Map.of("path", "1");
while(afterKey!=null) {
while (afterKey != null) {
CompositeAggregationBuilder taxon_aggregation = AggregationBuilders
.composite("NAME", List.of(new TermsValuesSourceBuilder("path").field("path.keyword")))
.size(10000);
if (afterKey != null) {
taxon_aggregation.aggregateAfter(afterKey);
}
TermsAggregationBuilder subAggregation = AggregationBuilders.terms("raw_name").field("italicised_form.keyword")
.size(10);
TermsAggregationBuilder subAggregation = AggregationBuilders.terms("raw_name")
.field("italicised_form.keyword").size(10);
taxon_aggregation.subAggregation(subAggregation);
SearchSourceBuilder taxonsourceBuilder = new SearchSourceBuilder();
taxonsourceBuilder.aggregation(taxon_aggregation);
Expand Down

0 comments on commit e760965

Please sign in to comment.