Skip to content

Use inheritance instead of composition to simplify search phase transitions #119272

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

Merged
merged 11 commits into from
Feb 24, 2025

Conversation

original-brownbear
Copy link
Contributor

We only need the extensibility for testing and it's a lot easier to reason about the code if we have explicit methods instead of overly complicated composition with lots of redundant references being retained all over the place.

-> lets simplify to inheritance and get shorter code that performs more predictably (especially when it comes to memory) as a first step. This also opens up the possibility of further simplifications and removing more retained state/memory as we go through the search phases.

…itions

We only need the extensibility for testing and it's a lot easier to
reason about the code if we have explicit methods instead of overly
complicated composition with lots of redundant references being retained
all over the place.

-> lets simplify to inheritance and get shorter code that performs more
predictably (especially when it comes to memory) as a first step.
This also opens up the possibility of further simplifications and
removing more retained state/memory as we go through the search phases.
@original-brownbear original-brownbear added >non-issue :Search Foundations/Search Catch all for Search Foundations labels Dec 25, 2024
@elasticsearchmachine elasticsearchmachine added v9.0.0 Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch labels Dec 25, 2024
@elasticsearchmachine
Copy link
Collaborator

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

@@ -180,4 +179,95 @@ ShardSearchRequest rewriteShardSearchRequest(ShardSearchRequest request) {

return request;
}

private static List<DfsKnnResults> mergeKnnResults(SearchRequest request, List<DfsSearchResult> dfsSearchResults) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two static methods move without changes, they are now only used from this class.

@drempapis
Copy link
Contributor

This change makes the code more readable. The classes' sequence is discrete and easy to follow.

SearchDfsQueryThenFetchAsyncAction 
->DfsQueryPhase 
->SearchQueryThenFetchAsyncAction 
->(RankFeaturePhase|FetchSearchPhase) 
->ExpandSearchPhase 
->FetchLookupFieldsPhase

It reminds me of the chain of responsibility pattern, where each step has a nextHandler (nextPhase) and a handle (run) method. It may be helpful to apply this logic here by exporting it to an interface implemented, e.g., by SearchPhase.

Similar logic can also be applied in the RankFeaturePhase, not updated in this pr.

Copy link
Contributor

@drempapis drempapis left a comment

Choose a reason for hiding this comment

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

LGTM! This is a refactoring task, which makes the code easier to follow. Thank you @original-brownbear

@original-brownbear original-brownbear added auto-backport Automatically create backport pull requests when merged v8.19.0 labels Feb 24, 2025
@original-brownbear
Copy link
Contributor Author

Thanks Dimitris!

@original-brownbear original-brownbear merged commit cae7f0a into elastic:main Feb 24, 2025
17 checks passed
@original-brownbear original-brownbear deleted the inheritance-yeay branch February 24, 2025 13:26
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
8.x Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 119272

original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Apr 9, 2025
…itions (elastic#119272)

We only need the extensibility for testing and it's a lot easier to
reason about the code if we have explicit methods instead of overly
complicated composition with lots of redundant references being retained
all over the place.

-> lets simplify to inheritance and get shorter code that performs more
predictably (especially when it comes to memory) as a first step.
This also opens up the possibility of further simplifications and
removing more retained state/memory as we go through the search phases.
original-brownbear added a commit that referenced this pull request Apr 9, 2025
…120079) (#126511)

* Remove unnecessary interfaces/abstractions from search phases (#120079)

A couple obvious cleanups where declared general interfaces aren't actually used
+ we shouldn't escape potentially heavy-weight search phases just to get their name
in a call so adjusted the failure API.

* Use inheritance instead of composition to simplify search phase transitions (#119272)

We only need the extensibility for testing and it's a lot easier to
reason about the code if we have explicit methods instead of overly
complicated composition with lots of redundant references being retained
all over the place.

-> lets simplify to inheritance and get shorter code that performs more
predictably (especially when it comes to memory) as a first step.
This also opens up the possibility of further simplifications and
removing more retained state/memory as we go through the search phases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged >non-issue :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants