我有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 Data ElasticSearch存储库片段,带有自定义方法:
public CustomSpringDataElasticsearchRepository extends ElasticsearchRepository<Article, String> {
@Query("The query from above")
Long countByMetadata_Indexed(int value);
}
当我使用上面显示的存储库方法时,得到java.lang.IllegalArgumentException: Expected 1 but found n results
。
自定义Spring Data Elasticsearch存储库方法(不带@Query)返回0(不带下划线的版本也返回0),尽管它应该正确地返回所有内容。
如何使用Spring Data ElasticSearch存储库获得正确的结果?为什么没有@Query的自定义方法也不能正常工作?
UPD:使用的spring-data-elasticsearch的版本为3.1.1.RELEASE。
阅读评论和吉拉问题之后,在这里回答:java.lang.IllegalArgumentException: Expected 1 but found n results