Skip to content

Add method to use faceting sub routes #90

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 1 commit into from
Jun 4, 2020
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
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Paths():
stop_words = 'stop-words'
synonyms = 'synonyms'
accept_new_fields = 'accept-new-fields'
attributes_for_faceting = 'attributes-for-faceting'

def __init__(self, url, apikey=None):
"""
Expand Down
48 changes: 48 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,54 @@ def update_accept_new_fields(self, body):
body
)

# ATTRIBUTES FOR FACETING SUB-ROUTES

def get_attributes_for_faceting(self):
"""
Get attributes for faceting of an index

Returns
----------
settings: `list`
List containing the attributes for faceting of the index
"""
return self.http.get(
self.__settings_url_for(self.config.paths.attributes_for_faceting)
)

def update_attributes_for_faceting(self, body):
"""
Update attributes for faceting of an index

Parameters
----------
body: `list`
List containing the attributes for faceting

Returns
----------
update: `dict`
Dictionnary containing an update id to track the action:
https://docs.meilisearch.com/references/updates.html#get-an-update-status
"""
return self.http.post(
self.__settings_url_for(self.config.paths.attributes_for_faceting),
body
)

def reset_attributes_for_faceting(self):
"""Reset attributes for faceting of an index to default values

Returns
----------
update: `dict`
Dictionnary containing an update id to track the action:
https://docs.meilisearch.com/references/updates.html#get-an-update-status
"""
return self.http.delete(
self.__settings_url_for(self.config.paths.attributes_for_faceting),
)

def __settings_url_for(self, sub_route):
return '{}/{}/{}/{}'.format(
self.config.paths.index,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
import meilisearch
from meilisearch.tests import BASE_URL, MASTER_KEY

class TestAttributesForFaceting:

""" TESTS: attributesForFaceting setting """

client = meilisearch.Client(BASE_URL, MASTER_KEY)
index = None
attributes_for_faceting = ['title', 'release_date']
dataset_file = None
dataset_json = None

def setup_class(self):
self.index = self.client.create_index(uid='indexUID')
self.dataset_file = open('./datasets/small_movies.json', 'r')
self.dataset_json = json.loads(self.dataset_file.read())
self.dataset_file.close()

def teardown_class(self):
self.index.delete()

def test_get_attributes_for_faceting(self):
""" Tests getting the attributes for faceting """
response = self.index.get_attributes_for_faceting()
assert isinstance(response, object)
assert response == []

def test_update_attributes_for_faceting(self):
"""Tests updating the attributes for faceting"""
response = self.index.update_attributes_for_faceting(self.attributes_for_faceting)
self.index.wait_for_pending_update(response['updateId'])
get_attributes_new = self.index.get_attributes_for_faceting()
assert len(get_attributes_new) == len(self.attributes_for_faceting)
get_attributes = self.index.get_attributes_for_faceting()
for attribute in self.attributes_for_faceting:
assert attribute in get_attributes

def test_reset_attributes_for_faceting(self):
"""Tests the reset of attributes for faceting to default values (in dataset)"""
response = self.index.reset_attributes_for_faceting()
assert isinstance(response, object)
assert 'updateId' in response
self.index.wait_for_pending_update(response['updateId'])
response = self.index.get_attributes_for_faceting()
assert isinstance(response, object)
assert response == []