From 95c73454ceb881577bb4f0ab8703f90f1a7382b3 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Fri, 16 Aug 2024 16:02:42 -0400 Subject: [PATCH 1/4] add has_index() --- pinecone/control/pinecone.py | 31 +++++++++++++++++-- .../control/serverless/test_has_index.py | 18 +++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/integration/control/serverless/test_has_index.py diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index b2b33fcc..f071135a 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -544,6 +544,33 @@ def describe_index(self, name: str): return IndexModel(description) + def has_index(self, name: str) -> bool: + """Checks if a Pinecone index exists. + + :param name: The name of the index to check for existence. + :return: Returns `True` if the index exists, `False` otherwise. + + ### Example Usage + + ```python + from pinecone import Pinecone + + client = YourClass() # Initialize your class instance + exists = client.index_exists("my_index_name") + + if exists: + print("The index exists.") + else: + print("The index does not exist.") + ``` + """ + + try: + self.index_api.describe_index(name) + return True + except Exception as e: + return False + def configure_index( self, name: str, @@ -737,12 +764,12 @@ def Index(self, name: str = "", host: str = "", **kwargs): pc = Pinecone(api_key=api_key) pc.create_index( - name='my-index', + name='my_index', dimension=1536, metric='cosine', spec=ServerlessSpec(cloud='aws', region='us-west-2') ) - index = pc.Index('my-index') + index = pc.Index('my_index') # Now you're ready to perform data operations index.query(vector=[...], top_k=10) diff --git a/tests/integration/control/serverless/test_has_index.py b/tests/integration/control/serverless/test_has_index.py new file mode 100644 index 00000000..ddef4ffd --- /dev/null +++ b/tests/integration/control/serverless/test_has_index.py @@ -0,0 +1,18 @@ +from tests.integration.helpers import random_string + + +class TestHasIndex: + def test_index_exists_success(self, client, create_sl_index_params): + name = create_sl_index_params["name"] + client.create_index(**create_sl_index_params) + has_index = client.has_index(name) + assert has_index == True + + def test_index_does_not_exist(self, client): + name = random_string(8) + has_index = client.has_index(name) + assert has_index == False + + def test_has_index_with_null_index_name(self, client): + has_index = client.has_index('') + assert has_index == False \ No newline at end of file From 56f4f98d147bb4dcb642fff29daf9293a3c8fd87 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Fri, 16 Aug 2024 16:13:44 -0400 Subject: [PATCH 2/4] fix linting issue --- pinecone/control/pinecone.py | 2 +- tests/integration/control/serverless/test_has_index.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index f071135a..e7b6780f 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -556,7 +556,7 @@ def has_index(self, name: str) -> bool: from pinecone import Pinecone client = YourClass() # Initialize your class instance - exists = client.index_exists("my_index_name") + exists = client.has_index("my_index_name") if exists: print("The index exists.") diff --git a/tests/integration/control/serverless/test_has_index.py b/tests/integration/control/serverless/test_has_index.py index ddef4ffd..7271b64c 100644 --- a/tests/integration/control/serverless/test_has_index.py +++ b/tests/integration/control/serverless/test_has_index.py @@ -15,4 +15,4 @@ def test_index_does_not_exist(self, client): def test_has_index_with_null_index_name(self, client): has_index = client.has_index('') - assert has_index == False \ No newline at end of file + assert has_index == False From 207ac95ec21f9e4793b2417505920e64eb9533ae Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Fri, 16 Aug 2024 16:20:37 -0400 Subject: [PATCH 3/4] fix the linting error x2 --- tests/integration/control/serverless/test_has_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/control/serverless/test_has_index.py b/tests/integration/control/serverless/test_has_index.py index 7271b64c..11b96b78 100644 --- a/tests/integration/control/serverless/test_has_index.py +++ b/tests/integration/control/serverless/test_has_index.py @@ -14,5 +14,5 @@ def test_index_does_not_exist(self, client): assert has_index == False def test_has_index_with_null_index_name(self, client): - has_index = client.has_index('') + has_index = client.has_index("") assert has_index == False From 3b7fbee106af87bd32ef106a610da144643e5778 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Wed, 28 Aug 2024 11:49:45 -0400 Subject: [PATCH 4/4] update has_index() --- pinecone/control/pinecone.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index e7b6780f..7d6ce02e 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -553,22 +553,22 @@ def has_index(self, name: str) -> bool: ### Example Usage ```python + import os from pinecone import Pinecone - client = YourClass() # Initialize your class instance - exists = client.has_index("my_index_name") + api_key = os.environ.get("PINECONE_API_KEY") + pc = Pinecone(api_key=api_key) - if exists: - print("The index exists.") + if pc.has_index("my_index_name"): + print("The index exists") else: - print("The index does not exist.") + print("The index does not exist") ``` """ - try: - self.index_api.describe_index(name) + if name in self.list_indexes().names(): return True - except Exception as e: + else: return False def configure_index(