Skip to content

Change create_index prototype #99

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 3 commits into from
Jun 15, 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
import meilisearch

client = meilisearch.Client('http://127.0.0.1:7700', 'masterKey')
index = client.create_index(uid='books') # If your index does not exist
index = client.get_index('books') # If you already created your index
index = client.create_index('books') # If your index does not exist
index = client.get_index('books') # If you already created your index

documents = [
{ 'book_id': 123, 'title': 'Pride and Prejudice' },
Expand Down Expand Up @@ -118,9 +118,9 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
#### Create an index <!-- omit in toc -->
```python
# Create an index
client.create_index(uid='books')
client.create_index('books')
# Create an index and give the primary-key
client.create_index(uid='books', primary_key='book_id')
client.create_index('books', {'primaryKey': 'book_id'})
```

#### List all indexes <!-- omit in toc -->
Expand Down
13 changes: 6 additions & 7 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, url, apiKey=None):
self.config = Config(url, apiKey)
self.http = HttpRequests(self.config)

def create_index(self, uid, primary_key=None, name=None):
def create_index(self, uid, options=None):
"""Create an index.

If the argument `uid` isn't passed in, it will be generated
Expand All @@ -35,10 +35,9 @@ def create_index(self, uid, primary_key=None, name=None):
----------
uid: str
UID of the index
primary_key: str, optional
Attribute used as unique document identifier
name: str, optional
Name of the index
options: dict, optional
Options passed during index creation (ex: primaryKey)

Returns
-------
index : Index
Expand All @@ -48,8 +47,8 @@ def create_index(self, uid, primary_key=None, name=None):
HTTPError
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
"""
index = Index(self.config, uid=uid)
index.create(self.config, uid=uid, primary_key=primary_key, name=name)
index = Index(self.config, uid)
index.create(self.config, uid, options)
return index

def get_indexes(self):
Expand Down
21 changes: 8 additions & 13 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ def get_primary_key(self):
return self.info()['primaryKey']

@staticmethod
def create(config, **body):
def create(config, uid, options=None):
"""Create an index.

Parameters
----------
body: **kwargs
Accepts uid, name and primaryKey as parameter.
uid: str
UID of the index
options: dict, optional
Options passed during index creation (ex: primaryKey)

Returns
-------
Expand All @@ -101,16 +103,9 @@ def create(config, **body):
HTTPError
In case of any error found here https://docs.meilisearch.com/references/#errors-status-code
"""
payload = {}
uid = body.get('uid', None)
if uid is not None:
payload['uid'] = uid
name = body.get('name', None)
if name is not None:
payload['name'] = name
primary_key = body.get('primary_key', None)
if primary_key is not None:
payload['primaryKey'] = primary_key
if options is None:
options = {}
payload = {'uid': uid, **options}
return HttpRequests(config).post(config.paths.index, payload)

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions meilisearch/tests/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestIndex:

client = meilisearch.Client(BASE_URL, MASTER_KEY)
index_uid = 'indexUID'
index_uid2 = 'indexUID2'

def setup_class(self):
clear_all_indexes(self.client)
Expand All @@ -17,6 +18,14 @@ def test_create_index(self):
index = self.client.create_index(uid=self.index_uid)
assert isinstance(index, object)
assert index.uid == self.index_uid
assert index.get_primary_key() is None

def test_create_index_with_primary_key(self):
"""Tests creating an index with a primary key"""
index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'})
assert isinstance(index, object)
assert index.uid == self.index_uid2
assert index.get_primary_key() == 'book_id'

def test_get_indexes(self):
"""Tests getting all indexes"""
Expand Down Expand Up @@ -69,3 +78,9 @@ def test_delete_index(self):
assert response.status_code == 204
with pytest.raises(Exception):
self.client.get_index(uid=self.index_uid).info()
index = self.client.get_index(uid=self.index_uid2)
response = index.delete()
assert isinstance(response, object)
assert response.status_code == 204
with pytest.raises(Exception):
self.client.get_index(uid=self.index_uid2).info()