Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Refactor Search to be Generic #696

Open
nik-suri opened this issue Feb 17, 2021 · 0 comments
Open

Refactor Search to be Generic #696

nik-suri opened this issue Feb 17, 2021 · 0 comments

Comments

@nik-suri
Copy link

nik-suri commented Feb 17, 2021

I want to perform some custom handling of my search result. For example, I run a search across three different indices which share some common fields. For example:

Search search = new Search.Builder(searchSourceBuilder.toString())
    .addIndex("index1")
    .addIndex("index2")
    .addIndex("index3")
    .build()

However, since these indices don't all have the exact same schema, reading this response isn't as simple as calling

List<Article> articles = result.getSourceAsObjectList(Article.class);

Instead, I would like to iterate over each json element. Then depending on the index which the json element is from, I would like to cast the json to the corresponding class.

Currently, the Search class is tied to the SearchResult class. Therefore, if I wanted to use my own CustomSearchResult, I would need to copy/paste the entire Search class, only to change SearchResult to CustomSearchResult in the three places where it appears.

A better way to do this would be to use a generic class - GenericSearch, which implements all of the code in Search except using a generic type for the SearchResult class. The signature could look like this:

public class GenericSearch extends AbstractAction<T extends JestResult> { ... }

We can maintain backwards compatibility by using public class Search extends GenericSearch<SearchResult> {}. More importantly, anyone can use their own result handlers in a maintainable way by using either public class CustomSearch extends GenericSearch<CustomSearchResult> {} or (inline):

GenericSearch search = new GenericSearch<CustomSearchResult>.Builder(searchSourceBuilder.toString())
    .addIndex("index1")
    .addIndex("index2")
    .addIndex("index3")
    .build()

Would love to hear feedback from the team on this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant