Repository: deepset-ai/haystack

Issue:
Unable to connect Open Distro for elasticsearch 
**Describe the bug**
I ran the docker image of the open distro for elastic search using WSL2 in windows the image is working perfectly fine and running on the localhost 9200 but I am unable to connect to this elastic search service using haystack. 
Haystack works perfectly fine with the elastic search but it doesn't work with open distro for elastic search. Can you please suggest any solution for the same?

**Error message**
![image](https://user-images.githubusercontent.com/43184379/127978790-84c11077-18db-4ac5-9987-4fd67aaa7fff.png)





Discussion:
Hi @Harshit1011 , sorry its not working for you. Could you paste here the command you use to start the docker and also the line of code you use to initialize the DocumentStore?
version: '3'
services:
  odfe-node1:
    image: amazon/opendistro-for-elasticsearch:1.13.2
    container_name: odfe-node1
    environment:
      - cluster.name=odfe-cluster
      - node.name=odfe-node1
      - discovery.seed_hosts=odfe-node1
      - cluster.initial_master_nodes=odfe-node1
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    volumes:
      - odfe-data1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9600:9600 # required for Performance Analyzer
    networks:
      - odfe-net
  kibana:
    image: amazon/opendistro-for-elasticsearch-kibana:1.13.2
    container_name: odfe-kibana
    ports:
      - 5601:5601
    expose:
      - "5601"
    environment:
      ELASTICSEARCH_URL: https://odfe-node1:9200
      ELASTICSEARCH_HOSTS: https://odfe-node1:9200
    networks:
      - odfe-net

volumes:
  odfe-data1:

networks:
  odfe-net:

<br>
<br>
<br>
<br>
<br>
i am using this docker compose file<br>
then Docker-compose up command<br>

<br><br>
"docstore_type": "OpenDistroElasticsearchDocumentStore"
Hmmmm I see, could you also provide for me the version of Haystack that you're using? OpenDistroForElasticSearch has been replaced by the OpenSearch framework and we have an OpenSearchDocumentStore in the latest versions which we are still actively maintaining. 
Currently, I am using haystack v0.9.0 .

@brandenchan any feedback

Resolving patch:
diff --git a/haystack/document_store/elasticsearch.py b/haystack/document_store/elasticsearch.py
--- a/haystack/document_store/elasticsearch.py
+++ b/haystack/document_store/elasticsearch.py
@@ -165,16 +165,8 @@ def _init_elastic_client(self,
                              ca_certs: Optional[str],
                              verify_certs: bool,
                              timeout: int) -> Elasticsearch:
-        # Create list of host(s) + port(s) to allow direct client connections to multiple elasticsearch nodes
-        if isinstance(host, list):
-            if isinstance(port, list):
-                if not len(port) == len(host):
-                    raise ValueError("Length of list `host` must match length of list `port`")
-                hosts = [{"host":h, "port":p} for h, p in zip(host,port)]
-            else:
-                hosts = [{"host": h, "port": port} for h in host]
-        else:
-            hosts = [{"host": host, "port": port}]
+
+        hosts = self._prepare_hosts(host, port)
 
         if (api_key or api_key_id) and not(api_key and api_key_id):
             raise ValueError("You must provide either both or none of `api_key_id` and `api_key`")
@@ -214,6 +206,19 @@ def _init_elastic_client(self,
                 f"Initial connection to Elasticsearch failed. Make sure you run an Elasticsearch instance at `{hosts}` and that it has finished the initial ramp up (can take > 30s).")
         return client
 
+    def _prepare_hosts(self, host, port):
+        # Create list of host(s) + port(s) to allow direct client connections to multiple elasticsearch nodes
+        if isinstance(host, list):
+            if isinstance(port, list):
+                if not len(port) == len(host):
+                    raise ValueError("Length of list `host` must match length of list `port`")
+                hosts = [{"host":h, "port":p} for h, p in zip(host,port)]
+            else:
+                hosts = [{"host": h, "port": port} for h in host]
+        else:
+            hosts = [{"host": host, "port": port}]
+        return hosts
+
     def _create_document_index(self, index_name: str):
         """
         Create a new index for storing documents. In case if an index with the name already exists, it ensures that
@@ -1129,7 +1134,15 @@ def _scale_embedding_score(self, score):
 
 
 class OpenDistroElasticsearchDocumentStore(OpenSearchDocumentStore):
-    def __init__(self):
+    """
+    A DocumentStore which has an Open Distro for Elasticsearch service behind it.
+    """
+    def __init__(self, host="https://admin:admin@localhost:9200/", similarity="cosine", **kwargs):
         logger.warning("Open Distro for Elasticsearch has been replaced by OpenSearch! "
                        "See https://opensearch.org/faq/ for details. "
-                       "We recommend using the OpenSearchDocumentStore instead.")
\ No newline at end of file
+                       "We recommend using the OpenSearchDocumentStore instead.")
+        super(OpenDistroElasticsearchDocumentStore, self).__init__(host=host,
+                                                                   similarity=similarity,
+                                                                   **kwargs)
+    def _prepare_hosts(self, host, port):
+        return host
\ No newline at end of file


Test patch: