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

Extract common code for ES and OS into a base class #2664

Merged
merged 4 commits into from
Jun 20, 2022
Merged

Conversation

masci
Copy link
Contributor

@masci masci commented Jun 15, 2022

Related issue: #1764
Proposed changes:

  • Create a new base class, ElasticsearchCommon implementing the code that Elasticsearch and Opensearch have in common at this moment
  • There should be no functional change, with ElasticsearchDocumentStore implementing the same type as before

Before

┌───────────────────────────┐                     
│                           │                     
│   KeywordDocumentStore    │                     
│                           │                     
│                           │                     
└───────────────────────────┘                     
              ▲                                   
              │                                   
              │                                   
┌───────────────────────────┐                     
│                           │                     
│ElasticsearchDocumentStore │       ┌────────────┐
│                           │──────▶│ ES Client  │
│                           │       └────────────┘
└───────────────────────────┘                     
              ▲                                   
              │                                   
              │                                   
┌───────────────────────────┐                     
│                           │                     
│  OpenSearchDocumentStore  │                     
│                           │                     
│                           │                     
└───────────────────────────┘                     

After

┌───────────────────────────┐                     
│                           │                     
│   KeywordDocumentStore    │                     
│                           │                     
│                           │                     
└───────────────────────────┘                     
              ▲                                   
              │                                   
              │                                   
┌───────────────────────────┐                     
│                           │                     
│    ElasticsearchCommon    │                     
│                           │                     
│                           │                     
└───────────────────────────┘                     
              ▲                                   
              │                                   
              │                                   
┌───────────────────────────┐                     
│                           │                     
│ElasticsearchDocumentStore │       ┌────────────┐
│                           │──────▶│ ES Client  │
│                           │       └────────────┘
└───────────────────────────┘                     
              ▲                                   
              │                                   
              │                                   
┌───────────────────────────┐                     
│                           │                     
│  OpenSearchDocumentStore  │                     
│                           │                     
│                           │                     
└───────────────────────────┘                     

Next step (upcoming PR)

                          ┌───────────────────────────┐                                            
                          │                           │                                            
                          │   KeywordDocumentStore    │                                            
                          │                           │                                            
                          │                           │                                            
                          └───────────────────────────┘                                            
                                        ▲                                                          
                                        │                                                          
                                        │                                                          
                          ┌───────────────────────────┐                                            
                          │                           │                                            
                          │    ElasticsearchCommon    │                                            
                          │                           │                                            
                          │                           │                                            
                          └───────────────────────────┘                                            
                                        ▲                                                          
              ┌─────────────────────────┴────────────────────────┐                                 
              │                                                  │                                 
┌───────────────────────────┐                      ┌───────────────────────────┐                   
│                           │                      │                           │                   
│ElasticsearchDocumentStore │      ┌────────────┐  │  OpenSearchDocumentStore  │     ┌────────────┐
│                           │─────▶│ ES Client  │  │                           │────▶│ OS Client  │
│                           │      └────────────┘  │                           │     └────────────┘
└───────────────────────────┘                      └───────────────────────────┘                   

Status (please check what you already did):

Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

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

Looks good! I have two questions regarding ElasticsearchCommon, nothing blocking though 🙂

return hosts


class ElasticsearchCommon(KeywordDocumentStore):
Copy link
Contributor

Choose a reason for hiding this comment

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

Two small issues here:

  • I think this should be an abstract class, because it's not meant to be implemented. It has no abstract methods for now, but I imagine it will once you move to the final hierarchy that you designed in the PR description. So if this is meant to be temporary we can make this abstract later.

  • I'm not too fond of the name. We use to prefix these base abstract classes with Base, so if I had to follow the pattern, this would be called BaseElasticsearchDocumentStore or anyway Base[Something]DocumentStore. The current ElasticsearchCommon is way too generic imho, doesn't imply anywhere we're even talking about a docstore. If this objection sounds too far-fetched, be aware that until very short ago we had an ElasticsearchRetriever, so ElasticsearchCommon sounds like a set of functions that could serve both, which is not correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • I think this should be an abstract class,

I tried, but in the end we don't need abstract methods here, and adding one just for the sake of make the class not instantiable is wrong IMO. I think good documentation and clean code is enough in Pythonland, but If this is important, I'd rather raise an error in the __new__ method of the class

I'm not too fond of the name.

Me too, it's pretty bad and this is a code smell - a class hierarchy isn't probably the right tool here, but this is way off topic 😅. I'll change it to BaseElasticsearchDocumentStore.

Anyways, you touch an interesting point here:

If this objection sounds too far-fetched, be aware that until very short ago we had an ElasticsearchRetriever, so ElasticsearchCommon sounds like a set of functions that could serve both, which is not correct.

I disagree - when thinking about names we should take into consideration the fully qualified name: ElasticsearchCommon alone is obviously bad, but its name is actually haystack.document_stores.elasticsearch. ElasticsearchCommon. It's not unusual to leverage fully qualified names in API design: for example, in Django you have a class named DatabaseWrapper in every backend instead of MysqlWrapper, PostgresqlWrapper and so on. Not something I would necessarily add to Haystack, just wanted to mention that packages and modules should be used to provide namespacing when writing Python APIs, as opposed to put all the effort in function or class names.

Copy link
Contributor

Choose a reason for hiding this comment

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

For the abstract class, I agree that if we have to do workarounds to make it really abstract then it's worthless. +1 for keeping it as it is.

For the name, nice idea! I still think it's better to use BaseElasticsearchDocumentStore here and I'm glad we agree, but I'll keep it in mind for other situations. Sounds like a handy solution to keep names short.

@masci masci merged commit 79b287b into master Jun 20, 2022
@masci masci deleted the massi/opensearch-py branch June 20, 2022 07:47
@julian-risch julian-risch mentioned this pull request Jun 21, 2022
2 tasks
Krak91 pushed a commit to Krak91/haystack that referenced this pull request Jul 26, 2022
* extract common code for ES and OS into a base class

* Update Documentation & Code Style

* give the base class a more obvious name

* Update Documentation & Code Style

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants