Skip to content

Commit 538b23d

Browse files
committed
Add index method and make the get_index method do an HTTP call
1 parent c538744 commit 538b23d

File tree

5 files changed

+121
-94
lines changed

5 files changed

+121
-94
lines changed

meilisearch/client.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ def __init__(self, url, apiKey=None):
2929
def create_index(self, uid, options=None):
3030
"""Create an index.
3131
32-
If the argument `uid` isn't passed in, it will be generated
33-
by MeiliSearch.
34-
3532
Parameters
3633
----------
3734
uid: str
@@ -42,15 +39,14 @@ def create_index(self, uid, options=None):
4239
Returns
4340
-------
4441
index : Index
45-
an instance of Index containing the information of the newly created index
42+
An instance of Index containing the information of the newly created index
4643
Raises
4744
------
4845
HTTPError
4946
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
5047
"""
51-
index = Index(self.config, uid)
52-
index.create(self.config, uid, options)
53-
return index
48+
index_dict = Index.create(self.config, uid, options)
49+
return Index(self.config, index_dict['uid'], index_dict['primaryKey'])
5450

5551
def get_indexes(self):
5652
"""Get all indexes.
@@ -64,11 +60,11 @@ def get_indexes(self):
6460
list
6561
List of indexes in dictionnary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])
6662
"""
67-
return Index.get_indexes(self.config)
68-
63+
return Index.list_all(self.config)
6964

7065
def get_index(self, uid):
71-
"""Get an index.
66+
"""Get the index.
67+
This index should already exist.
7268
7369
Raises
7470
------
@@ -77,9 +73,23 @@ def get_index(self, uid):
7773
Returns
7874
-------
7975
index : Index
80-
an instance of Index containing the information of the index found
76+
An Index instance containing the information the fetched index
77+
"""
78+
index_dict = Index(self.config, uid).fetch_info()
79+
return Index(self.config, index_dict['uid'], index_dict['primaryKey'])
80+
81+
def index(self, uid):
82+
"""Create an Index instance.
83+
This method doesn't trigger any HTTP call.
84+
85+
Returns
86+
-------
87+
index : Index
88+
An Index instance
8189
"""
82-
return Index.get_index(self.config, uid=uid)
90+
if uid is not None:
91+
return Index(self.config, uid=uid)
92+
raise Exception('The index UID should not be None')
8393

8494
def get_or_create_index(self, uid, options=None):
8595
"""Get an index, or create it if it doesn't exist.
@@ -94,19 +104,19 @@ def get_or_create_index(self, uid, options=None):
94104
Returns
95105
-------
96106
index : Index
97-
an instance of Index containing the information of the retrieved or newly created index
107+
An instance of Index containing the information of the retrieved or newly created index
98108
Raises
99109
------
100110
HTTPError
101111
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
102112
"""
103-
index = self.get_index(uid)
113+
index_instance = self.index(uid)
104114
try:
105-
index.create(self.config, uid, options)
115+
index_instance = self.create_index(uid, options)
106116
except MeiliSearchApiError as err:
107117
if err.error_code != 'index_already_exists':
108118
raise err
109-
return index
119+
return index_instance
110120

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

meilisearch/index.py

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,78 @@ class Index():
1414

1515
config = None
1616
http = None
17-
uid = ""
17+
uid = None
18+
primary_key = None
1819

19-
def __init__(self, config, uid):
20+
def __init__(self, config, uid, primary_key=None):
2021
"""
2122
Parameters
2223
----------
2324
config : Config
24-
Config object containing permission and location of meilisearch
25+
Config object containing permission and location of MeiliSearch.
2526
uid: str
2627
Uid of the index on which to perform the index actions.
2728
index_path: str
28-
Index url path
29+
Index url path.
2930
"""
3031
self.config = config
31-
self.uid = uid
3232
self.http = HttpRequests(config)
33+
self.uid = uid
34+
self.primary_key = primary_key
3335

3436
def delete(self):
35-
"""Delete an index from meilisearch
37+
"""Delete the index.
3638
37-
Returns
38-
----------
39-
update: `dict`
40-
Dictionnary containing an update id to track the action:
41-
https://docs.meilisearch.com/references/updates.html#get-an-update-status
39+
Raises
40+
------
41+
HTTPError
42+
In case of any error found here https://docs.meilisearch.com/references/#errors-status-code
4243
"""
4344
return self.http.delete('{}/{}'.format(self.config.paths.index, self.uid))
4445

4546
def update(self, **body):
46-
"""Update an index from meilisearch
47+
"""Update the index.
4748
4849
Parameters
4950
----------
5051
body: **kwargs
5152
Accepts primaryKey as an updatable parameter.
5253
5354
Returns
54-
----------
55-
update: `dict`
56-
Dictionnary containing an update id to track the action:
57-
https://docs.meilisearch.com/references/updates.html#get-an-update-status
55+
-------
56+
index: `dict`
57+
Dictionnary containing index information.
5858
"""
5959
payload = {}
6060
primary_key = body.get('primaryKey', None)
6161
if primary_key is not None:
6262
payload['primaryKey'] = primary_key
63-
return self.http.put('{}/{}'.format(self.config.paths.index, self.uid), payload)
63+
response = self.http.put('{}/{}'.format(self.config.paths.index, self.uid), payload)
64+
self.primary_key = response['primaryKey']
65+
return response
6466

65-
def info(self):
66-
"""Get info of index
67+
def fetch_info(self):
68+
"""Fetch the info of the index.
6769
6870
Returns
69-
----------
71+
-------
7072
index: `dict`
71-
Dictionnary containing index information.
73+
Dictionnary containing the index information.
7274
"""
73-
return self.http.get('{}/{}'.format(self.config.paths.index, self.uid))
75+
index_dict = self.http.get('{}/{}'.format(self.config.paths.index, self.uid))
76+
self.primary_key = index_dict['primaryKey']
77+
return index_dict
7478

7579
def get_primary_key(self):
76-
"""Get the primary key
80+
"""Get the primary key.
7781
7882
Returns
79-
----------
83+
-------
8084
primary_key: str
8185
String containing primary key.
8286
"""
83-
return self.info()['primaryKey']
87+
self.primary_key = self.fetch_info()['primaryKey']
88+
return self.primary_key
8489

8590
@staticmethod
8691
def create(config, uid, options=None):
@@ -89,14 +94,14 @@ def create(config, uid, options=None):
8994
Parameters
9095
----------
9196
uid: str
92-
UID of the index
97+
UID of the index.
9398
options: dict, optional
94-
Options passed during index creation (ex: primaryKey)
99+
Options passed during index creation (ex: { 'primaryKey': 'name' }).
95100
96101
Returns
97102
-------
98103
index : Index
99-
an instance of Index containing the information of the newly created index
104+
An instance of Index containing the information of the newly created index.
100105
Raises
101106
------
102107
HTTPError
@@ -108,41 +113,22 @@ def create(config, uid, options=None):
108113
return HttpRequests(config).post(config.paths.index, payload)
109114

110115
@staticmethod
111-
def get_indexes(config):
112-
"""Get all indexes from meilisearch.
116+
def list_all(config):
117+
"""Get all indexes
113118
114119
Returns
115120
-------
116121
indexes : list
117-
List of indexes (dict)
122+
List of indexes. Each index is a dictionnary.
118123
Raises
119124
------
120125
HTTPError
121126
In case of any error found here https://docs.meilisearch.com/references/#errors-status-code
122127
"""
123128
return HttpRequests(config).get(config.paths.index)
124129

125-
@staticmethod
126-
def get_index(config, uid):
127-
"""Get Index instance from given index
128-
129-
If the argument `uid` aren't passed in, it will raise an exception.
130-
131-
Returns
132-
-------
133-
index : Index
134-
Instance of Index with the given index.
135-
Raises
136-
------
137-
Exception
138-
If index UID is missing.
139-
"""
140-
if uid is not None:
141-
return Index(config, uid=uid)
142-
raise Exception('Uid is needed to find index')
143-
144130
def get_all_update_status(self):
145-
"""Get all update status from MeiliSearch
131+
"""Get all update status
146132
147133
Returns
148134
----------
@@ -158,7 +144,7 @@ def get_all_update_status(self):
158144
)
159145

160146
def get_update_status(self, update_id):
161-
"""Get one update from MeiliSearch
147+
"""Get one update status
162148
163149
Parameters
164150
----------
@@ -224,7 +210,7 @@ def get_stats(self):
224210
)
225211

226212
def search(self, query, opt_params=None):
227-
"""Search in meilisearch
213+
"""Search in the index
228214
229215
Parameters
230216
----------

meilisearch/tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def clear_all_indexes(client):
77
indexes = client.get_indexes()
88
for index in indexes:
9-
client.get_index(index['uid']).delete()
9+
client.index(index['uid']).delete()
1010

1111
def wait_for_dump_creation(client, dump_uid):
1212
dump_status = client.get_dump_status(dump_uid)

meilisearch/tests/client/test_client_dumps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def setup_method(self, method):
2626
self.index.wait_for_pending_update(response['updateId'])
2727

2828
def teardown_method(self, method):
29-
self.client.get_index('indexUID-' + method.__name__).delete()
29+
self.client.index('indexUID-' + method.__name__).delete()
3030

3131
def teardown_class(self):
32-
"""Cleans all indexes in MEiliSearch when all the test are done"""
32+
"""Cleans all indexes in MeiliSearch when all the test are done"""
3333
clear_all_indexes(self.client)
3434

3535
def test_dump_creation(self):

0 commit comments

Comments
 (0)