Skip to content

Conversation

@javanna
Copy link
Member

@javanna javanna commented Apr 19, 2023

The QueryCollectorContext abstraction was introduced by #24864 based on the requirement that the top docs collector creation needed to be delayed until after all the other collectors had been created. At the same time, collectors get wrapped depending on the search features enabled by the request, but the top score / total hit count collector is the root collector where the wrapping starts, which is why its corresponding context gets added at position 0 in the list of collector contexts.

Requirements have changed since #27666 , which means that we can go back to a simpler way of creating collectors and wrapping them. We no longer need a QueryCollectorContext abstraction, and we can instead create collectors straight-away, and wrap them as needed. This is much easier to follow compared to the very generic create(Collector) method that the context exposes.

TopDocsCollectorContext adds some value in that it incorporates all the logic around creating the top docs collector, yet it can be further simplified as well by making the postProcess method more specific.

@javanna javanna changed the title Double check if creating the collectors first is still needed Remove QueryCollectorContext abstraction Apr 19, 2023
@javanna javanna requested a review from iverase April 20, 2023 07:56
// plug in additional collectors, like aggregations
collectors.add(createAggsCollectorContext(searchContext.getAggsCollector()));
List<Collector> subCollectors = new ArrayList<>();
subCollectors.add(collector);
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we can move this complexity in the wrapWithProfilerCollectorIfNeeded method. The ide awould be to use varargs in the method like:

 private static Collector wrapWithProfilerCollectorIfNeeded(
        Profilers profilers,
        Collector collector,
        String profilerName,
        Collector... children
    ) {
        if (profilers == null) {
            return collector;
        }
        final List<InternalProfileCollector> profileCollectors;
        if (children == null) {
            profileCollectors = List.of();
        } else {
            profileCollectors = new ArrayList<>(children.length);
            Arrays.stream(children).forEach(c -> profileCollectors.add((InternalProfileCollector) c));
        }
        return new InternalProfileCollector(
            collector,
            profilerName,
            profileCollectors
        );
    }

So here we can just call the method like:

// plug in additional collectors, like aggregations
                // in this case we pass both collectors as children profile collectors
                collector = wrapWithProfilerCollectorIfNeeded(
                    searchContext.getProfilers(),
                    MultiCollector.wrap(collector, searchContext.getAggsCollector()),
                    REASON_SEARCH_MULTI,
                    collector,
                    searchContext.getAggsCollector());

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried that, it looked a bit awkward. I will try again.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this will work, #95427 will also help in that direction so that InternalProfileCollector no longer requires a list.

Copy link
Member Author

Choose a reason for hiding this comment

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

The casting needed was a bit on the way and did not combine well with the varargs, but I found a way to reuse the wrap method for aggs too. It's not perfect but it's better than before I think.

@iverase
Copy link
Contributor

iverase commented Apr 20, 2023

I like the direction of this PR, it makes the code much easier to follow.

@javanna javanna added :Search/Search Search-related issues that do not fall into other categories >refactoring labels Apr 20, 2023
@javanna javanna marked this pull request as ready for review April 20, 2023 13:38
@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Apr 20, 2023
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@javanna javanna requested a review from iverase April 20, 2023 21:16
Copy link
Contributor

@iverase iverase left a comment

Choose a reason for hiding this comment

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

LGTM

@javanna javanna merged commit 3bd41b2 into elastic:main Apr 21, 2023
@javanna javanna deleted the poc/collectors_first branch April 21, 2023 07:04
javanna added a commit to javanna/elasticsearch that referenced this pull request Apr 21, 2023
This is to complete the removal of the query collector context
abstraction implemented with elastic#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.
javanna added a commit that referenced this pull request Apr 21, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>refactoring :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.8.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants