0

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.

cluis92
  • 664
  • 12
  • 35
  • 1
    have you checked what `searchWithAggs` returns? Does this contain the aggregations? – P.J.Meisch Jun 02 '22 at 15:20
  • Yes so that led me to an error. Should I post another question regarding it? Also, Are aggregations possible using the @Query annotation? – cluis92 Jun 02 '22 at 21:38
  • I ask because I noticed that is what you mentioned in this post https://stackoverflow.com/questions/63075178/in-spring-data-elasticsearch-can-aggregation-queries-be-put-in-a-respository-im – cluis92 Jun 03 '22 at 00:43
  • 1
    so it seems that Spring Data Elasticsearch returns the aggregations. You should debug where it gets lost, probably when returning the REST response? That's not a Spring Data Elasticsearch issue. Btw, `AggregatedPage` was deprecated in version 4.0 and removed in 4.1. The currently latest maintained version is 4.3, I'd recommend to update. – P.J.Meisch Jun 03 '22 at 17:41
  • OK I agree I am actually updating it right now; I believe the latest way to do this would be with 'SearchPage' instead of aggregatedpage. – cluis92 Jun 03 '22 at 18:31

0 Answers0