Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync with meilisearch for ruby #233

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ruby/sync_with_meilisearch/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

gem 'appwrite'
gem 'meilisearch'
57 changes: 57 additions & 0 deletions ruby/sync_with_meilisearch/lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'appwrite'
require 'meilisearch'

def main(context)

client = Appwrite::Client.new
client
.set_endpoint('https://cloud.appwrite.io/v1')
.set_project(ENV['APPWRITE_FUNCTION_PROJECT_ID'])
.set_key(ENV['APPWRITE_API_KEY'])

meilisearch = MeiliSearch::Client.new(ENV['MEILISEARCH_ENDPOINT'], ENV['MEILISEARCH_ADMIN_API_KEY'])

index_name = ENV['MEILISEARCH_INDEX_NAME']

database = Appwrite::Database.new(client)

index = meilisearch.index(index_name)

cursor = nil

begin
queries = [Appwrite::Query.new.set_limit(100)]

if cursor
queries.push(Appwrite::Query.new.set_cursor(cursor))
end

documents = database.list_documents(
ENV['APPWRITE_DATABASE_ID'],
ENV['APPWRITE_COLLECTION_ID'],
queries
)

if documents['documents'].length > 0
cursor = documents['documents'].last['$id']
else
context.error('No more documents found.')
cursor = nil
break
end

context.log("Syncing chunk of #{documents['documents'].length} documents ...")
index.add_documents(documents['documents'], primary_key: '$id')
end while cursor

context.log('Sync finished.')

response = {
'message' => 'Sync finished.',
'status' => 'success'
}

context.response.set_body(response.to_json)

return context.response
end
15 changes: 15 additions & 0 deletions ruby/sync_with_meilisearch/lib/utils.rb
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'pathname'

def throw_if_missing(hash, keys)
missing = keys.select { |key| !hash.key?(key) || hash[key].nil? }
raise "Missing required fields: #{missing.join(', ')}" if missing.any?
end

def get_static_file(file_name)
static_folder = File.join(File.dirname(__FILE__), '../static')
File.read(File.join(static_folder, file_name))
end

def interpolate(template, values)
template.gsub(/{{([^}]+)}}/) { |match| values[match[2..-3].strip] || match }
end