如何在弹簧数据弹性搜索中设置结果大小为零
考虑以下弹性搜索查询:
Consider the following elasticsearch query :
{
"query": {"match_all": {}},
"size": 0,
"aggs": {
"Terms": { "terms": { "field":"fileName" }
}
}
}
这里我只是对聚合感兴趣,而不是在文档中。这就是为什么我设置 size:0
,它的工作原理。但是,我无法使用 spring-data
实现相同的操作。代码示例:
Here I'm just interested in the aggregation and not in the documents. That's why I set size:0
and it works as expected. However I'm unable to achieve the same with spring-data
. Code sample :
PageRequest page = new PageRequest(0, 0);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexName).withTypes(typeName)
.withQuery(queryBuilder).withAggregation(aggsBuilder)
.withPageable(pageable).build();
这个 PageRequest
构造函数会抛出异常,这是从它的父母:
This PageRequest
constructor throws an exception then, which is from its parent:
public AbstractPageRequest(int page, int size) {
if (page < 0) {
throw new IllegalArgumentException("Page index must not be less than zero!");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must not be less than one!");
}
this.page = page;
this.size = size;
}
问题:Spring文件有没有办法将文档大小限制为零?
Question : Is there any way in spring data to limit document size to zero ?
如果您使用ES 1.x,您可以做的是省略
,并指定搜索类型 COUNT
。
If you're using ES 1.x, what you can do is leave out the Pageable
and specify the search type COUNT
instead.
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexName).withTypes(typeName)
.withQuery(queryBuilder).withAggregation(aggsBuilder)
.withSearchType(SearchType.COUNT).build();
截至ES 2.x, SearchType.COUNT
将被废弃,不再可用,但是对于ES 1.x需要这样做。
As of ES 2.x, SearchType.COUNT
will be deprecated and not available anymore, but for ES 1.x needs this should do the job.
请注意,有一个类似的需求,对于其他用户来说,但问题仍然是开放的。
Note that there's a similar need for other users, too, but the issue is still open.