Skip to content

Commit 760c6c6

Browse files
curquizabidoubiwa
andauthored
Change create_index prototype (#99)
* Change create_index prototype * Update meilisearch/client.py Co-authored-by: cvermand <[email protected]> * Update meilisearch/index.py Co-authored-by: cvermand <[email protected]> Co-authored-by: cvermand <[email protected]>
1 parent e281f8a commit 760c6c6

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
6767
import meilisearch
6868

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

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

126126
#### List all indexes <!-- omit in toc -->

meilisearch/client.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, url, apiKey=None):
2525
self.config = Config(url, apiKey)
2626
self.http = HttpRequests(self.config)
2727

28-
def create_index(self, uid, primary_key=None, name=None):
28+
def create_index(self, uid, options=None):
2929
"""Create an index.
3030
3131
If the argument `uid` isn't passed in, it will be generated
@@ -35,10 +35,9 @@ def create_index(self, uid, primary_key=None, name=None):
3535
----------
3636
uid: str
3737
UID of the index
38-
primary_key: str, optional
39-
Attribute used as unique document identifier
40-
name: str, optional
41-
Name of the index
38+
options: dict, optional
39+
Options passed during index creation (ex: primaryKey)
40+
4241
Returns
4342
-------
4443
index : Index
@@ -48,8 +47,8 @@ def create_index(self, uid, primary_key=None, name=None):
4847
HTTPError
4948
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
5049
"""
51-
index = Index(self.config, uid=uid)
52-
index.create(self.config, uid=uid, primary_key=primary_key, name=name)
50+
index = Index(self.config, uid)
51+
index.create(self.config, uid, options)
5352
return index
5453

5554
def get_indexes(self):

meilisearch/index.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ def get_primary_key(self):
8484
return self.info()['primaryKey']
8585

8686
@staticmethod
87-
def create(config, **body):
87+
def create(config, uid, options=None):
8888
"""Create an index.
8989
9090
Parameters
9191
----------
92-
body: **kwargs
93-
Accepts uid, name and primaryKey as parameter.
92+
uid: str
93+
UID of the index
94+
options: dict, optional
95+
Options passed during index creation (ex: primaryKey)
9496
9597
Returns
9698
-------
@@ -101,16 +103,9 @@ def create(config, **body):
101103
HTTPError
102104
In case of any error found here https://docs.meilisearch.com/references/#errors-status-code
103105
"""
104-
payload = {}
105-
uid = body.get('uid', None)
106-
if uid is not None:
107-
payload['uid'] = uid
108-
name = body.get('name', None)
109-
if name is not None:
110-
payload['name'] = name
111-
primary_key = body.get('primary_key', None)
112-
if primary_key is not None:
113-
payload['primaryKey'] = primary_key
106+
if options is None:
107+
options = {}
108+
payload = {'uid': uid, **options}
114109
return HttpRequests(config).post(config.paths.index, payload)
115110

116111
@staticmethod

meilisearch/tests/index/test_index.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class TestIndex:
88

99
client = meilisearch.Client(BASE_URL, MASTER_KEY)
1010
index_uid = 'indexUID'
11+
index_uid2 = 'indexUID2'
1112

1213
def setup_class(self):
1314
clear_all_indexes(self.client)
@@ -17,6 +18,14 @@ def test_create_index(self):
1718
index = self.client.create_index(uid=self.index_uid)
1819
assert isinstance(index, object)
1920
assert index.uid == self.index_uid
21+
assert index.get_primary_key() is None
22+
23+
def test_create_index_with_primary_key(self):
24+
"""Tests creating an index with a primary key"""
25+
index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'})
26+
assert isinstance(index, object)
27+
assert index.uid == self.index_uid2
28+
assert index.get_primary_key() == 'book_id'
2029

2130
def test_get_indexes(self):
2231
"""Tests getting all indexes"""
@@ -69,3 +78,9 @@ def test_delete_index(self):
6978
assert response.status_code == 204
7079
with pytest.raises(Exception):
7180
self.client.get_index(uid=self.index_uid).info()
81+
index = self.client.get_index(uid=self.index_uid2)
82+
response = index.delete()
83+
assert isinstance(response, object)
84+
assert response.status_code == 204
85+
with pytest.raises(Exception):
86+
self.client.get_index(uid=self.index_uid2).info()

0 commit comments

Comments
 (0)