2014-06-30 20:09:55 -04:00
|
|
|
module Jobs
|
|
|
|
# if locale changes or search algorithm changes we may want to reindex stuff
|
|
|
|
class ReindexSearch < Jobs::Scheduled
|
|
|
|
every 1.day
|
|
|
|
|
|
|
|
def execute(args)
|
2017-08-16 07:38:34 -04:00
|
|
|
rebuild_problem_topics
|
|
|
|
rebuild_problem_posts
|
|
|
|
rebuild_problem_categories
|
|
|
|
rebuild_problem_users
|
|
|
|
end
|
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
def rebuild_problem_categories(limit = 500)
|
|
|
|
category_ids = load_problem_category_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
category_ids.each do |id|
|
|
|
|
category = Category.find_by(id: id)
|
|
|
|
SearchIndexer.index(category, force: true) if category
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_problem_users(limit = 10000)
|
2017-08-16 08:18:59 -04:00
|
|
|
user_ids = load_problem_user_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
user_ids.each do |id|
|
|
|
|
user = User.find_by(id: id)
|
|
|
|
SearchIndexer.index(user, force: true) if user
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_problem_topics(limit = 10000)
|
2017-08-16 08:18:59 -04:00
|
|
|
topic_ids = load_problem_topic_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
topic_ids.each do |id|
|
|
|
|
topic = Topic.find_by(id: id)
|
|
|
|
SearchIndexer.index(topic, force: true) if topic
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_problem_posts(limit = 10000)
|
2017-08-16 08:18:59 -04:00
|
|
|
post_ids = load_problem_post_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
post_ids.each do |id|
|
|
|
|
post = Post.find_by(id: id)
|
|
|
|
# could be deleted while iterating through batch
|
|
|
|
SearchIndexer.index(post, force: true) if post
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
def load_problem_post_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
Post.joins(:topic)
|
|
|
|
.where('posts.id IN (
|
|
|
|
SELECT p2.id FROM posts p2
|
|
|
|
LEFT JOIN post_search_data pd ON pd.locale = ? AND pd.version = ? AND p2.id = pd.post_id
|
|
|
|
WHERE pd.post_id IS NULL
|
|
|
|
)', SiteSetting.default_locale, Search::INDEX_VERSION)
|
|
|
|
.limit(limit)
|
2017-08-16 08:18:59 -04:00
|
|
|
.pluck(:id)
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
def load_problem_category_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
Category.joins(:category_search_data)
|
|
|
|
.where('category_search_data.locale != ?
|
|
|
|
OR category_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
|
|
|
|
.limit(limit)
|
2017-08-16 08:18:59 -04:00
|
|
|
.pluck(:id)
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
def load_problem_topic_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
Topic.joins(:topic_search_data)
|
|
|
|
.where('topic_search_data.locale != ?
|
|
|
|
OR topic_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
|
|
|
|
.limit(limit)
|
2017-08-16 08:18:59 -04:00
|
|
|
.pluck(:id)
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
|
2017-08-16 08:18:59 -04:00
|
|
|
def load_problem_user_ids(limit)
|
2017-08-16 07:38:34 -04:00
|
|
|
User.joins(:user_search_data)
|
|
|
|
.where('user_search_data.locale != ?
|
|
|
|
OR user_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
|
|
|
|
.limit(limit)
|
2017-08-16 08:18:59 -04:00
|
|
|
.pluck(:id)
|
2014-06-30 20:09:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|