Skip to content

Changes related to the next MeiliSearch release (v0.22.0) #318

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 9 commits into from
Sep 13, 2021
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
54 changes: 48 additions & 6 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ update_settings_1: |-
'typo',
'proximity',
'attribute',
'sort',
'exactness',
'desc(release_date)',
'desc(rank)'
'release_date:desc',
'rank:desc'
],
'distinctAttribute': 'movie_id',
'searchableAttributes': [
Expand All @@ -70,6 +71,10 @@ update_settings_1: |-
'genre',
'release_date'
],
'sortableAttributes': [
'title',
'release_date'
],
'stopWords': [
'the',
'a',
Expand Down Expand Up @@ -107,9 +112,10 @@ update_ranking_rules_1: |-
'typo',
'proximity',
'attribute',
'sort',
'exactness',
'asc(release_date)',
'desc(rank)'
'release_date:asc',
'rank:desc'
])
reset_ranking_rules_1: |-
client.index('movies').reset_ranking_rules()
Expand Down Expand Up @@ -149,6 +155,15 @@ update_displayed_attributes_1: |-
])
reset_displayed_attributes_1: |-
client.index('movies').reset_displayed_attributes()
get_sortable_attributes_1: |-
client.index('books').get_sortable_attributes()
update_sortable_attributes_1: |-
client.index('books').update_sortable_attributes([
'price',
'author'
])
reset_sortable_attributes_1: |-
client.index('books').reset_sortable_attributes()
get_index_stats_1: |-
client.index('movies').get_stats()
get_indexes_stats_1: |-
Expand Down Expand Up @@ -247,9 +262,10 @@ settings_guide_ranking_rules_1: |-
'typo',
'proximity',
'attribute',
'sort',
'exactness',
'asc(release_date)',
'desc(rank)'
'release_date:asc',
'rank:desc'
]
})
settings_guide_distinct_1: |-
Expand Down Expand Up @@ -350,3 +366,29 @@ 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: |-
client.index('books').update_sortable_attributes([
'author',
'price'
])
sorting_guide_update_ranking_rules_1: |-
client.index('books').update_ranking_rules([
'words',
'sort',
'typo',
'proximity',
'attribute',
'exactness'
])
sorting_guide_sort_parameter_1: |-
client.index('books').search('science fiction', {
'sort': ['price:asc']
})
sorting_guide_sort_parameter_2: |-
client.index('books').search('butler', {
'sort': ['author:desc']
})
search_parameter_guide_sort_1: |-
client.index('books').search('science fiction', {
'sort': ['price:asc']
})
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ JSON output:

## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.21.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.21.0).
This package only guarantees the compatibility with the [version v0.22.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.22.0).

## 💡 Learn More

Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Paths():
synonyms = 'synonyms'
accept_new_fields = 'accept-new-fields'
filterable_attributes = 'filterable-attributes'
sortable_attributes = 'sortable-attributes'
dumps = 'dumps'

def __init__(
Expand Down
64 changes: 64 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,70 @@ def reset_filterable_attributes(self) -> Dict[str, int]:
self.__settings_url_for(self.config.paths.filterable_attributes),
)


# SORTABLE ATTRIBUTES SUB-ROUTES

def get_sortable_attributes(self) -> List[str]:
"""
Get sortable attributes of the index.

Returns
-------
settings:
List containing the sortable attributes of the index

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.__settings_url_for(self.config.paths.sortable_attributes)
)

def update_sortable_attributes(self, body: List[str]) -> Dict[str, int]:
"""
Update sortable attributes of the index.

Parameters
----------
body:
List containing the sortable attributes.

Returns
-------
update:
Dictionary containing an update id to track the action:
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-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.post(
self.__settings_url_for(self.config.paths.sortable_attributes),
body
)

def reset_sortable_attributes(self) -> Dict[str, int]:
"""Reset sortable attributes of the index to default values.

Returns
-------
update:
Dictionary containing an update id to track the action:
https://docs.meilisearch.com/reference/api/updates.html#get-an-update-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.delete(
self.__settings_url_for(self.config.paths.sortable_attributes),
)

@staticmethod
def _batch(
documents: List[Dict[str, Any]], batch_size: int
Expand Down
78 changes: 78 additions & 0 deletions meilisearch/tests/index/test_index_search_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,84 @@ def test_custom_search_params_with_many_params(index_with_documents):
assert 'release_date' not in response['hits'][0]
assert response['hits'][0]['title'] == 'Avengers: Infinity War'

def test_custom_search_params_with_sort_string(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules([
'words',
'typo',
'sort',
'proximity',
'attribute',
'exactness'
])
index.wait_for_pending_update(response['updateId'])
update = index.update_sortable_attributes(['title'])
index.wait_for_pending_update(update['updateId'])
response = index.search(
'world',
{
'sort': ['title:asc']
}
)
assert isinstance(response, dict)
assert len(response['hits']) == 12
assert 'facetsDistribution' not in response
assert 'exhaustiveFacetsCount' not in response
assert response['hits'][0]['title'] == 'Alita: Battle Angel'
assert response['hits'][1]['title'] == 'Aquaman'

def test_custom_search_params_with_sort_int(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules([
'words',
'typo',
'sort',
'proximity',
'attribute',
'exactness'
])
index.wait_for_pending_update(response['updateId'])
update = index.update_sortable_attributes(['release_date'])
index.wait_for_pending_update(update['updateId'])
response = index.search(
'world',
{
'sort': ['release_date:asc']
}
)
assert isinstance(response, dict)
assert len(response['hits']) == 12
assert 'facetsDistribution' not in response
assert 'exhaustiveFacetsCount' not in response
assert response['hits'][0]['title'] == 'Avengers: Infinity War'
assert response['hits'][1]['title'] == 'Redcon-1'

def test_custom_search_params_with_multiple_sort(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules([
'words',
'typo',
'sort',
'proximity',
'attribute',
'exactness'
])
index.wait_for_pending_update(response['updateId'])
update = index.update_sortable_attributes(['title', 'release_date'])
index.wait_for_pending_update(update['updateId'])
response = index.search(
'world',
{
'sort': ['title:asc', 'release_date:asc']
}
)
assert isinstance(response, dict)
assert len(response['hits']) == 12
assert 'facetsDistribution' not in response
assert 'exhaustiveFacetsCount' not in response
assert response['hits'][0]['title'] == 'Alita: Battle Angel'
assert response['hits'][1]['title'] == 'Aquaman'

def test_phrase_search(index_with_documents):
response = index_with_documents().search('coco "dumbo"')
assert isinstance(response, dict)
Expand Down
1 change: 1 addition & 0 deletions meilisearch/tests/settings/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'typo',
'proximity',
'attribute',
'sort',
'exactness'
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'typo',
'proximity',
'attribute',
'sort',
'exactness'
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# pylint: disable=invalid-name

SORTABLE_ATTRIBUTES = ['title', 'release_date']

def test_get_sortable_attributes(empty_index):
"""Tests getting the sortable attributes."""
response = empty_index().get_sortable_attributes()
assert isinstance(response, list)
assert response == []

def test_update_sortable_attributes(empty_index):
"""Tests updating the sortable attributes."""
index = empty_index()
response = index.update_sortable_attributes(SORTABLE_ATTRIBUTES)
index.wait_for_pending_update(response['updateId'])
get_attributes = index.get_sortable_attributes()
assert len(get_attributes) == len(SORTABLE_ATTRIBUTES)
for attribute in SORTABLE_ATTRIBUTES:
assert attribute in get_attributes

def test_update_sortable_attributes_to_none(empty_index):
"""Tests updating the sortable attributes at null."""
index = empty_index()
# Update the settings first
response = index.update_sortable_attributes(SORTABLE_ATTRIBUTES)
update = index.wait_for_pending_update(response['updateId'])
assert update['status'] == 'processed'
# Check the settings have been correctly updated
get_attributes = index.get_sortable_attributes()
for attribute in SORTABLE_ATTRIBUTES:
assert attribute in get_attributes
# Launch test to update at null the setting
response = index.update_sortable_attributes(None)
index.wait_for_pending_update(response['updateId'])
response = index.get_sortable_attributes()
assert response == []

def test_reset_sortable_attributes(empty_index):
"""Tests resetting the sortable attributes setting to its default value"""
index = empty_index()
# Update the settings first
response = index.update_sortable_attributes(SORTABLE_ATTRIBUTES)
update = index.wait_for_pending_update(response['updateId'])
assert update['status'] == 'processed'
# Check the settings have been correctly updated
get_attributes = index.get_sortable_attributes()
assert len(get_attributes) == len(SORTABLE_ATTRIBUTES)
for attribute in SORTABLE_ATTRIBUTES:
assert attribute in get_attributes
# Check the reset of the settings
response = index.reset_sortable_attributes()
assert isinstance(response, dict)
assert 'updateId' in response
index.wait_for_pending_update(response['updateId'])
response = index.get_sortable_attributes()
assert isinstance(response, list)
assert response == []