Skip to content

Implement get_or_create_index method #114

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

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
client.create_index('books')
# Create an index and give the primary-key
client.create_index('books', {'primaryKey': 'book_id'})
# Get an index or create it if it doesn't exist
client.get_or_create_index('books', {'primaryKey': 'book_id'})
```

#### List all indexes <!-- omit in toc -->
Expand Down
28 changes: 28 additions & 0 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from meilisearch.index import Index
from meilisearch.config import Config
from meilisearch._httprequests import HttpRequests
from meilisearch.errors import MeiliSearchApiError

class Client():
"""
Expand Down Expand Up @@ -80,6 +81,33 @@ def get_index(self, uid):
"""
return Index.get_index(self.config, uid=uid)

def get_or_create_index(self, uid, options=None):
"""Get an index, or create it if it doesn't exist.

Parameters
----------
uid: str
UID of the index
options: dict, optional
Options passed during index creation (ex: primaryKey)

Returns
-------
index : Index
an instance of Index containing the information of the retrieved or newly created index
Raises
------
HTTPError
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
"""
index = self.get_index(uid)
try:
index.create(self.config, uid, options)
except MeiliSearchApiError as err:
if err.error_code != 'index_already_exists':
raise err
return index

def get_all_stats(self):
"""Get all stats of MeiliSearch

Expand Down
27 changes: 27 additions & 0 deletions meilisearch/tests/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestIndex:
index_uid = 'indexUID'
index_uid2 = 'indexUID2'
index_uid3 = 'indexUID3'
index_uid4 = 'indexUID4'

def setup_class(self):
clear_all_indexes(self.client)
Expand Down Expand Up @@ -56,6 +57,32 @@ def test_get_index_with_none_uid(self):
with pytest.raises(Exception):
self.client.get_index(uid=None)

def test_get_or_create_index(self):
"""Test get_or_create_index method"""
# self.client.create_index(self.index_uid3)
index_1 = self.client.get_or_create_index(self.index_uid4)
index_2 = self.client.get_or_create_index(self.index_uid4)
index_3 = self.client.get_or_create_index(self.index_uid4)
assert index_1.uid == index_2.uid == index_3.uid == self.index_uid4
update = index_1.add_documents([{
'book_id': 1,
'name': "Some book"
}])
index_1.wait_for_pending_update(update['updateId'])
documents = index_2.get_documents()
assert len(documents) == 1
index_2.delete()
with pytest.raises(Exception):
self.client.get_index(index_3).info()

def test_get_or_create_index_with_primary_key(self):
"""Test get_or_create_index method with primary key"""
index_1 = self.client.get_or_create_index('books', {'primaryKey': self.index_uid4})
index_2 = self.client.get_or_create_index('books', {'primaryKey': 'some_wrong_key'})
assert index_1.get_primary_key() == self.index_uid4
assert index_2.get_primary_key() == self.index_uid4
index_1.delete()

def test_index_info(self):
"""Tests getting an index's info"""
index = self.client.get_index(uid=self.index_uid)
Expand Down