Skip to content

Commit 8e15a1a

Browse files
authored
Rename TopDocsCollectorContext to TopDocsCollectorFactory (#95435)
This is to complete the removal of the query collector context abstraction implemented with #95383. The remaining TopDocsCollectorContext is more of a factory than a context object. This commit renames the class and all of its subclasses. It also adds javadocs to its methods to clarify the contract around them.
1 parent 02cbdb2 commit 8e15a1a

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

server/src/main/java/org/elasticsearch/search/query/QueryPhase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import static org.elasticsearch.search.profile.query.CollectorResult.REASON_SEARCH_MULTI;
5454
import static org.elasticsearch.search.profile.query.CollectorResult.REASON_SEARCH_POST_FILTER;
5555
import static org.elasticsearch.search.profile.query.CollectorResult.REASON_SEARCH_TERMINATE_AFTER_COUNT;
56-
import static org.elasticsearch.search.query.TopDocsCollectorContext.createTopDocsCollectorContext;
56+
import static org.elasticsearch.search.query.TopDocsCollectorFactory.createTopDocsCollectorFactory;
5757

5858
/**
5959
* Query phase of a search request, used to run the query and get back from each shard information about the matching documents
@@ -132,7 +132,7 @@ static void executeInternal(SearchContext searchContext) throws QueryPhaseExecut
132132
}
133133

134134
// create the top docs collector last when the other collectors are known
135-
final TopDocsCollectorContext topDocsFactory = createTopDocsCollectorContext(
135+
final TopDocsCollectorFactory topDocsFactory = createTopDocsCollectorFactory(
136136
searchContext,
137137
searchContext.parsedPostFilter() != null || searchContext.minimumScore() != null
138138
);

server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorContext.java renamed to server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorFactory.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,29 @@
6565

6666
/**
6767
* Creates and holds the main {@link Collector} that will be used to search and collect top hits.
68+
* Once the returned collector has been used to collect hits, allows to enrich the collected top docs to be set to the search context.
6869
*/
69-
abstract class TopDocsCollectorContext {
70+
abstract class TopDocsCollectorFactory {
7071
final String profilerName;
7172
final DocValueFormat[] sortValueFormats;
7273

73-
TopDocsCollectorContext(String profilerName, DocValueFormat[] sortValueFormats) {
74+
TopDocsCollectorFactory(String profilerName, DocValueFormat[] sortValueFormats) {
7475
this.profilerName = profilerName;
7576
this.sortValueFormats = sortValueFormats;
7677
}
7778

79+
/**
80+
* Returns the collector used to collect top hits, created depending on the incoming request options
81+
*/
7882
abstract Collector collector();
7983

84+
/**
85+
* Returns the collected top docs to be set to the {@link QuerySearchResult} within the search context.
86+
* To be called after collection, to enrich the top docs and wrap them with our {@link TopDocsAndMaxScore}.
87+
*/
8088
abstract TopDocsAndMaxScore topDocsAndMaxScore() throws IOException;
8189

82-
static class EmptyTopDocsCollectorContext extends TopDocsCollectorContext {
90+
static class EmptyTopDocsCollectorFactory extends TopDocsCollectorFactory {
8391
private final Sort sort;
8492
private final Collector collector;
8593
private final Supplier<TotalHits> hitCountSupplier;
@@ -89,7 +97,7 @@ static class EmptyTopDocsCollectorContext extends TopDocsCollectorContext {
8997
* @param sortAndFormats The sort clause if provided
9098
* @param trackTotalHitsUpTo The threshold up to which total hit count needs to be tracked
9199
*/
92-
private EmptyTopDocsCollectorContext(@Nullable SortAndFormats sortAndFormats, int trackTotalHitsUpTo) {
100+
private EmptyTopDocsCollectorFactory(@Nullable SortAndFormats sortAndFormats, int trackTotalHitsUpTo) {
93101
super(REASON_SEARCH_COUNT, null);
94102
this.sort = sortAndFormats == null ? null : sortAndFormats.sort;
95103
if (trackTotalHitsUpTo == SearchContext.TRACK_TOTAL_HITS_DISABLED) {
@@ -130,7 +138,7 @@ TopDocsAndMaxScore topDocsAndMaxScore() {
130138
}
131139
}
132140

133-
static class CollapsingTopDocsCollectorContext extends TopDocsCollectorContext {
141+
static class CollapsingTopDocsCollectorFactory extends TopDocsCollectorFactory {
134142
private final SinglePassGroupingCollector<?> topDocsCollector;
135143
private final Supplier<Float> maxScoreSupplier;
136144

@@ -141,7 +149,7 @@ static class CollapsingTopDocsCollectorContext extends TopDocsCollectorContext {
141149
* @param numHits The number of collapsed top hits to retrieve.
142150
* @param trackMaxScore True if max score should be tracked
143151
*/
144-
private CollapsingTopDocsCollectorContext(
152+
private CollapsingTopDocsCollectorFactory(
145153
CollapseContext collapseContext,
146154
@Nullable SortAndFormats sortAndFormats,
147155
int numHits,
@@ -175,7 +183,7 @@ TopDocsAndMaxScore topDocsAndMaxScore() throws IOException {
175183
}
176184
}
177185

178-
static class SimpleTopDocsCollectorContext extends TopDocsCollectorContext {
186+
static class SimpleTopDocsCollectorFactory extends TopDocsCollectorFactory {
179187

180188
private static TopDocsCollector<?> createCollector(
181189
@Nullable SortAndFormats sortAndFormats,
@@ -207,7 +215,7 @@ private static TopDocsCollector<?> createCollector(
207215
* @param trackTotalHitsUpTo Threshold up to which total hit count should be tracked
208216
* @param hasFilterCollector True if the collector chain contains at least one collector that can filter documents out
209217
*/
210-
private SimpleTopDocsCollectorContext(
218+
private SimpleTopDocsCollectorFactory(
211219
IndexReader reader,
212220
Query query,
213221
@Nullable SortAndFormats sortAndFormats,
@@ -289,11 +297,11 @@ TopDocsAndMaxScore topDocsAndMaxScore() {
289297
}
290298
}
291299

292-
static class ScrollingTopDocsCollectorContext extends SimpleTopDocsCollectorContext {
300+
static class ScrollingTopDocsCollectorFactory extends SimpleTopDocsCollectorFactory {
293301
private final ScrollContext scrollContext;
294302
private final int numberOfShards;
295303

296-
private ScrollingTopDocsCollectorContext(
304+
private ScrollingTopDocsCollectorFactory(
297305
IndexReader reader,
298306
Query query,
299307
ScrollContext scrollContext,
@@ -403,18 +411,18 @@ static int shortcutTotalHitCount(IndexReader reader, Query query) throws IOExcep
403411
}
404412

405413
/**
406-
* Creates a {@link TopDocsCollectorContext} from the provided <code>searchContext</code>.
414+
* Creates a {@link TopDocsCollectorFactory} from the provided <code>searchContext</code>.
407415
* @param hasFilterCollector True if the collector chain contains at least one collector that can filters document.
408416
*/
409-
static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searchContext, boolean hasFilterCollector)
417+
static TopDocsCollectorFactory createTopDocsCollectorFactory(SearchContext searchContext, boolean hasFilterCollector)
410418
throws IOException {
411419
final IndexReader reader = searchContext.searcher().getIndexReader();
412420
final Query query = searchContext.rewrittenQuery();
413421
// top collectors don't like a size of 0
414422
final int totalNumDocs = Math.max(1, reader.numDocs());
415423
if (searchContext.size() == 0) {
416424
// no matter what the value of from is
417-
return new EmptyTopDocsCollectorContext(searchContext.sort(), searchContext.trackTotalHitsUpTo());
425+
return new EmptyTopDocsCollectorFactory(searchContext.sort(), searchContext.trackTotalHitsUpTo());
418426
} else if (searchContext.scrollContext() != null) {
419427
// we can disable the tracking of total hits after the initial scroll query
420428
// since the total hits is preserved in the scroll context.
@@ -423,7 +431,7 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
423431
: SearchContext.TRACK_TOTAL_HITS_ACCURATE;
424432
// no matter what the value of from is
425433
int numDocs = Math.min(searchContext.size(), totalNumDocs);
426-
return new ScrollingTopDocsCollectorContext(
434+
return new ScrollingTopDocsCollectorFactory(
427435
reader,
428436
query,
429437
searchContext.scrollContext(),
@@ -435,9 +443,9 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
435443
hasFilterCollector
436444
);
437445
} else if (searchContext.collapse() != null) {
438-
boolean trackScores = searchContext.sort() == null ? true : searchContext.trackScores();
446+
boolean trackScores = searchContext.sort() == null || searchContext.trackScores();
439447
int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
440-
return new CollapsingTopDocsCollectorContext(
448+
return new CollapsingTopDocsCollectorFactory(
441449
searchContext.collapse(),
442450
searchContext.sort(),
443451
numDocs,
@@ -453,7 +461,7 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
453461
numDocs = Math.max(numDocs, rescoreContext.getWindowSize());
454462
}
455463
}
456-
return new SimpleTopDocsCollectorContext(
464+
return new SimpleTopDocsCollectorFactory(
457465
reader,
458466
query,
459467
searchContext.sort(),

server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
import java.util.List;
8484
import java.util.function.IntUnaryOperator;
8585

86-
import static org.elasticsearch.search.query.TopDocsCollectorContext.hasInfMaxScore;
86+
import static org.elasticsearch.search.query.TopDocsCollectorFactory.hasInfMaxScore;
8787
import static org.hamcrest.Matchers.anyOf;
8888
import static org.hamcrest.Matchers.arrayWithSize;
8989
import static org.hamcrest.Matchers.equalTo;
@@ -679,15 +679,15 @@ public void testDisableTopScoreCollection() throws Exception {
679679
context.parsedQuery(new ParsedQuery(q));
680680
context.setSize(3);
681681
context.trackTotalHitsUpTo(3);
682-
TopDocsCollectorContext topDocsContext = TopDocsCollectorContext.createTopDocsCollectorContext(context, false);
682+
TopDocsCollectorFactory topDocsContext = TopDocsCollectorFactory.createTopDocsCollectorFactory(context, false);
683683
assertEquals(topDocsContext.collector().scoreMode(), org.apache.lucene.search.ScoreMode.COMPLETE);
684684
QueryPhase.executeInternal(context);
685685
assertEquals(5, context.queryResult().topDocs().topDocs.totalHits.value);
686686
assertEquals(context.queryResult().topDocs().topDocs.totalHits.relation, TotalHits.Relation.EQUAL_TO);
687687
assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(3));
688688

689689
context.sort(new SortAndFormats(new Sort(new SortField("other", SortField.Type.INT)), new DocValueFormat[] { DocValueFormat.RAW }));
690-
topDocsContext = TopDocsCollectorContext.createTopDocsCollectorContext(context, false);
690+
topDocsContext = TopDocsCollectorFactory.createTopDocsCollectorFactory(context, false);
691691
assertEquals(topDocsContext.collector().scoreMode(), org.apache.lucene.search.ScoreMode.TOP_DOCS);
692692
QueryPhase.executeInternal(context);
693693
assertEquals(5, context.queryResult().topDocs().topDocs.totalHits.value);

server/src/test/java/org/elasticsearch/search/query/TopDocsCollectorContextTests.java renamed to server/src/test/java/org/elasticsearch/search/query/TopDocsCollectorFactoryTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import java.io.IOException;
2727

28-
public class TopDocsCollectorContextTests extends ESTestCase {
28+
public class TopDocsCollectorFactoryTests extends ESTestCase {
2929

3030
public void testShortcutTotalHitCountTextField() throws IOException {
3131
try (Directory dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir)) {
@@ -39,7 +39,7 @@ public void testShortcutTotalHitCountTextField() throws IOException {
3939
iw.commit();
4040
try (IndexReader reader = iw.getReader()) {
4141
final Query testQuery = new FieldExistsQuery("text");
42-
int hitCount = TopDocsCollectorContext.shortcutTotalHitCount(reader, testQuery);
42+
int hitCount = TopDocsCollectorFactory.shortcutTotalHitCount(reader, testQuery);
4343
assertEquals(-1, hitCount);
4444
}
4545
}
@@ -59,7 +59,7 @@ public void testShortcutTotalHitCountStringField() throws IOException {
5959
iw.commit();
6060
try (IndexReader reader = iw.getReader()) {
6161
final Query testQuery = new FieldExistsQuery("string");
62-
int hitCount = TopDocsCollectorContext.shortcutTotalHitCount(reader, testQuery);
62+
int hitCount = TopDocsCollectorFactory.shortcutTotalHitCount(reader, testQuery);
6363
assertEquals(2, hitCount);
6464
}
6565
}
@@ -75,7 +75,7 @@ public void testShortcutTotalHitCountNumericField() throws IOException {
7575
iw.commit();
7676
try (IndexReader reader = iw.getReader()) {
7777
final Query testQuery = new FieldExistsQuery("int");
78-
int hitCount = TopDocsCollectorContext.shortcutTotalHitCount(reader, testQuery);
78+
int hitCount = TopDocsCollectorFactory.shortcutTotalHitCount(reader, testQuery);
7979
assertEquals(1, hitCount);
8080
}
8181
}

0 commit comments

Comments
 (0)