From 99d7a5eb1301b0d446c8e39f8948aea685c80c13 Mon Sep 17 00:00:00 2001
From: seehi <65027140+seehi@users.noreply.github.com>
Date: Fri, 22 Mar 2024 05:20:23 +0800
Subject: [PATCH] fix unclosed session in es add function (#12135)

---
 .../vector_stores/elasticsearch/base.py       | 33 ++++++++++---------
 .../pyproject.toml                            |  2 +-
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
index c32056f71f..08d0d7e0c0 100644
--- a/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
+++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/llama_index/vector_stores/elasticsearch/base.py
@@ -375,22 +375,23 @@ class ElasticsearchStore(BasePydanticVectorStore):
             requests.append(request)
             return_ids.append(_id)
 
-        await async_bulk(
-            self.client, requests, chunk_size=self.batch_size, refresh=True
-        )
-        try:
-            success, failed = await async_bulk(
-                self.client, requests, stats_only=True, refresh=True
-            )
-            logger.debug(f"Added {success} and failed to add {failed} texts to index")
-
-            logger.debug(f"added texts {ids} to index")
-            return return_ids
-        except BulkIndexError as e:
-            logger.error(f"Error adding texts: {e}")
-            firstError = e.errors[0].get("index", {}).get("error", {})
-            logger.error(f"First error reason: {firstError.get('reason')}")
-            raise
+        async with self.client as client:
+            await async_bulk(client, requests, chunk_size=self.batch_size, refresh=True)
+            try:
+                success, failed = await async_bulk(
+                    client, requests, stats_only=True, refresh=True
+                )
+                logger.debug(
+                    f"Added {success} and failed to add {failed} texts to index"
+                )
+
+                logger.debug(f"added texts {ids} to index")
+                return return_ids
+            except BulkIndexError as e:
+                logger.error(f"Error adding texts: {e}")
+                firstError = e.errors[0].get("index", {}).get("error", {})
+                logger.error(f"First error reason: {firstError.get('reason')}")
+                raise
 
     def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
         """Delete node from Elasticsearch index.
diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/pyproject.toml b/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/pyproject.toml
index 12670ff21c..edf9f1c115 100644
--- a/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/pyproject.toml
+++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-elasticsearch/pyproject.toml
@@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
 license = "MIT"
 name = "llama-index-vector-stores-elasticsearch"
 readme = "README.md"
-version = "0.1.5"
+version = "0.1.6"
 
 [tool.poetry.dependencies]
 python = ">=3.8.1,<4.0"
-- 
GitLab