Skip to content

Commit 352d2b7

Browse files
committed
fixes
1 parent ff2639d commit 352d2b7

9 files changed

+67
-67
lines changed

meilisearch/index.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from meilisearch._httprequests import HttpRequests
66
from meilisearch.config import Config
77
from meilisearch.task import get_task, get_tasks, wait_for_task
8-
from meilisearch.models import DocumentsResults, TaskInfo, TaskResults, IndexStatsResults
8+
from meilisearch.models import DocumentsResults, Task, TaskInfo, TaskResults, IndexStats
99

1010
# pylint: disable=too-many-public-methods
1111
class Index():
@@ -152,7 +152,7 @@ def get_tasks(self, parameters: Optional[Dict[str, Any]] = None) -> TaskResults:
152152
tasks = get_tasks(self.config, parameters=parameters)
153153
return TaskResults(tasks)
154154

155-
def get_task(self, uid: int) -> TaskInfo:
155+
def get_task(self, uid: int) -> Task:
156156
"""Get one task through the route of a specific index.
157157
158158
Parameters
@@ -171,13 +171,13 @@ def get_task(self, uid: int) -> TaskInfo:
171171
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
172172
"""
173173
task = get_task(self.config, uid)
174-
return TaskInfo(**task)
174+
return Task(**task)
175175

176176
def wait_for_task(
177177
self, uid: int,
178178
timeout_in_ms: int = 5000,
179179
interval_in_ms: int = 50,
180-
) -> TaskInfo:
180+
) -> Task:
181181
"""Wait until Meilisearch processes a task until it fails or succeeds.
182182
183183
Parameters
@@ -200,9 +200,9 @@ def wait_for_task(
200200
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
201201
"""
202202
task = wait_for_task(self.config, uid, timeout_in_ms, interval_in_ms)
203-
return TaskInfo(**task)
203+
return Task(**task)
204204

205-
def get_stats(self) -> IndexStatsResults:
205+
def get_stats(self) -> IndexStats:
206206
"""Get stats of the index.
207207
208208
Get information about the number of documents, field frequencies, ...
@@ -221,7 +221,7 @@ def get_stats(self) -> IndexStatsResults:
221221
stats = self.http.get(
222222
f'{self.config.paths.index}/{self.uid}/{self.config.paths.stat}'
223223
)
224-
return IndexStatsResults(**stats)
224+
return IndexStats(**stats)
225225

226226
def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
227227
"""Search in the index.
@@ -315,7 +315,7 @@ def add_documents(
315315
self,
316316
documents: List[Dict[str, Any]],
317317
primary_key: Optional[str] = None,
318-
) -> Dict[str, Any]:
318+
) -> TaskInfo:
319319
"""Add documents to the index.
320320
321321
Parameters
@@ -337,14 +337,15 @@ def add_documents(
337337
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
338338
"""
339339
url = self._build_url(primary_key)
340-
return self.http.post(url, documents)
340+
response = self.http.post(url, documents)
341+
return TaskInfo(**response)
341342

342343
def add_documents_in_batches(
343344
self,
344345
documents: List[Dict[str, Any]],
345346
batch_size: int = 1000,
346347
primary_key: Optional[str] = None,
347-
) -> List[Dict[str, Any]]:
348+
) -> List[TaskInfo]:
348349
"""Add documents to the index in batches.
349350
350351
Parameters
@@ -369,13 +370,13 @@ def add_documents_in_batches(
369370
Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
370371
"""
371372

372-
uids = []
373+
tasks: List[TaskInfo] = []
373374

374375
for document_batch in self._batch(documents, batch_size):
375-
uid = self.add_documents(document_batch, primary_key)
376-
uids.append(uid)
376+
task = self.add_documents(document_batch, primary_key)
377+
tasks.append(task)
377378

378-
return uids
379+
return tasks
379380

380381
def add_documents_json(
381382
self,
@@ -494,7 +495,7 @@ def update_documents(
494495
self,
495496
documents: List[Dict[str, Any]],
496497
primary_key: Optional[str] = None
497-
) -> Dict[str, Any]:
498+
) -> TaskInfo:
498499
"""Update documents in the index.
499500
500501
Parameters
@@ -516,14 +517,15 @@ def update_documents(
516517
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
517518
"""
518519
url = self._build_url(primary_key)
519-
return self.http.put(url, documents)
520+
response = self.http.put(url, documents)
521+
return TaskInfo(**response)
520522

521523
def update_documents_in_batches(
522524
self,
523525
documents: List[Dict[str, Any]],
524526
batch_size: int = 1000,
525527
primary_key: Optional[str] = None
526-
) -> List[Dict[str, Any]]:
528+
) -> List[TaskInfo]:
527529
"""Update documents to the index in batches.
528530
529531
Parameters
@@ -548,15 +550,15 @@ def update_documents_in_batches(
548550
Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
549551
"""
550552

551-
uids = []
553+
tasks = []
552554

553555
for document_batch in self._batch(documents, batch_size):
554-
uid = self.update_documents(document_batch, primary_key)
555-
uids.append(uid)
556+
update_task = self.update_documents(document_batch, primary_key)
557+
tasks.append(update_task)
556558

557-
return uids
559+
return tasks
558560

559-
def delete_document(self, document_id: str) -> Dict[str, Any]:
561+
def delete_document(self, document_id: str) -> TaskInfo:
560562
"""Delete one document from the index.
561563
562564
Parameters
@@ -575,9 +577,10 @@ def delete_document(self, document_id: str) -> Dict[str, Any]:
575577
MeiliSearchApiError
576578
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
577579
"""
578-
return self.http.delete(
580+
response = self.http.delete(
579581
f'{self.config.paths.index}/{self.uid}/{self.config.paths.document}/{document_id}'
580582
)
583+
return TaskInfo(**response)
581584

582585
def delete_documents(self, ids: List[str]) -> Dict[str, int]:
583586
"""Delete multiple documents from the index.

meilisearch/models.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
from dataclasses import dataclass
21
from typing import Any, List, Dict, Optional
32
from camel_converter.pydantic_base import CamelBase
43

5-
@dataclass
6-
class DocumentsResults:
4+
class DocumentsResults(CamelBase):
75
results: List[Dict[str, Any]]
86
offset: int
97
limit: int
108
total: int
119

1210
class Task(CamelBase):
13-
uid: Optional[str]
11+
uid: str
1412
index_uid: str
15-
task_uid: Optional[str]
1613
status: str
1714
type: str
1815
details: Optional[Dict[str, Any]]
19-
duration: Optional[str]
16+
duration: str
2017
enqueued_at: str
21-
started_at: Optional[str]
22-
finished_at: Optional[str]
18+
started_at: str
19+
finished_at: str
2320

2421
class TaskInfo(CamelBase):
25-
uid: Optional[str]
26-
index_uid: str
2722
task_uid: Optional[str]
23+
index_uid: str
2824
status: str
2925
type: str
3026
details: Optional[Dict[str, Any]]
@@ -40,7 +36,7 @@ def __init__(self, resp: Dict[str, Any]) -> None:
4036
self.from_: int = resp['from']
4137
self.next_: int = resp['next']
4238

43-
class IndexStatsResults(CamelBase):
39+
class IndexStats(CamelBase):
4440
number_of_documents: int
4541
is_indexing: bool
4642
field_distribution: Dict[str, Any]

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def index_with_documents(empty_index, small_movies):
8989
def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
9090
index = empty_index(index_uid)
9191
task = index.add_documents(documents)
92-
index.wait_for_task(task['taskUid'])
92+
index.wait_for_task(task.task_uid)
9393
return index
9494
return index_maker
9595

tests/index/test_index_document_meilisearch.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def test_add_documents(empty_index, small_movies):
1616
"""Tests adding new documents to a clean index."""
1717
index = empty_index()
1818
response = index.add_documents(small_movies)
19-
assert isinstance(response, dict)
20-
assert 'taskUid' in response
21-
update = index.wait_for_task(response['taskUid'])
19+
assert isinstance(response, TaskInfo)
20+
assert response.task_uid != None
21+
update = index.wait_for_task(response.task_uid)
2222
assert index.get_primary_key() == 'id'
2323
assert update.status == 'succeeded'
2424

@@ -38,8 +38,8 @@ def test_add_documents_in_batches(
3838
assert ceil(len(small_movies) / batch_size) == len(response)
3939

4040
for r in response:
41-
assert 'taskUid' in r
42-
update = index.wait_for_task(r['taskUid'])
41+
assert r.task_uid != None
42+
update = index.wait_for_task(r.task_uid)
4343
assert update.status == 'succeeded'
4444

4545
assert index.get_primary_key() == expected_primary_key
@@ -91,13 +91,13 @@ def test_update_documents(index_with_documents, small_movies):
9191
response = index.get_documents()
9292
response.results[0]['title'] = 'Some title'
9393
update = index.update_documents([response.results[0]])
94-
assert isinstance(update, dict)
95-
assert 'taskUid' in update
96-
index.wait_for_task(update['taskUid'])
94+
assert isinstance(update, TaskInfo)
95+
assert update.task_uid != None
96+
index.wait_for_task(update.task_uid)
9797
response = index.get_documents()
9898
assert response.results[0]['title'] == 'Some title'
9999
update = index.update_documents(small_movies)
100-
index.wait_for_task(update['taskUid'])
100+
index.wait_for_task(update.task_uid)
101101
response = index.get_documents()
102102
assert response.results[0]['title'] != 'Some title'
103103

@@ -117,8 +117,8 @@ def test_update_documents_in_batches(
117117
assert ceil(len(small_movies) / batch_size) == len(response)
118118

119119
for r in response:
120-
assert 'taskUid' in r
121-
update = index.wait_for_task(r['taskUid'])
120+
assert r.task_uid != None
121+
update = index.wait_for_task(r.task_uid)
122122
assert update.status == 'succeeded'
123123

124124
assert index.get_primary_key() == expected_primary_key
@@ -127,9 +127,9 @@ def test_delete_document(index_with_documents):
127127
"""Tests deleting a single document."""
128128
index = index_with_documents()
129129
response = index.delete_document('500682')
130-
assert isinstance(response, dict)
131-
assert 'taskUid' in response
132-
index.wait_for_task(response['taskUid'])
130+
assert isinstance(response, TaskInfo)
131+
assert response.task_uid != None
132+
index.wait_for_task(response.task_uid)
133133
with pytest.raises(Exception):
134134
index.get_document('500682')
135135

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from meilisearch.models import IndexStatsResults
1+
from meilisearch.models import IndexStats
22

33
def test_get_stats(empty_index):
44
"""Tests getting stats of an index."""
55
response = empty_index().get_stats()
6-
assert isinstance(response, IndexStatsResults)
6+
assert isinstance(response, IndexStats)
77
assert response.number_of_documents == 0

tests/index/test_index_task_meilisearch.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
from tests import common
5-
from meilisearch.models import TaskInfo, TaskResults
5+
from meilisearch.models import Task, TaskResults
66

77
def test_get_tasks_default(index_with_documents):
88
"""Tests getting the tasks list of an empty index."""
@@ -17,8 +17,8 @@ def test_get_tasks(empty_index, small_movies):
1717
current_tasks = index.get_tasks()
1818
pre_count = len(current_tasks.results)
1919
response = index.add_documents(small_movies)
20-
assert 'taskUid' in response
21-
index.wait_for_task(response['taskUid'])
20+
assert response.task_uid != None
21+
index.wait_for_task(response.task_uid)
2222
tasks = index.get_tasks()
2323
assert len(tasks.results) == pre_count + 1
2424

@@ -49,8 +49,9 @@ def test_get_task(client):
4949
client.wait_for_task(task['taskUid'])
5050
index = client.get_index(uid=common.INDEX_UID)
5151
task = index.get_task(task['taskUid'])
52-
assert isinstance(task, TaskInfo)
52+
assert isinstance(task, Task)
5353
assert task.uid != None
54+
assert task.index_uid != None
5455
assert task.status != None
5556
assert task.type != None
5657
assert task.duration != None

tests/index/test_index_wait_for_task.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from datetime import datetime
44
import pytest
55
from meilisearch.errors import MeiliSearchTimeoutError
6-
from meilisearch.models import TaskInfo
6+
from meilisearch.models import Task
77

88
def test_wait_for_task_default(index_with_documents):
99
"""Tests waiting for an update with default parameters."""
1010
index = index_with_documents()
1111
response = index.add_documents([{'id': 1, 'title': 'Le Petit Prince'}])
12-
assert 'taskUid' in response
13-
update = index.wait_for_task(response['taskUid'])
14-
assert isinstance(update, TaskInfo)
12+
assert response.task_uid != None
13+
update = index.wait_for_task(response.task_uid)
14+
assert isinstance(update, Task)
1515
assert update.status != None
1616
assert update.status not in ('enqueued', 'processing')
1717

@@ -24,15 +24,15 @@ def test_wait_for_task_interval_custom(index_with_documents, small_movies):
2424
"""Tests call to wait for an update with custom interval."""
2525
index = index_with_documents()
2626
response = index.add_documents(small_movies)
27-
assert 'taskUid' in response
27+
assert response.task_uid != None
2828
start_time = datetime.now()
2929
wait_update = index.wait_for_task(
30-
response['taskUid'],
30+
response.task_uid,
3131
interval_in_ms=1000,
3232
timeout_in_ms=6000
3333
)
3434
time_delta = datetime.now() - start_time
35-
assert isinstance(wait_update, TaskInfo)
35+
assert isinstance(wait_update, Task)
3636
assert wait_update.status != None
3737
assert wait_update.status != 'enqueued'
3838
assert wait_update.status != 'processing'
@@ -42,13 +42,13 @@ def test_wait_for_task_interval_zero(index_with_documents, small_movies):
4242
"""Tests call to wait for an update with custom interval."""
4343
index = index_with_documents()
4444
response = index.add_documents(small_movies)
45-
assert 'taskUid' in response
45+
assert response.task_uid != None
4646
wait_update = index.wait_for_task(
47-
response['taskUid'],
47+
response.task_uid,
4848
interval_in_ms=0,
4949
timeout_in_ms=6000
5050
)
51-
assert isinstance(wait_update, TaskInfo)
51+
assert isinstance(wait_update, Task)
5252
assert wait_update.status != None
5353
assert wait_update.status != 'enqueued'
5454
assert wait_update.status != 'processing'

tests/settings/test_settings_displayed_attributes_meilisearch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_get_displayed_attributes(empty_index, small_movies):
1010
assert isinstance(response, list)
1111
assert response == ['*']
1212
response = index.add_documents(small_movies)
13-
index.wait_for_task(response['taskUid'])
13+
index.wait_for_task(response.task_uid)
1414
get_attributes = index.get_displayed_attributes()
1515
assert get_attributes == ['*']
1616

tests/settings/test_settings_searchable_attributes_meilisearch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_get_searchable_attributes(empty_index, small_movies):
99
assert isinstance(response, list)
1010
assert response == ['*']
1111
response = index.add_documents(small_movies, primary_key='id')
12-
index.wait_for_task(response['taskUid'])
12+
index.wait_for_task(response.task_uid)
1313
get_attributes = index.get_searchable_attributes()
1414
assert get_attributes == ['*']
1515

0 commit comments

Comments
 (0)