Skip to content

Commit

Permalink
Made empty collector with top_scores mode a public constant
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski committed Nov 18, 2024
1 parent 7f20f72 commit 990ab39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.opensearch.search.profile.query.CollectorResult.REASON_SEARCH_MIN_SCORE;
import static org.opensearch.search.profile.query.CollectorResult.REASON_SEARCH_MULTI;
Expand Down Expand Up @@ -112,29 +108,19 @@ public ReduceableSearchResult reduce(Collection<Collector> collectors) {
};
}

private static final Map<ScoreMode, QueryCollectorContext> CONTEXTS = Arrays.stream(ScoreMode.values())
.collect(
Collectors.toMap(
Function.identity(),
scoreMode -> createEmptyContext(formatContextName(scoreMode), createEmptyCollector(scoreMode))
)
);

private static String formatContextName(ScoreMode scoreMode) {
return String.format(Locale.ROOT, "empty_with_score_mode_%s", scoreMode.toString().toLowerCase(Locale.ROOT));
}

private static final Collector EMPTY_COLLECTOR = createEmptyCollector(ScoreMode.COMPLETE_NO_SCORES);
public static final QueryCollectorContext EMPTY_CONTEXT = CONTEXTS.get(ScoreMode.COMPLETE_NO_SCORES);

/**
* Returns the {@link QueryCollectorContext} for the provided {@link ScoreMode}
*
* @param scoreMode The score mode to get the context for
*/
public static QueryCollectorContext getContextForScoreMode(final ScoreMode scoreMode) {
return CONTEXTS.getOrDefault(scoreMode, EMPTY_CONTEXT);
}
public static final QueryCollectorContext EMPTY_CONTEXT = createEmptyContext(
formatContextName(ScoreMode.COMPLETE_NO_SCORES),
createEmptyCollector(ScoreMode.COMPLETE_NO_SCORES)
);
public static final QueryCollectorContext EMPTY_CONTEXT_TOP_SCORES_SCORE_MODE = createEmptyContext(
formatContextName(ScoreMode.TOP_SCORES),
createEmptyCollector(ScoreMode.TOP_SCORES)
);

private String profilerName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,28 +1197,8 @@ private void createTimeoutCheckerThenWaitThenRun(
verifyNoMoreInteractions(mockedSearchContext);
}

public void testQueryCollectorContextScoreModes() throws Exception {
// Test default empty context
QueryCollectorContext emptyContext = QueryCollectorContext.EMPTY_CONTEXT;
assertEquals(org.apache.lucene.search.ScoreMode.COMPLETE_NO_SCORES, emptyContext.create(null).scoreMode());

// Test getting context for different score modes
org.apache.lucene.search.ScoreMode[] scoreModes = org.apache.lucene.search.ScoreMode.values();
for (org.apache.lucene.search.ScoreMode scoreMode : scoreModes) {
QueryCollectorContext context = QueryCollectorContext.getContextForScoreMode(scoreMode);
assertEquals(scoreMode, context.create(null).scoreMode());
}

// Test that invalid score mode returns empty context
QueryCollectorContext defaultContext = QueryCollectorContext.getContextForScoreMode(null);
assertEquals(QueryCollectorContext.EMPTY_CONTEXT, defaultContext);
assertEquals(org.apache.lucene.search.ScoreMode.COMPLETE_NO_SCORES, defaultContext.create(null).scoreMode());
}

public void testQueryCollectorContextTopScores() throws Exception {
QueryCollectorContext topScoresContext = QueryCollectorContext.getContextForScoreMode(
org.apache.lucene.search.ScoreMode.TOP_SCORES
);
public void testEmptyQueryCollectorContextAndDifferentScoreModes() throws Exception {
QueryCollectorContext topScoresContext = QueryCollectorContext.EMPTY_CONTEXT_TOP_SCORES_SCORE_MODE;

// Verify score mode
assertEquals(org.apache.lucene.search.ScoreMode.TOP_SCORES, topScoresContext.create(null).scoreMode());
Expand Down

0 comments on commit 990ab39

Please sign in to comment.