Skip to content
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

Apply indexes changes #478

Merged
merged 8 commits into from
Jun 23, 2022
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
55 changes: 37 additions & 18 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hmac
import json
import datetime
from urllib import parse
from typing import Any, Dict, List, Optional, Union
from meilisearch.index import Index
from meilisearch.config import Config
Expand Down Expand Up @@ -78,46 +79,64 @@ def delete_index(self, uid: str) -> Dict[str, Any]:

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

def get_indexes(self) -> List[Index]:
def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
"""Get all indexes.

Parameters
----------
parameters (optional):
parameters accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes

Returns
-------
indexes:
List of Index instances.
Dictionary with limit, offset, total and results a list of Index instances.

Raises
------
MeiliSearchApiError
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
"""
response = self.http.get(self.config.paths.index)

return [
Index(
self.config,
index["uid"],
index["primaryKey"],
index["createdAt"],
index["updatedAt"],
)
for index in response
]

def get_raw_indexes(self) -> List[Dict[str, Any]]:
if parameters is None:
parameters = {}
response = self.http.get(
f'{self.config.paths.index}?{parse.urlencode(parameters)}'
)
response['results'] = [
Index(
self.config,
index["uid"],
index["primaryKey"],
index["createdAt"],
index["updatedAt"],
)
for index in response['results']
]
return response

def get_raw_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
"""Get all indexes in dictionary format.

Parameters
----------
parameters (optional):
parameters accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes

alallema marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
indexes:
List of indexes in dictionary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])
Dictionary with limit, offset, total and results a list of indexes in dictionary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])

Raises
------
MeiliSearchApiError
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
"""
return self.http.get(self.config.paths.index)
if parameters is None:
parameters = {}
return self.http.get(
f'{self.config.paths.index}?{parse.urlencode(parameters)}'
)

def get_index(self, uid: str) -> Index:
"""Get the index.
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def clear_indexes(client):
yield
# Deletes all the indexes in the Meilisearch instance.
indexes = client.get_indexes()
for index in indexes:
for index in indexes['results']:
task = client.index(index.uid).delete()
client.wait_for_task(task['uid'])

Expand Down
26 changes: 19 additions & 7 deletions tests/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,34 @@ def test_create_index_with_uid_in_options(client):
def test_get_indexes(client):
"""Tests getting all indexes."""
response = client.get_indexes()
uids = [index.uid for index in response]
assert isinstance(response, list)
uids = [index.uid for index in response['results']]
assert isinstance(response['results'], list)
assert common.INDEX_UID in uids
assert common.INDEX_UID2 in uids
assert common.INDEX_UID3 in uids
assert len(response) == 3
assert len(response['results']) == 3

@pytest.mark.usefixtures("indexes_sample")
def test_get_indexes_with_parameters(client):
"""Tests getting all indexes."""
response = client.get_indexes(parameters={'limit':1, 'offset': 1})
assert len(response['results']) == 1

@pytest.mark.usefixtures("indexes_sample")
def test_get_raw_indexes(client):
response = client.get_raw_indexes()
uids = [index['uid'] for index in response]
assert isinstance(response, list)
uids = [index['uid'] for index in response['results']]
assert isinstance(response['results'], list)
assert common.INDEX_UID in uids
assert common.INDEX_UID2 in uids
assert common.INDEX_UID3 in uids
assert len(response) == 3
assert len(response['results']) == 3

@pytest.mark.usefixtures("indexes_sample")
def test_get_raw_indexeswith_parameters(client):
response = client.get_raw_indexes(parameters={'limit':1, 'offset': 1})
assert isinstance(response['results'], list)
assert len(response['results']) == 1

def test_index_with_any_uid(client):
index = client.index('anyUID')
Expand Down Expand Up @@ -165,7 +177,7 @@ def test_delete_index_by_client(client):
client.wait_for_task(response['uid'])
with pytest.raises(Exception):
client.get_index(uid=common.INDEX_UID3)
assert len(client.get_indexes()) == 0
assert len(client.get_indexes()['results']) == 0

@pytest.mark.usefixtures("indexes_sample")
def test_delete(client):
Expand Down