Skip to content

Commit 01f5950

Browse files
bors[bot]CaroFG
andauthored
Merge #111
111: Add more search tests r=curquiza a=CaroFG Co-authored-by: CaroFG <[email protected]> Co-authored-by: CaroFG <[email protected]>
2 parents c2a8809 + 1fe374f commit 01f5950

14 files changed

+792
-147
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Style/GlobalVars:
2424
AllowedVariables:
2525
- $URL
2626
- $MASTER_KEY
27+
- $DEFAULT_SEARCH_RESPONSE_KEYS
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Cropped search' do
4+
before(:all) do
5+
@documents = [
6+
{
7+
objectId: 123,
8+
title: 'Pride and Prejudice',
9+
genre: 'romance',
10+
description: 'Pride and Prejudice is a romantic novel of manners written by Jane Austen in 1813.'
11+
},
12+
{
13+
objectId: 456, title: 'Le Petit Prince',
14+
genre: 'adventure',
15+
description: 'Le Petit Prince is a novella by French aristocrat, writer, and aviator Antoine de Saint-Exupéry.'
16+
},
17+
{
18+
objectId: 1,
19+
title: 'Alice In Wonderland',
20+
genre: 'adventure',
21+
desription: 'Alice\'s Adventures in Wonderland is an 1865 novel by English author Lewis Carroll.'
22+
},
23+
{
24+
objectId: 2,
25+
title: 'Le Rouge et le Noir',
26+
genre: 'romance',
27+
description: 'Le Rouge et le Noir is a historical psychological novel in two volumes by Stendhal.'
28+
},
29+
{
30+
objectId: 1344,
31+
title: 'The Hobbit',
32+
genre: 'adventure',
33+
description: 'The Hobbit is a children\'s fantasy novel by English author J. R. R. Tolkien.'
34+
},
35+
{
36+
objectId: 4,
37+
title: 'Harry Potter and the Half-Blood Prince',
38+
genre: 'fantasy',
39+
description: 'Harry Potter and the Half-Blood Prince is a fantasy novel written by J.K. Rowling.'
40+
},
41+
{
42+
objectId: 42,
43+
title: 'The Hitchhiker\'s Guide to the Galaxy',
44+
description: 'The Hitchhiker\'s Guide to the Galaxy is a comedy science fiction series by Douglas Adams.'
45+
}
46+
]
47+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
48+
clear_all_indexes(client)
49+
@index = client.create_index('books')
50+
@index.add_documents(@documents)
51+
sleep(0.1)
52+
end
53+
54+
after(:all) do
55+
@index.delete
56+
end
57+
58+
it 'does a custom search with attributes to crop' do
59+
response = @index.search('galaxy', { attributesToCrop: ['description'], cropLength: 15 })
60+
expect(response['hits'].first).to have_key('_formatted')
61+
expect(response['hits'].first['_formatted']['description']).to eq('s Guide to the Galaxy is a comedy')
62+
end
63+
64+
it 'does a custom placehodler search with attributes to crop' do
65+
response = @index.search('', { attributesToCrop: ['description'], cropLength: 20 })
66+
expect(response['hits'].first).to have_key('_formatted')
67+
expect(response['hits'].first['description']).to eq(@documents.first[:description])
68+
expect(response['hits'].first['_formatted']['description']).not_to eq(@documents.first[:description])
69+
end
70+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Search with highlight' do
4+
before(:all) do
5+
@documents = [
6+
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
7+
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
8+
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
9+
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
10+
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
11+
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
12+
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
13+
]
14+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
15+
clear_all_indexes(client)
16+
@index = client.create_index('books')
17+
@index.add_documents(@documents)
18+
sleep(0.1)
19+
end
20+
21+
after(:all) do
22+
@index.delete
23+
end
24+
25+
it 'does a custom search with highlight' do
26+
response = @index.search('the', attributesToHighlight: ['title'])
27+
expect(response).to be_a(Hash)
28+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
29+
expect(response['hits'].count).to eq(3)
30+
expect(response['hits'].first).to have_key('_formatted')
31+
expect(response['hits'].first['_formatted']['title']).to eq('<em>The</em> Hobbit')
32+
end
33+
34+
it 'does a custom placeholder search with attributes to highlight' do
35+
response = @index.search('', attributesToHighlight: ['*'])
36+
expect(response).to be_a(Hash)
37+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
38+
expect(response['hits'].count).to eq(7)
39+
expect(response['hits'].first).to have_key('_formatted')
40+
end
41+
42+
it 'does a custom placeholder search (nil) with attributes to highlight' do
43+
response = @index.search(nil, attributesToHighlight: ['*'])
44+
expect(response).to be_a(Hash)
45+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
46+
expect(response['hits'].count).to eq(@documents.count)
47+
expect(response['hits'].first).to have_key('_formatted')
48+
end
49+
end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Search with attributes to retrieve' do
4+
before(:all) do
5+
@documents = [
6+
{ objectId: 123, title: 'Pride and Prejudice', genre: 'romance' },
7+
{ objectId: 456, title: 'Le Petit Prince', genre: 'adventure' },
8+
{ objectId: 1, title: 'Alice In Wonderland', genre: 'adventure' },
9+
{ objectId: 2, title: 'Le Rouge et le Noir', genre: 'romance' },
10+
{ objectId: 1344, title: 'The Hobbit', genre: 'adventure' },
11+
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', genre: 'fantasy' },
12+
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
13+
]
14+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
15+
clear_all_indexes(client)
16+
@index = client.create_index('books')
17+
@index.add_documents(@documents)
18+
sleep(0.1)
19+
end
20+
21+
after(:all) do
22+
@index.delete
23+
end
24+
25+
it 'does a custom search with one attributesToRetrieve' do
26+
response = @index.search('the', attributesToRetrieve: ['title'])
27+
expect(response).to be_a(Hash)
28+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
29+
expect(response['hits'].count).to eq(3)
30+
expect(response['hits'].first).to have_key('title')
31+
expect(response['hits'].first).not_to have_key('objectId')
32+
expect(response['hits'].first).not_to have_key('genre')
33+
end
34+
35+
it 'does a custom search with multiple attributesToRetrieve' do
36+
response = @index.search('the', attributesToRetrieve: ['title', 'genre'])
37+
expect(response).to be_a(Hash)
38+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
39+
expect(response['hits'].count).to eq(3)
40+
expect(response['hits'].first).to have_key('title')
41+
expect(response['hits'].first).not_to have_key('objectId')
42+
expect(response['hits'].first).to have_key('genre')
43+
end
44+
45+
it 'does a custom search with all attributesToRetrieve' do
46+
response = @index.search('the', attributesToRetrieve: ['*'])
47+
expect(response).to be_a(Hash)
48+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
49+
expect(response['hits'].count).to eq(3)
50+
expect(response['hits'].first).to have_key('objectId')
51+
expect(response['hits'].first).to have_key('title')
52+
expect(response['hits'].first).to have_key('genre')
53+
end
54+
55+
it 'does a custom placeholder search with one attributesToRetrieve' do
56+
response = @index.search('', attributesToRetrieve: ['title'])
57+
expect(response).to be_a(Hash)
58+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
59+
expect(response['hits'].count).to eq(@documents.count)
60+
expect(response['hits'].first).to have_key('title')
61+
expect(response['hits'].first).not_to have_key('objectId')
62+
expect(response['hits'].first).not_to have_key('genre')
63+
end
64+
65+
it 'does a custom placeholder search with multiple attributesToRetrieve' do
66+
response = @index.search('', attributesToRetrieve: ['title', 'genre'])
67+
expect(response).to be_a(Hash)
68+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
69+
expect(response['hits'].count).to eq(@documents.count)
70+
expect(response['hits'].first).to have_key('title')
71+
expect(response['hits'].first).not_to have_key('objectId')
72+
expect(response['hits'].first).to have_key('genre')
73+
end
74+
75+
it 'does a custom placeholder search with all attributesToRetrieve' do
76+
response = @index.search('', attributesToRetrieve: ['*'])
77+
expect(response).to be_a(Hash)
78+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
79+
expect(response['hits'].count).to eq(@documents.count)
80+
expect(response['hits'].first).to have_key('title')
81+
expect(response['hits'].first).to have_key('objectId')
82+
expect(response['hits'].first).to have_key('genre')
83+
end
84+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Search with facetFilters' do
4+
before(:all) do
5+
@documents = [
6+
{ objectId: 123, title: 'Pride and Prejudice', year: '1813', genre: 'romance' },
7+
{ objectId: 456, title: 'Le Petit Prince', year: '1943', genre: 'adventure' },
8+
{ objectId: 1, title: 'Alice In Wonderland', year: '1865', genre: 'adventure' },
9+
{ objectId: 2, title: 'Le Rouge et le Noir', year: '1830', genre: 'romance' },
10+
{ objectId: 1344, title: 'The Hobbit', year: '1937', genre: 'adventure' },
11+
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', year: '2005', genre: 'fantasy' },
12+
{ objectId: 2056, title: 'Harry Potter and the Deathly Hallows', year: '2007', genre: 'fantasy' },
13+
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy', year: '1978' }
14+
]
15+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
16+
clear_all_indexes(client)
17+
@index = client.create_index('books')
18+
@index.add_documents(@documents)
19+
@index.update_attributes_for_faceting(['genre', 'year'])
20+
sleep(0.1)
21+
end
22+
23+
after(:all) do
24+
@index.delete
25+
end
26+
27+
it 'does a custom search with facetFilters' do
28+
response = @index.search('prinec', facetFilters: ['genre: fantasy'])
29+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
30+
expect(response['nbHits']).to eq(1)
31+
expect(response['hits'][0]['objectId']).to eq(4)
32+
end
33+
34+
it 'does a custom search with multiple facetFilters' do
35+
response = @index.search('potter', facetFilters: ['genre:fantasy', ['year:2005']])
36+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
37+
expect(response['nbHits']).to eq(1)
38+
expect(response['hits'][0]['objectId']).to eq(4)
39+
end
40+
41+
it 'does a custom placeholder search with facetFilters' do
42+
response = @index.search('', facetFilters: ['genre:fantasy'])
43+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
44+
expect(response['nbHits']).to eq(2)
45+
expect(response['hits'][0]['objectId']).to eq(4)
46+
expect(response['hits'][1]['objectId']).to eq(2056)
47+
end
48+
49+
it 'does a custom placeholder search with multiple facetFilters' do
50+
response = @index.search('', facetFilters: ['genre:adventure', ['year:1937']])
51+
expect(response.keys).to contain_exactly(*$DEFAULT_SEARCH_RESPONSE_KEYS)
52+
expect(response['nbHits']).to eq(1)
53+
expect(response['hits'][0]['objectId']).to eq(1344)
54+
end
55+
end
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'MeiliSearch::Index - Search with facetsDistribution' do
4+
before(:all) do
5+
@documents = [
6+
{
7+
objectId: 123,
8+
title: 'Pride and Prejudice',
9+
year: '1813',
10+
author: 'Jane Austen',
11+
genre: 'romance'
12+
},
13+
{
14+
objectId: 456,
15+
title: 'Le Petit Prince',
16+
year: '1943',
17+
author: 'Antoine de Saint-Exupéry',
18+
genre: 'adventure'
19+
},
20+
{
21+
objectId: 1,
22+
title: 'Alice In Wonderland',
23+
year: '1865',
24+
author: 'Lewis Carroll',
25+
genre: 'adventure'
26+
},
27+
{
28+
objectId: 2,
29+
title: 'Le Rouge et le Noir',
30+
year: '1830',
31+
author: 'Stendhal',
32+
genre: 'romance'
33+
},
34+
{
35+
objectId: 1344,
36+
title: 'The Hobbit',
37+
year: '1937',
38+
author: 'J. R. R. Tolkien',
39+
genre: 'adventure'
40+
},
41+
{
42+
objectId: 4,
43+
title: 'Harry Potter and the Half-Blood Prince',
44+
year: '2005',
45+
author: 'J. K. Rowling',
46+
genre: 'fantasy'
47+
},
48+
{
49+
objectId: 2056,
50+
title: 'Harry Potter and the Deathly Hallows',
51+
year: '2007',
52+
author: 'J. K. Rowling',
53+
genre: 'fantasy'
54+
},
55+
{
56+
objectId: 42,
57+
title: 'The Hitchhiker\'s Guide to the Galaxy',
58+
year: '1978',
59+
author: 'Douglas Adams'
60+
},
61+
{
62+
objectId: 190,
63+
title: 'A Game of Thrones',
64+
year: '1996',
65+
author: 'George R. R. Martin',
66+
genre: 'fantasy'
67+
}
68+
]
69+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
70+
clear_all_indexes(client)
71+
@index = client.create_index('books')
72+
@index.add_documents(@documents)
73+
@index.update_attributes_for_faceting(['genre', 'year', 'author'])
74+
sleep(0.1)
75+
end
76+
77+
after(:all) do
78+
@index.delete
79+
end
80+
81+
it 'does a custom search with facetsDistribution' do
82+
response = @index.search('prinec', facetsDistribution: ['genre', 'author'])
83+
expect(response.keys).to contain_exactly(
84+
*$DEFAULT_SEARCH_RESPONSE_KEYS,
85+
'facetsDistribution',
86+
'exhaustiveFacetsCount'
87+
)
88+
expect(response['exhaustiveFacetsCount']).to be true
89+
expect(response['nbHits']).to eq(2)
90+
expect(response['facetsDistribution'].keys).to contain_exactly('genre', 'author')
91+
expect(response['facetsDistribution']['genre'].keys).to contain_exactly('romance', 'adventure', 'fantasy')
92+
expect(response['facetsDistribution']['genre']['romance']).to eq(0)
93+
expect(response['facetsDistribution']['genre']['adventure']).to eq(1)
94+
expect(response['facetsDistribution']['genre']['fantasy']).to eq(1)
95+
expect(response['facetsDistribution']['author']['J. K. Rowling']).to eq(1)
96+
expect(response['facetsDistribution']['author']['Antoine de Saint-Exupéry']).to eq(1)
97+
end
98+
99+
it 'does a custom placeholder search with facetsDistribution' do
100+
response = @index.search('', facetsDistribution: ['genre', 'author'])
101+
expect(response.keys).to contain_exactly(
102+
*$DEFAULT_SEARCH_RESPONSE_KEYS,
103+
'facetsDistribution',
104+
'exhaustiveFacetsCount'
105+
)
106+
expect(response['exhaustiveFacetsCount']).to be true
107+
expect(response['nbHits']).to eq(@documents.count)
108+
expect(response['facetsDistribution'].keys).to contain_exactly('genre', 'author')
109+
expect(response['facetsDistribution']['genre'].keys).to contain_exactly('romance', 'adventure', 'fantasy')
110+
expect(response['facetsDistribution']['genre']['romance']).to eq(2)
111+
expect(response['facetsDistribution']['genre']['adventure']).to eq(3)
112+
expect(response['facetsDistribution']['genre']['fantasy']).to eq(3)
113+
expect(response['facetsDistribution']['author']['J. K. Rowling']).to eq(2)
114+
end
115+
end

0 commit comments

Comments
 (0)