如何使用Swagger API调用(Spring Data Elasticsearch)获取Elasticsearch数据聚合

发布于 2025-02-03 21:17:46 字数 2743 浏览 1 评论 0原文

我是Spring Data Elasticsearch的新手。我希望能够返回从Elasticsearch到前端的数据聚合(简单地,对字段的“真实”计数)。

这是我的Elasticsearch查询。

GET member/_search
{
  "query": {
    "query_string": {
      "query": "1301"
    }
  },
  "aggs": {
    "sdoh_aggs": {
      "terms": {
        "field": "populationStreams.adultsWithChronicConditionIndicator"
      }
    }
  }
}

1301只是我正在搜索的ID,我的聚合在一个“ true”或“ false”的字段上。我在后端使用Java,并且正在使用Swagger API测试API调用。我想首先使用Swagger“ Get”呼叫获得聚合(或“ Agg”),因为我认为我可以从那里从前端解析。

这是我的Java资源类。

     * {@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());
        }

我正在尝试从服务类返回一个汇总的页面,如“此处”评论的行中可以看出。

这是我的服务课。

    /**
     * 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);
    }

从注释的代码中可以看出,我以前返回了成员对象映射到成员对象的页面。现在,我想返回一个汇总的页面,因为那是我认为的包含我的数据聚合的内容,但这可能是不正确的。

最后,这是我的会员搜索存储库,其中包含我的elasticsearch查询(带有数据聚合)。

/**
 * 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);

}

但是,这不起作用,因为我的宣传呼叫只是在没有数据聚合的情况下返回成员dto对象。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文