Skip to content

Commit 0206ec6

Browse files
authored
Add method to use faceting sub routes (#90)
1 parent 6cc1a39 commit 0206ec6

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Paths():
2121
stop_words = 'stop-words'
2222
synonyms = 'synonyms'
2323
accept_new_fields = 'accept-new-fields'
24+
attributes_for_faceting = 'attributes-for-faceting'
2425

2526
def __init__(self, url, apikey=None):
2627
"""

meilisearch/index.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,54 @@ def update_accept_new_fields(self, body):
814814
body
815815
)
816816

817+
# ATTRIBUTES FOR FACETING SUB-ROUTES
818+
819+
def get_attributes_for_faceting(self):
820+
"""
821+
Get attributes for faceting of an index
822+
823+
Returns
824+
----------
825+
settings: `list`
826+
List containing the attributes for faceting of the index
827+
"""
828+
return self.http.get(
829+
self.__settings_url_for(self.config.paths.attributes_for_faceting)
830+
)
831+
832+
def update_attributes_for_faceting(self, body):
833+
"""
834+
Update attributes for faceting of an index
835+
836+
Parameters
837+
----------
838+
body: `list`
839+
List containing the attributes for faceting
840+
841+
Returns
842+
----------
843+
update: `dict`
844+
Dictionnary containing an update id to track the action:
845+
https://docs.meilisearch.com/references/updates.html#get-an-update-status
846+
"""
847+
return self.http.post(
848+
self.__settings_url_for(self.config.paths.attributes_for_faceting),
849+
body
850+
)
851+
852+
def reset_attributes_for_faceting(self):
853+
"""Reset attributes for faceting of an index to default values
854+
855+
Returns
856+
----------
857+
update: `dict`
858+
Dictionnary containing an update id to track the action:
859+
https://docs.meilisearch.com/references/updates.html#get-an-update-status
860+
"""
861+
return self.http.delete(
862+
self.__settings_url_for(self.config.paths.attributes_for_faceting),
863+
)
864+
817865
def __settings_url_for(self, sub_route):
818866
return '{}/{}/{}/{}'.format(
819867
self.config.paths.index,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
import meilisearch
3+
from meilisearch.tests import BASE_URL, MASTER_KEY
4+
5+
class TestAttributesForFaceting:
6+
7+
""" TESTS: attributesForFaceting setting """
8+
9+
client = meilisearch.Client(BASE_URL, MASTER_KEY)
10+
index = None
11+
attributes_for_faceting = ['title', 'release_date']
12+
dataset_file = None
13+
dataset_json = None
14+
15+
def setup_class(self):
16+
self.index = self.client.create_index(uid='indexUID')
17+
self.dataset_file = open('./datasets/small_movies.json', 'r')
18+
self.dataset_json = json.loads(self.dataset_file.read())
19+
self.dataset_file.close()
20+
21+
def teardown_class(self):
22+
self.index.delete()
23+
24+
def test_get_attributes_for_faceting(self):
25+
""" Tests getting the attributes for faceting """
26+
response = self.index.get_attributes_for_faceting()
27+
assert isinstance(response, object)
28+
assert response == []
29+
30+
def test_update_attributes_for_faceting(self):
31+
"""Tests updating the attributes for faceting"""
32+
response = self.index.update_attributes_for_faceting(self.attributes_for_faceting)
33+
self.index.wait_for_pending_update(response['updateId'])
34+
get_attributes_new = self.index.get_attributes_for_faceting()
35+
assert len(get_attributes_new) == len(self.attributes_for_faceting)
36+
get_attributes = self.index.get_attributes_for_faceting()
37+
for attribute in self.attributes_for_faceting:
38+
assert attribute in get_attributes
39+
40+
def test_reset_attributes_for_faceting(self):
41+
"""Tests the reset of attributes for faceting to default values (in dataset)"""
42+
response = self.index.reset_attributes_for_faceting()
43+
assert isinstance(response, object)
44+
assert 'updateId' in response
45+
self.index.wait_for_pending_update(response['updateId'])
46+
response = self.index.get_attributes_for_faceting()
47+
assert isinstance(response, object)
48+
assert response == []

0 commit comments

Comments
 (0)