Skip to content

Commit

Permalink
Add get_or_create_index method (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
curquiza authored Jul 9, 2020
1 parent 4e05a6a commit 746c191
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
client.create_index('books')
# Create an index and give the primary-key
client.create_index('books', primaryKey: 'book_id')
# Create an index or get the already existing one
client.create_index('books')
client.get_or_create_index('books', primaryKey: 'book_id')
```

#### List all indexes <!-- omit in toc -->
Expand Down
9 changes: 9 additions & 0 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def create_index(index_uid, options = {})
index_object(res['uid'])
end

def get_or_create_index(index_uid, options = {})
begin
create_index(index_uid, options)
rescue ApiError => e
raise e unless e.code == 'index_already_exists'
end
index_object(index_uid)
end

def delete_index(index_uid)
index_object(index_uid).delete
end
Expand Down
35 changes: 33 additions & 2 deletions spec/meilisearch/client/indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
@uid1 = 'uid1'
@uid2 = 'uid2'
@uid3 = 'uid3'
@uid4 = 'uid4'
@uid5 = 'uid5'
@primary_key = 'objectId'
end

Expand All @@ -31,6 +33,31 @@
expect(index.primary_key).to eq(@primary_key)
end

it 'creates an new index with get_or_create_index method' do
index = @client.get_or_create_index(@uid4)
expect(@client.indexes.count).to eq(4)
expect(@client.index(@uid4).uid).to eq(index.uid)
expect(@client.index(@uid4).uid).to eq(@uid4)
expect(@client.index(@uid4).primary_key).to be_nil
end

it 'creates an new index with get_or_create_index method and a primary-key' do
index = @client.get_or_create_index(@uid5, primaryKey: 'title')
expect(@client.indexes.count).to eq(5)
expect(@client.index(@uid5).uid).to eq(index.uid)
expect(@client.index(@uid5).uid).to eq(@uid5)
expect(@client.index(@uid5).primary_key).to eq(index.primary_key)
expect(@client.index(@uid5).primary_key).to eq('title')
end

it 'get an already existing index with get_or_create_index method' do
index = @client.get_or_create_index(@uid5)
expect(@client.indexes.count).to eq(5)
expect(@client.index(@uid5).uid).to eq(index.uid)
expect(@client.index(@uid5).uid).to eq(@uid5)
expect(@client.index(@uid5).primary_key).to eq('title')
end

it 'fails to create an index with an uid already taken' do
expect do
@client.create_index(@uid1)
Expand All @@ -46,9 +73,9 @@
it 'gets list of indexes' do
response = @client.indexes
expect(response).to be_a(Array)
expect(response.count).to eq(3)
expect(response.count).to eq(5)
uids = response.map { |elem| elem['uid'] }
expect(uids).to contain_exactly(@uid1, @uid2, @uid3)
expect(uids).to contain_exactly(@uid1, @uid2, @uid3, @uid4, @uid5)
end

it 'shows a specific index' do
Expand All @@ -72,6 +99,10 @@
expect { @client.show_index(@uid2) }.to raise_index_not_found_meilisearch_api_error
expect(@client.delete_index(@uid3)).to be_nil
expect { @client.show_index(@uid3) }.to raise_index_not_found_meilisearch_api_error
expect(@client.delete_index(@uid4)).to be_nil
expect { @client.show_index(@uid4) }.to raise_index_not_found_meilisearch_api_error
expect(@client.delete_index(@uid5)).to be_nil
expect { @client.show_index(@uid5) }.to raise_index_not_found_meilisearch_api_error
expect(@client.indexes.count).to eq(0)
end

Expand Down

0 comments on commit 746c191

Please sign in to comment.