Skip to content

Commit d0d0120

Browse files
authored
Merge pull request #478 from meilisearch/index_changes
Apply indexes changes
2 parents 5cd9b36 + ccd86d7 commit d0d0120

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

meilisearch/client.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hmac
44
import json
55
import datetime
6+
from urllib import parse
67
from typing import Any, Dict, List, Optional, Union
78
from meilisearch.index import Index
89
from meilisearch.config import Config
@@ -78,46 +79,64 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
7879

7980
return self.http.delete(f'{self.config.paths.index}/{uid}')
8081

81-
def get_indexes(self) -> List[Index]:
82+
def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
8283
"""Get all indexes.
8384
85+
Parameters
86+
----------
87+
parameters (optional):
88+
parameters accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
89+
8490
Returns
8591
-------
8692
indexes:
87-
List of Index instances.
93+
Dictionary with limit, offset, total and results a list of Index instances.
8894
8995
Raises
9096
------
9197
MeiliSearchApiError
9298
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
9399
"""
94-
response = self.http.get(self.config.paths.index)
95-
96-
return [
97-
Index(
98-
self.config,
99-
index["uid"],
100-
index["primaryKey"],
101-
index["createdAt"],
102-
index["updatedAt"],
103-
)
104-
for index in response
105-
]
106-
107-
def get_raw_indexes(self) -> List[Dict[str, Any]]:
100+
if parameters is None:
101+
parameters = {}
102+
response = self.http.get(
103+
f'{self.config.paths.index}?{parse.urlencode(parameters)}'
104+
)
105+
response['results'] = [
106+
Index(
107+
self.config,
108+
index["uid"],
109+
index["primaryKey"],
110+
index["createdAt"],
111+
index["updatedAt"],
112+
)
113+
for index in response['results']
114+
]
115+
return response
116+
117+
def get_raw_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
108118
"""Get all indexes in dictionary format.
109119
120+
Parameters
121+
----------
122+
parameters (optional):
123+
parameters accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
124+
110125
Returns
111126
-------
112127
indexes:
113-
List of indexes in dictionary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])
128+
Dictionary with limit, offset, total and results a list of indexes in dictionary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])
114129
115130
Raises
116131
------
117132
MeiliSearchApiError
118133
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
119134
"""
120-
return self.http.get(self.config.paths.index)
135+
if parameters is None:
136+
parameters = {}
137+
return self.http.get(
138+
f'{self.config.paths.index}?{parse.urlencode(parameters)}'
139+
)
121140

122141
def get_index(self, uid: str) -> Index:
123142
"""Get the index.

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def clear_indexes(client):
2121
yield
2222
# Deletes all the indexes in the Meilisearch instance.
2323
indexes = client.get_indexes()
24-
for index in indexes:
24+
for index in indexes['results']:
2525
task = client.index(index.uid).delete()
2626
client.wait_for_task(task['taskUid'])
2727

tests/index/test_index.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,34 @@ def test_create_index_with_uid_in_options(client):
4040
def test_get_indexes(client):
4141
"""Tests getting all indexes."""
4242
response = client.get_indexes()
43-
uids = [index.uid for index in response]
44-
assert isinstance(response, list)
43+
uids = [index.uid for index in response['results']]
44+
assert isinstance(response['results'], list)
4545
assert common.INDEX_UID in uids
4646
assert common.INDEX_UID2 in uids
4747
assert common.INDEX_UID3 in uids
48-
assert len(response) == 3
48+
assert len(response['results']) == 3
49+
50+
@pytest.mark.usefixtures("indexes_sample")
51+
def test_get_indexes_with_parameters(client):
52+
"""Tests getting all indexes."""
53+
response = client.get_indexes(parameters={'limit':1, 'offset': 1})
54+
assert len(response['results']) == 1
4955

5056
@pytest.mark.usefixtures("indexes_sample")
5157
def test_get_raw_indexes(client):
5258
response = client.get_raw_indexes()
53-
uids = [index['uid'] for index in response]
54-
assert isinstance(response, list)
59+
uids = [index['uid'] for index in response['results']]
60+
assert isinstance(response['results'], list)
5561
assert common.INDEX_UID in uids
5662
assert common.INDEX_UID2 in uids
5763
assert common.INDEX_UID3 in uids
58-
assert len(response) == 3
64+
assert len(response['results']) == 3
65+
66+
@pytest.mark.usefixtures("indexes_sample")
67+
def test_get_raw_indexeswith_parameters(client):
68+
response = client.get_raw_indexes(parameters={'limit':1, 'offset': 1})
69+
assert isinstance(response['results'], list)
70+
assert len(response['results']) == 1
5971

6072
def test_index_with_any_uid(client):
6173
index = client.index('anyUID')
@@ -165,7 +177,7 @@ def test_delete_index_by_client(client):
165177
client.wait_for_task(response['taskUid'])
166178
with pytest.raises(Exception):
167179
client.get_index(uid=common.INDEX_UID3)
168-
assert len(client.get_indexes()) == 0
180+
assert len(client.get_indexes()['results']) == 0
169181

170182
@pytest.mark.usefixtures("indexes_sample")
171183
def test_delete(client):

0 commit comments

Comments
 (0)