From 20c9c60dea9c8bead9a7216741f98ddc47ef6fc0 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Sat, 1 Oct 2016 08:53:30 +0200 Subject: [PATCH] FEATURE: support multisite configuration for search:reindex task --- lib/tasks/search.rake | 92 +++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/lib/tasks/search.rake b/lib/tasks/search.rake index 3d6483cad75..4f118227c69 100644 --- a/lib/tasks/search.rake +++ b/lib/tasks/search.rake @@ -1,46 +1,54 @@ task "search:reindex" => :environment do + ENV['RAILS_DB'] ? reindex_search : reindex_search_all_sites +end + +def reindex_search(db=RailsMultisite::ConnectionManagement.current_db) + puts "Reindexing '#{db}'" + puts "" + puts "Posts:" + Post.exec_sql("select p.id, p.cooked, c.name category, t.title, p.post_number, t.id topic_id from + posts p + join topics t on t.id = p.topic_id + left join categories c on c.id = t.category_id + ").each do |p| + post_id = p["id"] + cooked = p["cooked"] + title = p["title"] + category = p["cat"] + post_number = p["post_number"].to_i + topic_id = p["topic_id"].to_i + + SearchObserver.update_posts_index(post_id, cooked, title, category) + SearchObserver.update_topics_index(topic_id, title , cooked) if post_number == 1 + + putc "." + end + + puts + puts "Users:" + User.exec_sql("select id, name, username from users").each do |u| + id = u["id"] + name = u["name"] + username = u["username"] + SearchObserver.update_users_index(id, username, name) + + putc "." + end + + puts + puts "Categories" + + Category.exec_sql("select id, name from categories").each do |c| + id = c["id"] + name = c["name"] + SearchObserver.update_categories_index(id, name) + end + + puts +end + +def reindex_search_all_sites RailsMultisite::ConnectionManagement.each_connection do |db| - puts "Reindexing #{db}" - puts "" - puts "Posts:" - Post.exec_sql("select p.id, p.cooked, c.name category, t.title, p.post_number, t.id topic_id from - posts p - join topics t on t.id = p.topic_id - left join categories c on c.id = t.category_id - ").each do |p| - post_id = p["id"] - cooked = p["cooked"] - title = p["title"] - category = p["cat"] - post_number = p["post_number"].to_i - topic_id = p["topic_id"].to_i - - SearchObserver.update_posts_index(post_id, cooked, title, category) - SearchObserver.update_topics_index(topic_id, title , cooked) if post_number == 1 - - putc "." - end - - puts - puts "Users:" - User.exec_sql("select id, name, username from users").each do |u| - id = u["id"] - name = u["name"] - username = u["username"] - SearchObserver.update_users_index(id, username, name) - - putc "." - end - - puts - puts "Categories" - - Category.exec_sql("select id, name from categories").each do |c| - id = c["id"] - name = c["name"] - SearchObserver.update_categories_index(id, name) - end - - puts + reindex_search(db) end end