Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure nested field support #444

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
Apr 22, 2022
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: 54 additions & 0 deletions datasets/nested_movies.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/index/test_index_search_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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