Skip to content

Commit a5efa10

Browse files
committed
Add support nested fields
1 parent 9781980 commit a5efa10

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

datasets/nested_movies.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[
2+
{
3+
"id": 1,
4+
"title": "Pride and Prejudice",
5+
"info": {
6+
"comment": "A great book",
7+
"reviewNb": 50
8+
}
9+
},
10+
{
11+
"id": 2,
12+
"title": "Le Petit Prince",
13+
"info": {
14+
"comment": "A french book",
15+
"reviewNb": 600
16+
}
17+
},
18+
{
19+
"id": 3,
20+
"title": "Le Rouge et le Noir",
21+
"info": {
22+
"comment": "Another french book",
23+
"reviewNb": 700
24+
}
25+
},
26+
{
27+
"id": 4,
28+
"title": "Alice In Wonderland",
29+
"comment": "A weird book",
30+
"info": {
31+
"comment": "A weird book",
32+
"reviewNb": 800
33+
}
34+
},
35+
{
36+
"id": 5,
37+
"title": "The Hobbit",
38+
"info": {
39+
"comment": "An awesome book",
40+
"reviewNb": 900
41+
}
42+
},
43+
{
44+
"id": 6,
45+
"title": "Harry Potter and the Half-Blood Prince",
46+
"info": {
47+
"comment": "The best book",
48+
"reviewNb": 1000
49+
}
50+
},
51+
{ "id": 7,
52+
"title": "The Hitchhiker's Guide to the Galaxy"
53+
}
54+
]

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def songs_ndjson():
6767
with open('./datasets/songs.ndjson', 'r', encoding='utf-8') as song_ndjson_file:
6868
return song_ndjson_file.read().encode('utf-8')
6969

70+
@fixture(scope='session')
71+
def nested_movies():
72+
"""
73+
Runs once per session. Provides the content of nested_movies.json.
74+
"""
75+
with open('./datasets/nested_movies.json', 'r', encoding='utf-8') as nested_movie_file:
76+
yield json.loads(nested_movie_file.read())
77+
7078
@fixture(scope='function')
7179
def empty_index(client, index_uid: Optional[str] = None):
7280
index_uid = index_uid if index_uid else common.INDEX_UID

tests/index/test_index_search_meilisearch.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,37 @@ def test_phrase_search(index_with_documents):
350350
assert 'release_date' in response['hits'][0]
351351
assert response['hits'][0]['title'] == 'Dumbo'
352352
assert '_formatted' not in response['hits'][0]
353+
354+
def test_basic_search_on_nested_documents(index_with_documents, nested_movies):
355+
"""Tests search with an simple query on nested fields."""
356+
response = index_with_documents('nested_fields_index', nested_movies).search('An awesome')
357+
assert isinstance(response, dict)
358+
assert response['hits'][0]['id'] == 5
359+
assert len(response['hits']) == 1
360+
361+
def test_search_on_nested_documents_with_searchable_attributes(index_with_documents, nested_movies):
362+
"""Tests search on nested fields with searchable attribute."""
363+
index = index_with_documents('nested_fields_index', nested_movies)
364+
response_searchable_attributes = index.update_searchable_attributes(['title', 'info.comment'])
365+
index.wait_for_task(response_searchable_attributes['uid'])
366+
response = index.search('An awesome')
367+
assert isinstance(response, dict)
368+
assert response['hits'][0]['id'] == 5
369+
assert len(response['hits']) == 1
370+
371+
def test_search_on_nested_documents_with_sortable_attributes(index_with_documents, nested_movies):
372+
"""Tests search on nested fields with searchable attribute and sortable attributes."""
373+
index = index_with_documents('nested_fields_index', nested_movies)
374+
response_settings = index.update_settings({
375+
'searchableAttributes': ['title', 'info.comment'],
376+
'sortableAttributes': ['info.reviewNb'],
377+
})
378+
index.wait_for_task(response_settings['uid'])
379+
response = index.search(
380+
'',
381+
{
382+
'sort': ['info.reviewNb:desc']
383+
}
384+
)
385+
assert isinstance(response, dict)
386+
assert response['hits'][0]['id'] == 6

0 commit comments

Comments
 (0)