Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to create empty query collector context with customizable score mode #16660

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix 'org.apache.hc.core5.http.ParseException: Invalid protocol version' under JDK 16+ ([#4827](https://github.com/opensearch-project/OpenSearch/pull/4827))
- Fix compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))
- Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response ([#9993](https://github.com/opensearch-project/OpenSearch/pull/9993))
- Add method to create empty query collector context with customizable score mode ([#16660](https://github.com/opensearch-project/OpenSearch/pull/16660))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@

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 All @@ -67,38 +72,69 @@
*/
@PublicApi(since = "1.0.0")
public abstract class QueryCollectorContext {
private static final Collector EMPTY_COLLECTOR = new SimpleCollector() {
@Override
public void collect(int doc) {}

@Override
public ScoreMode scoreMode() {
return ScoreMode.COMPLETE_NO_SCORES;
}
};
private static Collector createEmptyCollector(ScoreMode scoreMode) {
martin-gaievski marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please reference the relevant Apache Lucene issue as we've discussed, so it will be clear why we are doing score mode "dance", thanks!

return new SimpleCollector() {
@Override
public void collect(int doc) {}

public static final QueryCollectorContext EMPTY_CONTEXT = new QueryCollectorContext("empty") {
@Override
public ScoreMode scoreMode() {
return scoreMode;
}
};
}

@Override
Collector create(Collector in) throws IOException {
return EMPTY_COLLECTOR;
}
private static final ReduceableSearchResult EMPTY_RESULT = result -> {};

@Override
CollectorManager<?, ReduceableSearchResult> createManager(CollectorManager<?, ReduceableSearchResult> in) throws IOException {
return new CollectorManager<Collector, ReduceableSearchResult>() {
@Override
public Collector newCollector() throws IOException {
return EMPTY_COLLECTOR;
}
private static QueryCollectorContext createEmptyContext(String name, Collector collector) {
return new QueryCollectorContext(name) {
@Override
Collector create(Collector in) {
return collector;

Check warning on line 94 in server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java#L94

Added line #L94 was not covered by tests
}

@Override
public ReduceableSearchResult reduce(Collection<Collector> collectors) throws IOException {
return result -> {};
}
};
}
};
@Override
CollectorManager<?, ReduceableSearchResult> createManager(CollectorManager<?, ReduceableSearchResult> in) {
return new CollectorManager<>() {

Check warning on line 99 in server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java#L99

Added line #L99 was not covered by tests
@Override
public Collector newCollector() {
return collector;

Check warning on line 102 in server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java#L102

Added line #L102 was not covered by tests
}

@Override
public ReduceableSearchResult reduce(Collection<Collector> collectors) {
return EMPTY_RESULT;

Check warning on line 107 in server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java#L107

Added line #L107 was not covered by tests
}
};

}
};
}

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);

Check warning on line 136 in server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/QueryCollectorContext.java#L136

Added line #L136 was not covered by tests
}
martin-gaievski marked this conversation as resolved.
Show resolved Hide resolved

private String profilerName;

Expand Down
Loading