无法使用 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
我有 Article 实体,里面有一个嵌套的 属性,比方说 Metadata.
我需要计算所有文章,这些文章在这个嵌套 属性 中有一个特定的字段,假设 indexed,分配给例如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
的值放在自定义方法 之上:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"match": {
"metadata.indexed": 1
}
}
]
}
}
}
}
]
}
}
}
我的自定义 Spring 数据 ElasticSearch 存储库片段与自定义方法:
public CustomSpringDataElasticsearchRepository extends ElasticsearchRepository<Article, String> {
@Query("The query from above")
Long countByMetadata_Indexed(int value);
}
当我使用上面显示的存储库方法时,我得到 。
Custom Spring Data Elasticsearch Repository 方法(没有@Query)returns 0(没有下划线的版本returns 0)虽然它应该return一切正确。
如何使用 Spring Data ElasticSearch Repository 获得正确的结果?为什么没有 @Query 的自定义方法也不能正常工作?
UPD: spring-data-elasticsearch使用的版本是3.1.1.RELEASE.
目前版本库查询方法(3.2.4.RELEASE)不支持嵌套字段内的字段计数。
.
换句话说,目前,通过Spring Data ElasticSearch 进行此查询的唯一方法是使用ElasticsearchTemplate
bean 或ElasticsearchOperations
bean。
信用:P.J.Meisch
我有 Article 实体,里面有一个嵌套的 属性,比方说 Metadata.
我需要计算所有文章,这些文章在这个嵌套 属性 中有一个特定的字段,假设 indexed,分配给例如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
的值放在自定义方法 之上:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"match": {
"metadata.indexed": 1
}
}
]
}
}
}
}
]
}
}
}
我的自定义 Spring 数据 ElasticSearch 存储库片段与自定义方法:
public CustomSpringDataElasticsearchRepository extends ElasticsearchRepository<Article, String> {
@Query("The query from above")
Long countByMetadata_Indexed(int value);
}
当我使用上面显示的存储库方法时,我得到
Custom Spring Data Elasticsearch Repository 方法(没有@Query)returns 0(没有下划线的版本returns 0)虽然它应该return一切正确。
如何使用 Spring Data ElasticSearch Repository 获得正确的结果?为什么没有 @Query 的自定义方法也不能正常工作?
UPD: spring-data-elasticsearch使用的版本是3.1.1.RELEASE.
目前版本库查询方法(3.2.4.RELEASE)不支持嵌套字段内的字段计数。
换句话说,目前,通过Spring Data ElasticSearch 进行此查询的唯一方法是使用ElasticsearchTemplate
bean 或ElasticsearchOperations
bean。
信用:P.J.Meisch