无法使用 Spring Data ElasticSearch Repository 计算嵌套属性内具有特定值字段的实体的出现次数 [英] Can't count the occurences of the entity with a field of particular value inside a nested property using Spring Data ElasticSearch Repository

查看:61
本文介绍了无法使用 Spring Data ElasticSearch Repository 计算嵌套属性内具有特定值字段的实体的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Article 实体,其中有一个嵌套的属性,比如 元数据.

I have the Article entity and inside it there is a nested property, let's say Metadata.

我需要计算所有文章,这些文章在此嵌套属性中具有特定字段,例如 indexed,分配给例如1.

I need to count all articles, which have a particular field inside this nested property, let's say indexed, assigned to e.g. 1.

Java 文档片段:

@Document(indexName = "article", type = "article", useServerConfiguration = true, createIndex = false)
@Setting(settingPath = "/mappings/settings.json")
@Mapping(mappingPath = "/mappings/articles.json")
public class Article {

// getters and setters, empty constructor are omitted for brevity
    @Id
    private String id;

    private Metadata metadata;

// remainder of the body is omitted

}

Metadata.class 片段

public class Metadata {

// getters and setters, empty constructor are omitted for brevity
    private Integer indexed;

// remainder of the body is omitted
}

我用来检索满足给定条件的文章的查询,并将其作为 @org.springframework.data.elasticsearch.annotations.Query 的值放在自定义方法:

The query I use to retrieve articles, which satisfy the given criteria and which I put as a value of @org.springframework.data.elasticsearch.annotations.Query on top of the custom method:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "metadata",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "metadata.indexed": 1
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

我使用自定义方法的自定义 Spring Data ElasticSearch 存储库片段:

public CustomSpringDataElasticsearchRepository extends ElasticsearchRepository<Article, String> {

    @Query("The query from above")
    Long countByMetadata_Indexed(int value);
}

当我使用上面显示的存储库方法时,我得到 java.lang.IllegalArgumentException:预期为 1,但发现 n 个结果.

When I use the repository method shown above , I get java.lang.IllegalArgumentException: Expected 1 but found n results.

自定义 Spring Data Elasticsearch Repository 方法(不带 @Query)返回 0(没有下划线的版本也返回 0),但它应该正确返回所有内容.

Custom Spring Data Elasticsearch Repository method(without @Query) returns 0(version without underscore returns 0 as well) though it should return everything correctly.

如何使用 Spring Data ElasticSearch Repository 获得正确的结果?为什么没有@Query 的自定义方法也不能正常工作?

How do I get the correct results using Spring Data ElasticSearch Repository? Why does the custom method without @Query doesn't work as well?

UPD:使用的spring-data-elasticsearch版本是3.1.1.RELEASE.

推荐答案

Repository 查询方法目前(3.2.4.RELEASE) 不支持按嵌套字段内的字段计数.

Repository query methods currently(3.2.4.RELEASE) don't support the count by the fields inside nested fields.

如前所述,从最新版本开始,@Query 注释不支持自定义计数查询(3.2.4.发布).

As was mentioned previously, @Query annotation doesn't support custom count queries as of the latest version(3.2.4.RELEASE).

换句话说,目前通过 Spring Data ElasticSearch 执行此查询的唯一方法是使用 ElasticsearchTemplate bean 或 ElasticsearchOperations bean.

In other words, currently, the only way to do this query through Spring Data ElasticSearch is to use ElasticsearchTemplate bean or ElasticsearchOperations bean.

信用:P.J.Meisch

这篇关于无法使用 Spring Data ElasticSearch Repository 计算嵌套属性内具有特定值字段的实体的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆