You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by jo...@apache.org on 2026/09/25 08:13:15 UTC
(jackrabbit-oak) branch oak-search-mongot updated: OAK-search-mongot: fix cursor batch size and drain-on-getSize() bugs (#3143)
This is an automated email from the ASF dual-hosted git repository.
Joscorbe pushed a commit to branch oak-search-mongot
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/oak-search-mongot by this push:
new 62f4bd5409 OAK-search-mongot: fix cursor batch size and drain-on-getSize() bugs (#3143)
62f4bd5409 is described below
commit 62f4bd5409ed881512c68497b4c5c1ed0eac89ad
Author: Dominik Süß <do...@gmail.com>
AuthorDate: Fri Sep 25 10:13:08 2026 +0200
OAK-search-mongot: fix cursor batch size and drain-on-getSize() bugs (#3143)
* OAK: fix mongot aggregate cursor to use largest configured batch size
MongotIndex.aggregateCursor() always used fetchSizes[0] (default 10) as the
MongoDB driver's batchSize, regardless of how many other fetch-size tiers
were configured. Every driver batch is a full network round-trip to the
server, so a 2000-hit query needed ~200 round trips and took ~18-25s,
even for plans with no $search stage (this is a generic aggregation-cursor
cost, not specific to search queries).
Confirmed the root cause empirically against a live Atlas cluster: the
exact same query/collection took 24.9s at batchSize=10, 2.8s at
batchSize=100, and 0.76s at batchSize=1000 - only the batch size changed.
Fix: use the largest configured fetch size instead of the smallest, since
no adaptive/tiered logic actually exists to grow the batch size over a
cursor's lifetime. Documented in-code that a properly adaptive scheme
(sized to an actual row-limit hint) would need that hint threaded through
the backend-neutral IndexPlan/FulltextIndex SPI (oak-search), which is out
of scope for this module-local fix.
Co-authored-by: Copilot <22...@users.noreply.github.com>
* OAK: use a query-specific $count estimator instead of draining the cursor for Cursor.getSize()
streamingCursor() wired rows::getSize (MongotResultIterator.getSize()) as the
Cursor's SizeEstimator, which fully drains the entire result cursor - in the
same batched round trips fixed in the previous commit - the moment anything
calls Cursor.getSize()/JCR's RowIterator.getSize() (a common "N results
found" UI pattern), even if the caller never intends to iterate all rows.
Meanwhile MongotIndex.getSizeEstimator(IndexPlan) was already implemented
(cheap, index-level numDocs()) but never actually invoked anywhere in this
class - dead code - unlike ElasticIndex, which wires its equivalent,
query-filtered getDocCountFor() into its own non-facet query path.
numDocs() itself isn't a like-for-like replacement here: it's a whole-index
count, not filtered by this query's predicate, so reusing it as-is would
silently return the wrong (larger) count. Instead, add a query-specific
countMatches() helper that appends $count to the already-built pipeline -
a cheap server-side count with no document bodies returned - and wire that
into streamingCursor() in place of the drain-based estimator. Documented
that the existing getSizeEstimator(IndexPlan) override is now intentionally
unused (kept only to satisfy the FulltextIndex SPI contract).
Co-authored-by: Copilot <22...@users.noreply.github.com>
---------
Co-authored-by: Copilot <22...@users.noreply.github.com>
---
.../plugins/index/mongot/query/MongotIndex.java | 50 ++++++++++++++++++++--
1 file changed, 47 insertions(+), 3 deletions(-)
diff --git a/oak-search-mongot/src/main/java/org/apache/jackrabbit/oak/plugins/index/mongot/query/MongotIndex.java b/oak-search-mongot/src/main/java/org/apache/jackrabbit/oak/plugins/index/mongot/query/MongotIndex.java
index c14886b50c..8ad18dba1d 100644
--- a/oak-search-mongot/src/main/java/org/apache/jackrabbit/oak/plugins/index/mongot/query/MongotIndex.java
+++ b/oak-search-mongot/src/main/java/org/apache/jackrabbit/oak/plugins/index/mongot/query/MongotIndex.java
@@ -17,6 +17,7 @@
package org.apache.jackrabbit.oak.plugins.index.mongot.query;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
@@ -85,6 +86,10 @@ final class MongotIndex extends FulltextIndex {
@Override
protected SizeEstimator getSizeEstimator(IndexPlan plan) {
+ // Required by the FulltextIndex SPI contract, but not called anywhere in this
+ // class: it reports the whole index's document count rather than a
+ // query-specific count, so it wouldn't be accurate for Cursor.getSize().
+ // See streamingCursor()'s own $count-based estimator for the one actually used.
return () -> {
MongotIndexNode node = (MongotIndexNode) acquireIndexNode(plan);
try {
@@ -218,8 +223,26 @@ final class MongotIndex extends FulltextIndex {
MongotResultAdapter.excerpts(result, request.excerptColumns()),
null, null);
}, documents::close);
+ // Use a query-specific $count aggregation instead of rows::getSize: the latter
+ // would fully drain the streaming cursor (in aggregateCursor()'s batches) the
+ // moment anything calls Cursor.getSize()/JCR's RowIterator.getSize() - a common
+ // "N results found" UI pattern - even when the caller never intends to iterate
+ // all rows. $count is cheap (server-side count, no document bodies returned)
+ // and, unlike the unused getSizeEstimator(IndexPlan) override above, reflects
+ // this query's actual filter rather than the whole index's document count.
+ SizeEstimator sizeEstimator = () -> countMatches(collection, pipeline, definition);
return new FulltextPathCursor(rows, NEVER_REWOUND, plan,
- plan.getFilter().getQueryLimits(), rows::getSize);
+ plan.getFilter().getQueryLimits(), sizeEstimator);
+ }
+
+ private static long countMatches(MongoCollection<Document> collection,
+ List<Document> pipeline,
+ MongotIndexDefinition definition) {
+ List<Document> countPipeline = new ArrayList<>(pipeline);
+ countPipeline.add(new Document("$count", "n"));
+ try (MongoCursor<Document> cursor = aggregateCursor(collection, countPipeline, definition)) {
+ return cursor.hasNext() ? ((Number) cursor.next().get("n")).longValue() : 0L;
+ }
}
private static Cursor cursor(List<FulltextResultRow> rows, IndexPlan plan) {
@@ -268,9 +291,30 @@ final class MongotIndex extends FulltextIndex {
+ TimeUnit.MILLISECONDS.toNanos(SEARCH_READINESS_TIMEOUT_MILLIS);
while (true) {
try {
- int[] fetchSizes = definition.getQueryFetchSizes();
+ // Each driver batch is a full round-trip to the server, so the batch
+ // size should be as large as the configuration allows: using the
+ // smallest configured fetch size here previously turned a query with
+ // many hits into hundreds of round-trips and dominated query latency
+ // (e.g. ~18s for ~2000 hits at fetchSizes[0]=10, vs. <1s at 1000; the
+ // MongoDB driver holds one batch at a time, and the server caps a
+ // single batch response at 16MB regardless of the requested size, so
+ // this is safe rather than causing unbounded per-batch memory/time).
+ //
+ // NOTE / follow-up optimization opportunity (out of scope here): this
+ // always uses the largest configured size, even for queries that only
+ // need a handful of rows (e.g. a small LIMIT / typeahead query), which
+ // wastes server/network work building an oversized first batch. A
+ // properly adaptive scheme (small first batch, growing only if the
+ // caller keeps pulling) needs a row-limit hint to be threaded through
+ // from the query plan down to here - that hint doesn't currently exist
+ // in the backend-neutral IndexPlan/Filter/FulltextIndex SPI (oak-search)
+ // that this and the Elastic/Lucene backends all implement, so it isn't
+ // something this module can fix alone. Worth revisiting as a shared,
+ // implementation-neutral improvement to FulltextIndex/IndexPlan rather
+ // than a mongot-only special case.
+ int batchSize = Arrays.stream(definition.getQueryFetchSizes()).max().getAsInt();
AggregateIterable<Document> aggregation = collection.aggregate(pipeline)
- .batchSize(fetchSizes[0])
+ .batchSize(batchSize)
.maxTime(definition.getQueryTimeoutMillis(), TimeUnit.MILLISECONDS);
return aggregation.iterator();
} catch (MongoCommandException e) {