Closed
Description
We are thinking about creating a getOrCreateIndex
method.
Description
This method returns an Index
object. It checks if the index already exists and if it's not, it creates the index.
How
By trying to create the index, and catching the exception. In every situation, only one HTTP call is done.
An example in ruby:
require 'meilisearch'
URL = 'http://127.0.0.1:7700'
def get_or_create_index(index_uid, primary_key = nil)
client = MeiliSearch::Client.new(URL)
begin
index = client.create_index(uid: index_uid, primaryKey: primary_key)
rescue MeiliSearch::HTTPError => e
raise e unless e.message.include? 'index already exists'
index = client.index(index_uid)
end
return index
end
index = get_or_create_index('indexUID')
puts index.show
(In ruby the index
method is the equivalent of getIndex
in other SDKs, and this method does not do any HTTP call)
This example is out of the MeiliSearch library. Of course, this method must be included in the client class.
Tests
This method must be tested:
- Test with no index created before
- Test with an index already existing
- Test with a primary-key in parameter
Steps
Do it in:
Removed from the to-do list:
- Go -> not relevant since the client is divided into sub-clients that take an index themselves. The main client does not take any index.
Feedback
Don't hesitate to suggest other ideas to implement the method, or if I forgot any tests 🙂