Skip to content

Commit 014aa3d

Browse files
authored
Add reactive search batch size option to query.
Original Pull Request #2392 Closes #2061
1 parent 9446d72 commit 014aa3d

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
13131313
Duration scrollTimeout = query.getScrollTime() != null ? query.getScrollTime() : Duration.ofMinutes(1);
13141314
builder.scroll(time(scrollTimeout));
13151315
// limit the number of documents in a batch
1316-
builder.size(500);
1316+
builder.size(query.getReactiveBatchSize());
13171317
}
13181318

13191319
if (!isEmpty(query.getIndicesBoost())) {

Diff for: src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java

+18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
*/
4848
public class BaseQuery implements Query {
4949

50+
private static final int DEFAULT_REACTIVE_BATCH_SIZE = 500;
51+
5052
@Nullable protected Sort sort;
5153
protected Pageable pageable = DEFAULT_PAGE;
5254
protected List<String> fields = new ArrayList<>();
@@ -75,6 +77,7 @@ public class BaseQuery implements Query {
7577
@Nullable protected PointInTime pointInTime;
7678

7779
private boolean queryIsUpdatedByConverter = false;
80+
@Nullable private Integer reactiveBatchSize = null;
7881

7982
public BaseQuery() {}
8083

@@ -105,6 +108,7 @@ public <Q extends BaseQuery, B extends BaseQueryBuilder<Q, B>> BaseQuery(BaseQue
105108
this.requestCache = builder.getRequestCache();
106109
this.idsWithRouting = builder.getIdsWithRouting();
107110
this.pointInTime = builder.getPointInTime();
111+
this.reactiveBatchSize = builder.getReactiveBatchSize();
108112
}
109113

110114
@Override
@@ -471,6 +475,7 @@ public void setPointInTime(@Nullable PointInTime pointInTime) {
471475

472476
/**
473477
* used internally. Not considered part of the API.
478+
*
474479
* @since 5.0
475480
*/
476481
public boolean queryIsUpdatedByConverter() {
@@ -479,9 +484,22 @@ public boolean queryIsUpdatedByConverter() {
479484

480485
/**
481486
* used internally. Not considered part of the API.
487+
*
482488
* @since 5.0
483489
*/
484490
public void setQueryIsUpdatedByConverter(boolean queryIsUpdatedByConverter) {
485491
this.queryIsUpdatedByConverter = queryIsUpdatedByConverter;
486492
}
493+
494+
@Override
495+
public Integer getReactiveBatchSize() {
496+
return reactiveBatchSize != null ? reactiveBatchSize : DEFAULT_REACTIVE_BATCH_SIZE;
497+
}
498+
499+
/**
500+
* @since 5.1
501+
*/
502+
public void setReactiveBatchSize(Integer reactiveBatchSize) {
503+
this.reactiveBatchSize = reactiveBatchSize;
504+
}
487505
}

Diff for: src/main/java/org/springframework/data/elasticsearch/core/query/BaseQueryBuilder.java

+17
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
6666
protected final List<RuntimeField> runtimeFields = new ArrayList<>();
6767
@Nullable protected Query.PointInTime pointInTime;
6868

69+
@Nullable Integer reactiveBatchSize;
70+
6971
@Nullable
7072
public Sort getSort() {
7173
return sort;
@@ -191,6 +193,13 @@ public Query.PointInTime getPointInTime() {
191193
return pointInTime;
192194
}
193195

196+
/**
197+
* @since 5.1
198+
*/
199+
public Integer getReactiveBatchSize() {
200+
return reactiveBatchSize;
201+
}
202+
194203
public SELF withPageable(Pageable pageable) {
195204
this.pageable = pageable;
196205
return self();
@@ -375,6 +384,14 @@ public SELF withPointInTime(@Nullable Query.PointInTime pointInTime) {
375384
return self();
376385
}
377386

387+
/**
388+
* @since 5.1
389+
*/
390+
public SELF withReactiveBatchSize(@Nullable Integer reactiveBatchSize) {
391+
this.reactiveBatchSize = reactiveBatchSize;
392+
return self();
393+
}
394+
378395
public abstract Q build();
379396

380397
private SELF self() {

Diff for: src/main/java/org/springframework/data/elasticsearch/core/query/Query.java

+11
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ default PointInTime getPointInTime() {
448448
return null;
449449
};
450450

451+
/**
452+
* returns the number of documents that are requested when the reactive code does a batched search operation. This is
453+
* the case when a query has no limit and no Pageable set.
454+
*
455+
* @return the batch size, defaults to 500 in {@link BaseQuery}
456+
* @since 5.1
457+
*/
458+
default Integer getReactiveBatchSize() {
459+
return 500;
460+
}
461+
451462
/**
452463
* @since 4.3
453464
*/

0 commit comments

Comments
 (0)