Skip to content

Changes related to the next Meilisearch release (v0.28.0) #472

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

Merged
merged 71 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
1124b13
Update README.md
meili-bot Jun 9, 2022
97f33e4
Add search changes
curquiza Jun 14, 2022
e4d8b8e
Update HTTP methods
curquiza Jun 14, 2022
527d584
Merge pull request #474 from meilisearch/search-changes
alallema Jun 15, 2022
6bfee07
Merge pull request #475 from meilisearch/http-methods
alallema Jun 15, 2022
f3398c0
Apply tasks changes
alallema Jun 15, 2022
426ff12
Apply indexes changes
alallema Jun 15, 2022
ce31c76
Apply dumps changes
alallema Jun 15, 2022
9d9ccc5
Apply documents changes
alallema Jun 16, 2022
b403b97
Remove code sample
alallema Jun 16, 2022
6c36062
Remove useless and unsure test
alallema Jun 16, 2022
a572124
Wrap indexes in result
alallema Jun 16, 2022
3c0374d
Merge pull request #479 from meilisearch/dumps_changes
alallema Jun 16, 2022
67d134b
Fix useless test
alallema Jun 16, 2022
173f054
Update tests/client/test_client_task_meilisearch.py
alallema Jun 16, 2022
39af8fb
Add possibility for multiples indexes in task
alallema Jun 16, 2022
e5e0634
Merge branch 'bump-meilisearch-v0.28.0' into task_changes
alallema Jun 20, 2022
7743b50
Fix get_task return
alallema Jun 20, 2022
5f212b7
Add ressource query parameter to task
alallema Jun 21, 2022
18f75f8
modify naming for resource query
alallema Jun 21, 2022
e3bbec6
Add tests for ressourceQuery
alallema Jun 21, 2022
3c038a0
Fix naming
alallema Jun 21, 2022
06174e8
Fix naming
alallema Jun 21, 2022
d053e9f
Add resource query to indexes
alallema Jun 21, 2022
4220e48
Change get_task parameter name
alallema Jun 21, 2022
265091c
Change get_task parameter name
alallema Jun 21, 2022
805d1e4
Change get_task parameter name
alallema Jun 21, 2022
dde7c70
Change get_task parameter name
alallema Jun 22, 2022
ebb2fb3
Change get_indexes parameter name
alallema Jun 22, 2022
6f741dd
Change get_documents parameter name
alallema Jun 22, 2022
3019eb7
Change get_task prototype
alallema Jun 22, 2022
6df98d5
Change get_documents parameter name
alallema Jun 22, 2022
3396ad5
Change get_indexes parameter name
alallema Jun 22, 2022
c74fa86
Fix if no parameters
alallema Jun 22, 2022
a5ca389
Adapt return comment for get_documents
alallema Jun 22, 2022
2a3cebe
Adapt return comment for get_indexes
alallema Jun 22, 2022
0d6a002
Change condition in task
alallema Jun 23, 2022
82413df
Merge pull request #476 from meilisearch/task_changes
alallema Jun 23, 2022
5cd9b36
Merge pull request #481 from meilisearch/documents_changes
alallema Jun 23, 2022
ccd86d7
Fix return of get_raw_indexes
alallema Jun 23, 2022
d0d0120
Merge pull request #478 from meilisearch/index_changes
alallema Jun 23, 2022
c1b640a
Apply key changes
alallema Jun 15, 2022
ece7430
Modify param and return information for get_key(s)
alallema Jun 23, 2022
80947ac
Fix tests
alallema Jun 23, 2022
a254d6a
Add uid to the generateTenantToken method
alallema Jun 23, 2022
8015bf8
Merge pull request #477 from meilisearch/key_changes
alallema Jun 24, 2022
93af29a
Replace uid by api_key_uid
alallema Jun 27, 2022
2b38a30
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
523c661
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
19c90ca
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
28faa65
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
7e8b4b7
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
36b90b7
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
744dac7
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
3866553
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
3b04b76
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
20d3106
Update tests/client/test_client_tenant_token.py
alallema Jun 27, 2022
27083d1
Merge pull request #484 from meilisearch/generate_token
alallema Jun 27, 2022
cd973eb
Fix get_tasks index
alallema Jun 27, 2022
36e1f8d
Add keys parameter in missing routes v0.28.x
alallema Jun 30, 2022
d77346a
Merge pull request #489 from meilisearch/add_keys_parameter_in_missin…
alallema Jun 30, 2022
f788884
Adding pagination parameters to key route
alallema Jul 5, 2022
65dcc18
Add pagination parameters to get_document
alallema Jul 5, 2022
8672f4d
Add fields list and string in get_document parameters
alallema Jul 5, 2022
4c9eed5
Revert fix test
alallema Jul 5, 2022
ad82f85
Fix test task
alallema Jul 5, 2022
e390899
Merge pull request #491 from meilisearch/key_changes
alallema Jul 6, 2022
f1cfc58
Improve parameters for get_document method
alallema Jul 6, 2022
ed982e2
Add parameters definition
alallema Jul 7, 2022
e677b44
Merge pull request #492 from meilisearch/documents_changes
alallema Jul 7, 2022
7557aee
Merge branch 'main' into bump-meilisearch-v0.28.0
alallema Jul 11, 2022
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
9 changes: 4 additions & 5 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ search_parameter_guide_highlight_tag_1: |-
})
search_parameter_guide_matches_1: |-
client.index('movies').search('winter feast', {
'matches': 'true'
'showMatchesPosition': 'true'
})
settings_guide_synonyms_1: |-
client.index('tops').update_settings({
Expand Down Expand Up @@ -491,16 +491,14 @@ faceted_search_filter_1: |-
})
faceted_search_facets_distribution_1: |-
client.index('movies').search('Batman', {
'facetsDistribution': ['genres']
'facets': ['genres']
})
faceted_search_walkthrough_filter_1: |-
client.index('movies').search('thriller', {
'filter': [['genres = Horror', 'genres = Mystery'], 'director = "Jordan Peele"']
})
post_dump_1: |-
client.create_dump()
get_dump_status_1: |-
client.get_dump_status('20201101-110357260')
phrase_search_1: |-
client.index('movies').search('"african american" horror')
sorting_guide_update_sortable_attributes_1: |-
Expand Down Expand Up @@ -591,14 +589,15 @@ authorization_header_1: |-
client = Client('http://127.0.0.1:7700', 'masterKey')
client.get_keys()
tenant_token_guide_generate_sdk_1: |-
uid = '85c3c2f9-bdd6-41f1-abd8-11fcf80e0f76';
api_key = 'B5KdX2MY2jV6EXfUs6scSfmC...'
expires_at = datetime(2025, 12, 20)
search_rules = {
'patient_medical_records': {
'filter': 'user_id = 1'
}
}
token = client.generate_tenant_token(search_rules=search_rules, api_key=api_key, expires_at=expires_at)
token = client.generate_tenant_token(api_key_uid=uid, search_rules=search_rules, api_key=api_key, expires_at=expires_at)
tenant_token_guide_search_sdk_1: |-
front_end_client = Client('http://127.0.0.1:7700', token)
front_end_client.index('patient_medical_records').search('blood test')
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ index.search(
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"estimatedTotalHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
```

## 🤖 Compatibility with Meilisearch

This package only guarantees the compatibility with the [version v0.27.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.27.0).
This package only guarantees the compatibility with the [version v0.28.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.28.0).

## 💡 Learn More

Expand Down
137 changes: 81 additions & 56 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re
import base64
import hashlib
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 +80,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

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 Expand Up @@ -221,13 +241,13 @@ def is_healthy(self) -> bool:
return False
return True

def get_key(self, key: str) -> Dict[str, Any]:
def get_key(self, key_or_uid: str) -> Dict[str, Any]:
"""Gets information about a specific API key.

Parameters
----------
key:
The key for which to retrieve the information.
key_or_uid:
The key or the uid for which to retrieve the information.

Returns
-------
Expand All @@ -240,23 +260,32 @@ def get_key(self, key: str) -> Dict[str, Any]:
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(f'{self.config.paths.keys}/{key}')
return self.http.get(f'{self.config.paths.keys}/{key_or_uid}')

def get_keys(self) -> Dict[str, Any]:
def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Gets the Meilisearch API keys.

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

Returns
-------
keys:
API keys.
Dictionary with limit, offset, total and results a list of dictionaries containing the key information.
https://docs.meilisearch.com/reference/api/keys.html#get-keys

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

def create_key(
self,
Expand Down Expand Up @@ -288,7 +317,7 @@ def create_key(

def update_key(
self,
key: str,
key_or_uid: str,
options: Dict[str, Any]
) -> Dict[str, Any]:
"""Update an API key.
Expand All @@ -297,7 +326,7 @@ def update_key(

----------
key:
The key for which to update the information.
The key or the uid of the key for which to update the information.
options:
The information to use in creating the key (ex: { 'description': 'Search Key', 'expiresAt': '22-01-01' }). Note that if an
expires_at value is included it should be in UTC time.
Expand All @@ -313,16 +342,16 @@ def update_key(
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
"""
url = f'{self.config.paths.keys}/{key}'
url = f'{self.config.paths.keys}/{key_or_uid}'
return self.http.patch(url, options)

def delete_key(self, key: str) -> Dict[str, int]:
def delete_key(self, key_or_uid: str) -> Dict[str, int]:
"""Deletes an API key.

Parameters
----------
key:
The key to delete.
The key or the uid of the key to delete.

Returns
-------
Expand All @@ -335,7 +364,7 @@ def delete_key(self, key: str) -> Dict[str, int]:
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.delete(f'{self.config.paths.keys}/{key}')
return self.http.delete(f'{self.config.paths.keys}/{key_or_uid}')

def get_version(self) -> Dict[str, str]:
"""Get version Meilisearch
Expand Down Expand Up @@ -383,43 +412,26 @@ def create_dump(self) -> Dict[str, str]:
"""
return self.http.post(self.config.paths.dumps)

def get_dump_status(self, uid: str) -> Dict[str, str]:
"""Retrieve the status of a Meilisearch dump creation.
def get_tasks(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Dict[str, Any]]]:
"""Get all tasks.

Parameters
----------
uid:
UID of the dump.

Returns
-------
Dump status:
Information about the dump status.
https://docs.meilisearch.com/reference/api/dump.html#get-dump-status

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.dumps + '/' + str(uid) + '/status'
)

def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
"""Get all tasks.
parameters (optional):
parameters accepted by the get tasks route: https://docs.meilisearch.com/reference/api/tasks.html#get-all-tasks.
`indexUid` should be set as a List.

Returns
-------
task:
Dictionary containing a list of all enqueued, processing, succeeded or failed tasks.
Dictionary with limit, from, next and results containing a list of all enqueued, processing, succeeded or failed tasks.

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 get_tasks(self.config)
return get_tasks(self.config, parameters=parameters)

def get_task(self, uid: int) -> Dict[str, Any]:
"""Get one task.
Expand Down Expand Up @@ -471,6 +483,7 @@ def wait_for_task(

def generate_tenant_token(
self,
api_key_uid: str,
search_rules: Union[Dict[str, Any], List[str]],
*,
expires_at: Optional[datetime.datetime] = None,
Expand All @@ -480,6 +493,8 @@ def generate_tenant_token(

Parameters
----------
api_key_uid:
The uid of the API key used as issuer of the token.
search_rules:
A Dictionary or list of string which contains the rules to be enforced at search time for all or specific
accessible indexes for the signing API Key.
Expand All @@ -499,6 +514,8 @@ def generate_tenant_token(
# Validate all fields
if api_key == '' or api_key is None and self.config.api_key is None:
raise Exception('An api key is required in the client or should be passed as an argument.')
if api_key_uid == '' or api_key_uid is None or self._valid_uuid(api_key_uid) is False:
raise Exception('An uid is required and must comply to the uuid4 format.')
if not search_rules or search_rules == ['']:
raise Exception('The search_rules field is mandatory and should be defined.')
if expires_at and expires_at < datetime.datetime.utcnow():
Expand All @@ -514,7 +531,7 @@ def generate_tenant_token(

# Add the required fields to the payload
payload = {
'apiKeyPrefix': api_key[0:8],
'apiKeyUid': api_key_uid,
'searchRules': search_rules,
'exp': int(datetime.datetime.timestamp(expires_at)) if expires_at is not None else None
}
Expand All @@ -540,3 +557,11 @@ def _base64url_encode(
data: bytes
) -> str:
return base64.urlsafe_b64encode(data).decode('utf-8').replace('=','')

@staticmethod
def _valid_uuid(
uuid: str
) -> bool:
uuid4hex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}', re.I)
match = uuid4hex.match(uuid)
return bool(match)
Loading