java - 使用 Spring Data ElasticsearchRepository 在 ElasticSearch 中查找唯一字段值

标签 java rest elasticsearch spring-data-elasticsearch

我有一个扩展 ElasticsearchRepository 的接口(interface),并已成功创建搜索方法,例如:

Page<AuditResult> findByCustomerCodeAndHost(String customerCode, String host, Pageable pageable);

现在,我希望命中一个端点,该端点将返回该 customerCode 的所有可能的主机值,以便我可以在前端构建一个下拉列表以选择要发送到该 findByCustomerCodeAndHost 端点的值,如下所示:

List<String> findUniqueHostByCustomerCode(String customerCode)

使用 ElasticsearchRepository 是否可以实现这一点?

我知道我可以使用 Distinct 关键字,例如 List<String> findDistinctByCustomerCode(String customerCode);但这不允许我指定主机字段。

<小时/>

编辑: 以下是我如何完成我想要的事情,但由于目前不可能使用 ElasticsearchRepository 来实际做到这一点,所以它不是一个真正的“答案”。

我创建了一个 Spring 网络 @RestController我暴露了一个类@GetMapping执行聚合查询的 REST 端点。

kibana控制台中的查询:

GET auditresult/_search
{
  "size": "0",
  "aggs" : {
    "uniq_custCode" : {
      "terms" : { "field" : "customerCode", "include": "<CUSTOMER_CODE>" },
        "aggs" : {
          "uniq_host" : {
           "terms" : { "field" : "host"}
          }
        }
      }
    }
  }

并且,基于这个问题ElasticSearch aggregation with Java我想出了

@GetMapping("/hosts/{customerCode}")
String getHostsByCustomer(@PathVariable String customerCode) {
    SearchRequest searchRequest = new SearchRequest("auditresult");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
    IncludeExclude ie = new IncludeExclude(customerCode, "");
    TermsAggregationBuilder aggregation =
            AggregationBuilders
                    .terms("uniq_custCode").includeExclude(ie)
                    .field("customerCode")
                    .subAggregation(
                            AggregationBuilders
                                    .terms("uniq_host")
                                    .field("host")
                    );
    searchSourceBuilder.aggregation(aggregation);
    searchRequest.source(searchSourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms cust = searchResponse.getAggregations().get("uniq_custCode");
        StringBuilder sb = new StringBuilder();
        sb.append("{\"hosts\":[");
        for (Terms.Bucket bucket : cust.getBuckets()) {
            Terms hosts = bucket.getAggregations().get("uniq_hosts");
            for (Terms.Bucket host : hosts.getBuckets()) {
                System.out.println(host.getKey());
                sb.append("\"" + host.getKey() + "\",");
            }
        }
        String out = sb.toString();
        out = out.substring(0, out.length() - 1);
        return out + "]}";
    } catch (IOException e) {
        e.printStackTrace();
        return "{\"hosts\":[]}";
    }
}

最佳答案

这里你需要的是 Spring Data 所谓的投影,对于 Spring Data MongoDB 你可以 read the documentation看看它是如何工作的。

唉,这在 Spring Data Elasticsearch 中还没有实现,我创建了 an issue in Jira为此。

关于java - 使用 Spring Data ElasticsearchRepository 在 ElasticSearch 中查找唯一字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61414148/

相关文章:

elasticsearch - ES 6.x-带时区的直方图脚本

java - Elasticsearch返回对象字段

java - Spring Security BCrypt 密码编码器 - 工作负载因素

java - 如何修复 java.util.NoSuchElementException 错误

java - 当我指定它们在 IDE 中的位置时,Java 项目中的引用库仅找到 native dll

php - REST 和 MySQL 偏移量选择

python - elasticsearch —获取正确的查询字符串

java - 升级到 SonarQube 5.4 后无法启动 SonarQube 服务器

database - 是否有轻量级、可嵌入的键/值数据库? (类似于 diet couchdb)

php - 为什么 Laravel API 在 POST 和 PUT 方法上返回 419 状态码?