DATAES-988 Allow specifying max results in NativeSearchQueryBuilder

Original PR: #561 

Add a withMaxResults method to NativeSearchQueryBuilder for specifying the maxResults
on the built Query. This is common for aggregation queries where search hits are not
needed.

Having the builder method for setting maxResults is a minor ergonomic improvement, e.g.

NativeSearchQuery searchQuery = NativeSearchQueryBuilder()
        .withQuery(matchAllQuery())
        .addAggregation(terms("examples").field("example"))
        .withMaxResults(0)
        .build();

versus what was required before:

NativeSearchQuery searchQuery = NativeSearchQueryBuilder()
        .withQuery(matchAllQuery())
        .addAggregation(terms("examples").field("example"))
        .build();
searchQuery.setMaxResults(0);
This commit is contained in:
Patrick Strawderman 2020-11-30 09:54:30 -08:00 committed by GitHub
parent 7912ae9779
commit c66443ab5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -66,6 +66,7 @@ public class NativeSearchQueryBuilder {
@Nullable private SearchType searchType;
@Nullable private IndicesOptions indicesOptions;
@Nullable private String preference;
@Nullable private Integer maxResults;
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
@ -167,6 +168,11 @@ public class NativeSearchQueryBuilder {
return this;
}
public NativeSearchQueryBuilder withMaxResults(Integer maxResults) {
this.maxResults = maxResults;
return this;
}
public NativeSearchQuery build() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders,
@ -218,10 +224,15 @@ public class NativeSearchQueryBuilder {
if (indicesOptions != null) {
nativeSearchQuery.setIndicesOptions(indicesOptions);
}
if (preference != null) {
nativeSearchQuery.setPreference(preference);
}
if (maxResults != null) {
nativeSearchQuery.setMaxResults(maxResults);
}
return nativeSearchQuery;
}
}

View File

@ -118,6 +118,7 @@ public class ElasticsearchTemplateAggregationTests {
.withQuery(matchAllQuery()) //
.withSearchType(SearchType.DEFAULT) //
.addAggregation(terms("subjects").field("subject")) //
.withMaxResults(0) //
.build();
// when
SearchHits<ArticleEntity> searchHits = operations.search(searchQuery, ArticleEntity.class,
@ -127,6 +128,7 @@ public class ElasticsearchTemplateAggregationTests {
// then
assertThat(aggregations).isNotNull();
assertThat(aggregations.asMap().get("subjects")).isNotNull();
assertThat(searchHits.hasSearchHits()).isFalse();
}
/**