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 1 commit
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/meilisearch/Gemfile
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

gem 'appwrite'
gem 'meilisearch'
12 changes: 12 additions & 0 deletions ruby/meilisearch/config.rb
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Config
APPWRITE_ENDPOINT = ENV['APPWRITE_ENDPOINT']
APPWRITE_API_KEY = ENV['APPWRITE_API_KEY']
APPWRITE_FUNCTION_PROJECT_ID = ENV['APPWRITE_FUNCTION_PROJECT_ID']
APPWRITE_DATABASE_ID = ENV['APPWRITE_DATABASE_ID']
APPWRITE_COLLECTION_ID = ENV['APPWRITE_COLLECTION_ID']
MEILISEARCH_ENDPOINT = ENV['MEILISEARCH_ENDPOINT']
MEILISEARCH_INDEX_NAME = ENV['MEILISEARCH_INDEX_NAME']
MEILISEARCH_ADMIN_API_KEY = ENV['MEILISEARCH_ADMIN_API_KEY']
MEILISEARCH_SEARCH_API_KEY = ENV['MEILISEARCH_SEARCH_API_KEY']
end

46 changes: 46 additions & 0 deletions ruby/meilisearch/lib/main.rb
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'appwrite'
require 'meilisearch'
require_relative 'config'

appwrite = Appwrite::Client.new
appwrite
.set_endpoint(Config::APPWRITE_ENDPOINT)
.set_project(Config::APPWRITE_FUNCTION_PROJECT_ID)
.set_key(Config::APPWRITE_API_KEY)

meilisearch = MeiliSearch::Client.new(Config::MEILISEARCH_ENDPOINT, Config::MEILISEARCH_ADMIN_API_KEY)

index_name = Config::MEILISEARCH_INDEX_NAME

database = Appwrite::Database.new(appwrite)

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(
Config::APPWRITE_DATABASE_ID,
Config::APPWRITE_COLLECTION_ID,
queries
)

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

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

puts 'Sync finished.'
15 changes: 15 additions & 0 deletions ruby/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