diff --git a/datasets/nested_movies.json b/datasets/nested_movies.json new file mode 100644 index 00000000..68a97335 --- /dev/null +++ b/datasets/nested_movies.json @@ -0,0 +1,54 @@ +[ + { + "id": 1, + "title": "Pride and Prejudice", + "info": { + "comment": "A great book", + "reviewNb": 50 + } + }, + { + "id": 2, + "title": "Le Petit Prince", + "info": { + "comment": "A french book", + "reviewNb": 600 + } + }, + { + "id": 3, + "title": "Le Rouge et le Noir", + "info": { + "comment": "Another french book", + "reviewNb": 700 + } + }, + { + "id": 4, + "title": "Alice In Wonderland", + "comment": "A weird book", + "info": { + "comment": "A weird book", + "reviewNb": 800 + } + }, + { + "id": 5, + "title": "The Hobbit", + "info": { + "comment": "An awesome book", + "reviewNb": 900 + } + }, + { + "id": 6, + "title": "Harry Potter and the Half-Blood Prince", + "info": { + "comment": "The best book", + "reviewNb": 1000 + } + }, + { "id": 7, + "title": "The Hitchhiker's Guide to the Galaxy" + } +] diff --git a/tests/conftest.py b/tests/conftest.py index 43124544..37f9dbb8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -67,6 +67,14 @@ def songs_ndjson(): with open('./datasets/songs.ndjson', 'r', encoding='utf-8') as song_ndjson_file: return song_ndjson_file.read().encode('utf-8') +@fixture(scope='session') +def nested_movies(): + """ + Runs once per session. Provides the content of nested_movies.json. + """ + with open('./datasets/nested_movies.json', 'r', encoding='utf-8') as nested_movie_file: + yield json.loads(nested_movie_file.read()) + @fixture(scope='function') def empty_index(client, index_uid: Optional[str] = None): index_uid = index_uid if index_uid else common.INDEX_UID diff --git a/tests/index/test_index_search_meilisearch.py b/tests/index/test_index_search_meilisearch.py index 9886c908..7cbdafd2 100644 --- a/tests/index/test_index_search_meilisearch.py +++ b/tests/index/test_index_search_meilisearch.py @@ -350,3 +350,37 @@ def test_phrase_search(index_with_documents): assert 'release_date' in response['hits'][0] assert response['hits'][0]['title'] == 'Dumbo' assert '_formatted' not in response['hits'][0] + +def test_basic_search_on_nested_documents(index_with_documents, nested_movies): + """Tests search with an simple query on nested fields.""" + response = index_with_documents('nested_fields_index', nested_movies).search('An awesome') + assert isinstance(response, dict) + assert response['hits'][0]['id'] == 5 + assert len(response['hits']) == 1 + +def test_search_on_nested_documents_with_searchable_attributes(index_with_documents, nested_movies): + """Tests search on nested fields with searchable attribute.""" + index = index_with_documents('nested_fields_index', nested_movies) + response_searchable_attributes = index.update_searchable_attributes(['title', 'info.comment']) + index.wait_for_task(response_searchable_attributes['uid']) + response = index.search('An awesome') + assert isinstance(response, dict) + assert response['hits'][0]['id'] == 5 + assert len(response['hits']) == 1 + +def test_search_on_nested_documents_with_sortable_attributes(index_with_documents, nested_movies): + """Tests search on nested fields with searchable attribute and sortable attributes.""" + index = index_with_documents('nested_fields_index', nested_movies) + response_settings = index.update_settings({ + 'searchableAttributes': ['title', 'info.comment'], + 'sortableAttributes': ['info.reviewNb'], + }) + index.wait_for_task(response_settings['uid']) + response = index.search( + '', + { + 'sort': ['info.reviewNb:desc'] + } + ) + assert isinstance(response, dict) + assert response['hits'][0]['id'] == 6