Instantly share code, notes, and snippets.

View ProfileService.java
public String deleteProfileDocument(String id) throws Exception {
DeleteRequest deleteRequest = new DeleteRequest(INDEX, TYPE, id);
DeleteResponse response =
client.delete(deleteRequest, RequestOptions.DEFAULT);
return response
.getResult()
.name();
View ProfileController.java
@DeleteMapping("/{id}")
public String deleteProfile(@PathVariable String id)
throws Exception {
return service.deleteProfileDocument(id);
}
View ProfileService.java
public List<ProfileDocument> searchByTechnology(String technology) throws Exception {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.boolQuery()
.must(QueryBuilders
.matchQuery("technologies.name", technology));
View ProfileController.java
@GetMapping(value = "/search")
public List<ProfileDocument> search(
@RequestParam(value = "technology") String technology)
throws Exception {
return service.searchByTechnology(technology);
}
View ProfileService.java
private List<ProfileDocument> getSearchResult(SearchResponse response) {
SearchHit[] searchHit = response.getHits().getHits();
List<ProfileDocument> profileDocuments = new ArrayList<>();
if (searchHit.length > 0) {
Arrays.stream(searchHit)
.forEach(hit -> profileDocuments
View ProfileService.java
public List<ProfileDocument> findAll() throws Exception {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse =
client.search(searchRequest, RequestOptions.DEFAULT);
View ProfileController.java
@GetMapping
public List<ProfileDocument> findAll() throws Exception {
return service.findAll();
}
View ProfileService.java
public String updateProfile(ProfileDocument document) throws Exception {
ProfileDocument resultDocument = findById(document.getId());
UpdateRequest updateRequest = new UpdateRequest(
INDEX,
TYPE,
resultDocument.getId());
Map<String, Object> documentMapper =
View ProfileController.java
@PutMapping
public ResponseEntity updateProfile(@RequestBody ProfileDocument document) throws Exception {
return new ResponseEntity(service.updateProfile(document), HttpStatus.CREATED);
}
View ProfileService.java
public ProfileDocument findById(String id) throws Exception {
GetRequest getRequest = new GetRequest(INDEX, TYPE, id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> resultMap = getResponse.getSource();
return objectMapper
.convertValue(resultMap, ProfileDocument.class);