Skip to content

Commit a3ad362

Browse files
bors[bot]curquiza
andauthored
Merge #123
123: Changes due to implicit index creation introduction r=curquiza a=curquiza Related to meilisearch/integration-guides#48 Changes: - code samples are updated - Getting Started is updated Breaking changes: - `get_or_create_index` now does at least one HTTP call (and two sometimes) - a readable attribute was added to the `Index` class: `primary_key`. It is updated when updating/creating/fetching the index. If you directly want to fetch the upstream primary-key, use `index.fetch_primary_key` that will do an HTTP call. `index.primary_key` is now a simple getter and will not do any HTTP call. - `index.show` and `client.show_index(...)` were removed. There are replaced by `index.fetch_info` and `client.fetch_index(...)`. There is no `client.get_index(...)` method because `get_XXX` are not idiomatic in ruby. - `index.update` now returns an `Index` object Co-authored-by: Clementine Urquizar <[email protected]>
2 parents f202f13 + 3d99a02 commit a3ad362

File tree

8 files changed

+99
-73
lines changed

8 files changed

+99
-73
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# You can read more on https://github.com/meilisearch/documentation/tree/master/.vuepress/code-samples
55
---
66
get_one_index_1: |-
7-
client.index('movies')
7+
client.fetch_index('movies')
88
list_all_indexes_1: |-
99
client.indexes
1010
create_an_index_1: |-

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444

4545
# lock
4646
Gemfile.lock
47+
48+
# VScode
49+
.vscode/

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
7474
require 'meilisearch'
7575

7676
client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
77-
index = client.create_index('books') # If your index does not exist
78-
index = client.index('books') # If you already created your index
77+
78+
# An index is where the documents are stored.
79+
index = client.index('books')
7980

8081
documents = [
8182
{ book_id: 123, title: 'Pride and Prejudice' },
@@ -85,6 +86,7 @@ documents = [
8586
{ book_id: 4, title: 'Harry Potter and the Half-Blood Prince' },
8687
{ book_id: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
8788
]
89+
# If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
8890
index.add_documents(documents) # => { "updateId": 0 }
8991
```
9092

lib/meilisearch/client.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ def indexes
1010
http_get '/indexes'
1111
end
1212

13-
def show_index(index_uid)
14-
index_object(index_uid).show
15-
end
16-
1713
# Usage:
1814
# client.create_index('indexUID')
1915
# client.create_index('indexUID', primaryKey: 'id')
2016
def create_index(index_uid, options = {})
2117
body = options.merge(uid: index_uid)
22-
res = http_post '/indexes', body
23-
index_object(res['uid'])
18+
index_hash = http_post '/indexes', body
19+
index_object(index_hash['uid'], index_hash['primaryKey'])
2420
end
2521

2622
def get_or_create_index(index_uid, options = {})
2723
begin
28-
create_index(index_uid, options)
24+
index_instance = fetch_index(index_uid)
2925
rescue ApiError => e
30-
raise e unless e.code == 'index_already_exists'
26+
raise e unless e.code == 'index_not_found'
27+
28+
index_instance = create_index(index_uid, options)
3129
end
32-
index_object(index_uid)
30+
index_instance
3331
end
3432

3533
def delete_index(index_uid)
@@ -41,7 +39,10 @@ def delete_index(index_uid)
4139
def index(index_uid)
4240
index_object(index_uid)
4341
end
44-
alias get_index index
42+
43+
def fetch_index(index_uid)
44+
index_object(index_uid).fetch_info
45+
end
4546

4647
### KEYS
4748

@@ -86,8 +87,8 @@ def dump_status(dump_uid)
8687

8788
private
8889

89-
def index_object(uid)
90-
Index.new(uid, @base_url, @api_key)
90+
def index_object(uid, primary_key = nil)
91+
Index.new(uid, @base_url, @api_key, primary_key)
9192
end
9293
end
9394
end

lib/meilisearch/index.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55

66
module MeiliSearch
77
class Index < HTTPRequest
8-
attr_reader :uid
8+
attr_reader :uid, :primary_key
99

10-
def initialize(index_uid, url, api_key = nil)
10+
def initialize(index_uid, url, api_key = nil, primary_key = nil)
1111
@uid = index_uid
12+
@primary_key = primary_key
1213
super(url, api_key)
1314
end
1415

15-
def show
16-
http_get "/indexes/#{@uid}"
16+
def fetch_info
17+
index_hash = http_get "/indexes/#{@uid}"
18+
@primary_key = index_hash['primaryKey']
19+
self
1720
end
18-
alias show_index show
1921

2022
def update(body)
21-
http_put "/indexes/#{@uid}", body
23+
index_hash = http_put "/indexes/#{@uid}", body
24+
@primary_key = index_hash['primaryKey']
25+
self
2226
end
2327
alias update_index update
2428

@@ -27,10 +31,10 @@ def delete
2731
end
2832
alias delete_index delete
2933

30-
def primary_key
31-
show['primaryKey']
34+
def fetch_primary_key
35+
fetch_info.primary_key
3236
end
33-
alias get_primary_key primary_key
37+
alias get_primary_key fetch_primary_key
3438

3539
### DOCUMENTS
3640

spec/meilisearch/client/indexes_spec.rb

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,53 @@
1717
expect(index).to be_a(MeiliSearch::Index)
1818
expect(index.uid).to eq(@uid1)
1919
expect(index.primary_key).to be_nil
20+
expect(index.fetch_primary_key).to be_nil
2021
end
2122

2223
it 'creates an index with primary-key' do
2324
index = @client.create_index(@uid2, primaryKey: @primary_key)
2425
expect(index).to be_a(MeiliSearch::Index)
2526
expect(index.uid).to eq(@uid2)
2627
expect(index.primary_key).to eq(@primary_key)
28+
expect(index.fetch_primary_key).to eq(@primary_key)
2729
end
2830

2931
it 'creates an index with uid in options - should not take it into account' do
3032
index = @client.create_index(@uid3, primaryKey: @primary_key, uid: 'wrong')
3133
expect(index).to be_a(MeiliSearch::Index)
3234
expect(index.uid).to eq(@uid3)
3335
expect(index.primary_key).to eq(@primary_key)
36+
expect(index.fetch_primary_key).to eq(@primary_key)
3437
end
3538

3639
it 'creates an new index with get_or_create_index method' do
3740
index = @client.get_or_create_index(@uid4)
41+
expect(index).to be_a(MeiliSearch::Index)
3842
expect(@client.indexes.count).to eq(4)
39-
expect(@client.index(@uid4).uid).to eq(index.uid)
40-
expect(@client.index(@uid4).uid).to eq(@uid4)
41-
expect(@client.index(@uid4).primary_key).to be_nil
43+
expect(@client.fetch_index(@uid4).uid).to eq(index.uid)
44+
expect(@client.fetch_index(@uid4).uid).to eq(@uid4)
45+
expect(@client.fetch_index(@uid4).primary_key).to be_nil
46+
expect(@client.fetch_index(@uid4).primary_key).to eq(index.primary_key)
4247
end
4348

4449
it 'creates an new index with get_or_create_index method and a primary-key' do
4550
index = @client.get_or_create_index(@uid5, primaryKey: 'title')
51+
expect(index).to be_a(MeiliSearch::Index)
4652
expect(@client.indexes.count).to eq(5)
47-
expect(@client.index(@uid5).uid).to eq(index.uid)
48-
expect(@client.index(@uid5).uid).to eq(@uid5)
49-
expect(@client.index(@uid5).primary_key).to eq(index.primary_key)
50-
expect(@client.index(@uid5).primary_key).to eq('title')
53+
expect(@client.fetch_index(@uid5).uid).to eq(index.uid)
54+
expect(@client.fetch_index(@uid5).uid).to eq(@uid5)
55+
expect(@client.fetch_index(@uid5).primary_key).to eq(index.primary_key)
56+
expect(@client.fetch_index(@uid5).primary_key).to eq('title')
5157
end
5258

5359
it 'get an already existing index with get_or_create_index method' do
5460
index = @client.get_or_create_index(@uid5)
61+
expect(index).to be_a(MeiliSearch::Index)
5562
expect(@client.indexes.count).to eq(5)
56-
expect(@client.index(@uid5).uid).to eq(index.uid)
57-
expect(@client.index(@uid5).uid).to eq(@uid5)
58-
expect(@client.index(@uid5).primary_key).to eq('title')
63+
expect(@client.fetch_index(@uid5).uid).to eq(index.uid)
64+
expect(@client.fetch_index(@uid5).uid).to eq(@uid5)
65+
expect(@client.fetch_index(@uid5).primary_key).to eq('title')
66+
expect(@client.fetch_index(@uid5).primary_key).to eq(index.primary_key)
5967
end
6068

6169
it 'fails to create an index with an uid already taken' do
@@ -78,35 +86,34 @@
7886
expect(uids).to contain_exactly(@uid1, @uid2, @uid3, @uid4, @uid5)
7987
end
8088

81-
it 'shows a specific index' do
82-
response = @client.show_index(@uid2)
83-
expect(response).to be_a(Hash)
84-
expect(response['uid']).to eq(@uid2)
85-
expect(response['primaryKey']).to eq(@primary_key)
89+
it 'fetch a specific index' do
90+
response = @client.fetch_index(@uid2)
91+
expect(response).to be_a(MeiliSearch::Index)
92+
expect(response.uid).to eq(@uid2)
93+
expect(response.primary_key).to eq(@primary_key)
94+
expect(response.fetch_primary_key).to eq(@primary_key)
8695
end
8796

8897
it 'returns an index object based on uid' do
8998
index = @client.index(@uid2)
9099
expect(index).to be_a(MeiliSearch::Index)
91100
expect(index.uid).to eq(@uid2)
101+
expect(index.primary_key).to be_nil
102+
expect(index.fetch_primary_key).to eq(@primary_key)
92103
expect(index.primary_key).to eq(@primary_key)
93104
end
94105

95106
it 'deletes index' do
96107
expect(@client.delete_index(@uid1)).to be_nil
97-
expect { @client.show_index(@uid1) }.to raise_index_not_found_meilisearch_api_error
108+
expect { @client.fetch_index(@uid1) }.to raise_index_not_found_meilisearch_api_error
98109
expect(@client.delete_index(@uid2)).to be_nil
99-
expect { @client.show_index(@uid2) }.to raise_index_not_found_meilisearch_api_error
110+
expect { @client.fetch_index(@uid2) }.to raise_index_not_found_meilisearch_api_error
100111
expect(@client.delete_index(@uid3)).to be_nil
101-
expect { @client.show_index(@uid3) }.to raise_index_not_found_meilisearch_api_error
112+
expect { @client.fetch_index(@uid3) }.to raise_index_not_found_meilisearch_api_error
102113
expect(@client.delete_index(@uid4)).to be_nil
103-
expect { @client.show_index(@uid4) }.to raise_index_not_found_meilisearch_api_error
114+
expect { @client.fetch_index(@uid4) }.to raise_index_not_found_meilisearch_api_error
104115
expect(@client.delete_index(@uid5)).to be_nil
105-
expect { @client.show_index(@uid5) }.to raise_index_not_found_meilisearch_api_error
116+
expect { @client.fetch_index(@uid5) }.to raise_index_not_found_meilisearch_api_error
106117
expect(@client.indexes.count).to eq(0)
107118
end
108-
109-
it 'works with method aliases' do
110-
expect(@client.method(:index) == @client.method(:get_index)).to be_truthy
111-
end
112119
end

spec/meilisearch/index/base_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
@index2 = client.create_index(@uid2, primaryKey: @primary_key)
1212
end
1313

14-
it 'shows the index' do
15-
response = @index1.show
16-
expect(response).to be_a(Hash)
17-
expect(response['name']).to eq(@uid1)
18-
expect(response['uid']).to eq(@uid1)
19-
expect(response['uid']).to eq(@index1.uid)
20-
expect(response['primaryKey']).to be_nil
14+
it 'fetch the info of the index' do
15+
index = @index1.fetch_info
16+
expect(index).to be_a(MeiliSearch::Index)
17+
expect(index.uid).to eq(@uid1)
18+
expect(index.primary_key).to be_nil
2119
end
2220

2321
it 'get primary-key of index if null' do
2422
expect(@index1.primary_key).to be_nil
23+
expect(@index1.fetch_primary_key).to be_nil
2524
end
2625

2726
it 'get primary-key of index if it exists' do
2827
expect(@index2.primary_key).to eq(@primary_key)
28+
expect(@index2.fetch_primary_key).to eq(@primary_key)
2929
end
3030

3131
it 'get uid of index' do
@@ -34,10 +34,11 @@
3434

3535
it 'updates primary-key of index if not defined before' do
3636
new_primary_key = 'id_test'
37-
response = @index1.update(primaryKey: new_primary_key)
38-
expect(response).to be_a(Hash)
39-
expect(response['uid']).to eq(@uid1)
40-
expect(@index1.primary_key).to eq(new_primary_key)
37+
index = @index1.update(primaryKey: new_primary_key)
38+
expect(index).to be_a(MeiliSearch::Index)
39+
expect(index.uid).to eq(@uid1)
40+
expect(index.primary_key).to eq(new_primary_key)
41+
expect(index.fetch_primary_key).to eq(new_primary_key)
4142
end
4243

4344
it 'returns error if trying to update primary-key if it is already defined' do
@@ -53,21 +54,20 @@
5354

5455
it 'deletes index' do
5556
expect(@index1.delete).to be_nil
56-
expect { @index1.show }.to raise_index_not_found_meilisearch_api_error
57+
expect { @index1.fetch_info }.to raise_index_not_found_meilisearch_api_error
5758
expect(@index2.delete).to be_nil
58-
expect { @index2.show }.to raise_index_not_found_meilisearch_api_error
59+
expect { @index2.fetch_info }.to raise_index_not_found_meilisearch_api_error
5960
end
6061

6162
it 'fails to manipulate index object after deletion' do
62-
expect { @index2.primary_key }.to raise_index_not_found_meilisearch_api_error
63-
expect { @index2.show }.to raise_index_not_found_meilisearch_api_error
63+
expect { @index2.fetch_primary_key }.to raise_index_not_found_meilisearch_api_error
64+
expect { @index2.fetch_info }.to raise_index_not_found_meilisearch_api_error
6465
expect { @index2.update(primaryKey: 'id_test') }.to raise_index_not_found_meilisearch_api_error
6566
expect { @index2.delete }.to raise_index_not_found_meilisearch_api_error
6667
end
6768

6869
it 'works with method aliases' do
69-
expect(@index1.method(:show) == @index1.method(:show_index)).to be_truthy
70-
expect(@index1.method(:primary_key) == @index1.method(:get_primary_key)).to be_truthy
70+
expect(@index1.method(:fetch_primary_key) == @index1.method(:get_primary_key)).to be_truthy
7171
expect(@index1.method(:update) == @index1.method(:update_index)).to be_truthy
7272
expect(@index1.method(:delete) == @index1.method(:delete_index)).to be_truthy
7373
end

0 commit comments

Comments
 (0)