I am new to spring data elasticsearch. I want to be able to return data aggregations (simply, the count of values that are 'true' for a field) from elasticsearch to the front-end.
Here is my elasticsearch query.
GET member/_search
{
"query": {
"query_string": {
"query": "1301"
}
},
"aggs": {
"sdoh_aggs": {
"terms": {
"field": "populationStreams.adultsWithChronicConditionIndicator"
}
}
}
}
1301 is just an ID I am searching by, and my aggregation is on a field which is either 'true' or 'false'. I am using Java on the backend, and am using swagger API to test the API calls. I would like to first get the aggregation (or 'agg') using a swagger 'GET' call, as I think I can parse it on the front end from there.
Here is my java resource class.
* {@code SEARCH /_search/members?query=:query} : search for the member corresponding
* to the query.
*
* @param query the query of the member search.
* @param pageable the pagination information.
* @return the result of the search.
*/
@GetMapping("/_search/members")
public ResponseEntity<List<MemberDTO>> searchMembers(@RequestParam String query, Pageable pageable) {
log.debug("REST request to search for a page of Members for query {}", query);
//Here
AggregatedPage<MemberDTO> page = memberService.search(query, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
I am trying to return an aggregated page from a service class, as can be seen under the commented line 'Here'.
Here is my service class.
/**
* Search for the member corresponding to the query.
*
* @param query the query of the search.
* @param pageable the pagination information.
* @return the list of entities.
*/
@Transactional(readOnly = true)
public AggregatedPage<MemberDTO> search(String query, Pageable pageable) {
log.debug("Request to search for a page of Members for query {}", query);//searchWithAggs
// return memberSearchRepository.search(queryStringQuery(query), pageable)
// .map(memberMapper::toDto);
return memberSearchRepository.searchWithAggs((query), pageable);
}
As can be seen in the commented code, I was previously returning a Page of memberDTO's that was mapped to a Member object. Now, I want to return an aggregated page, as that is what I was thinking would contain my data aggregation, but this may be incorrect.
Finally, here is my member search repository, which contains my elasticsearch query (with the data aggregation).
/**
* Spring Data Elasticsearch repository for the {@link Member} entity.
*/
public interface MemberSearchRepository extends ElasticsearchRepository<Member, Long> {
@Query("{\"query_string\": {\"query\": \"?0\"}}, \"aggs\": {\"sdoh_aggs\": {\"terms\": {\"field\": \"populationStreams.adultsWithChronicConditionIndicator\"}}}")
AggregatedPage<MemberDTO> searchWithAggs(String query, Pageable pageable);
}
However, this doesn't work, as my swagger call simply returns the memberDTO object without the data aggregation.