A platform for community discussion. Free, open, simple.
DEV: Improve readability of setting weights in `SearchIndexer`.
Guo Xiang Tan8b811533b17d
diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rbindex 4c0ac81dbac..8eb16cab773 100644--- a/app/services/search_indexer.rb+++ b/app/services/search_indexer.rb@@ -21,7 +21,9 @@ class SearchIndexerHtmlScrubber.scrub(html, strip_diacritics: strip_diacritics)end- def self.update_index(table: , id: , raw_data:)+ def self.update_index(table: , id: , a_weight: nil, b_weight: nil, c_weight: nil, d_weight: nil)+ raw_data = [a_weight, b_weight, c_weight, d_weight]+search_data = raw_data.map do |data|Search.prepare_data(data || "", :index)end@@ -85,37 +87,64 @@ class SearchIndexertable_name.camelize.constantize.upsert(params)rescue => e- # TODO is there any way we can safely avoid this?- # best way is probably pushing search indexer into a dedicated process so it no longer happens on save- # instead in the post processor- Discourse.warn_exception(- e,- message: "Unexpected error while indexing #{table} for search"- )+ if Rails.env.test?+ raise+ else+ # TODO is there any way we can safely avoid this?+ # best way is probably pushing search indexer into a dedicated process so it no longer happens on save+ # instead in the post processor+ Discourse.warn_exception(+ e,+ message: "Unexpected error while indexing #{table} for search"+ )+ endenddef self.update_topics_index(topic_id, title, cooked)- scrubbed_cooked = scrub_html_for_search(cooked)[0...Topic::MAX_SIMILAR_BODY_LENGTH]-# a bit inconsitent that we use title as A and body as B when in# the post index body is D- update_index(table: 'topic', id: topic_id, raw_data: [title, scrubbed_cooked])+ update_index(+ table: 'topic',+ id: topic_id,+ a_weight: title,+ b_weight: scrub_html_for_search(cooked)[0...Topic::MAX_SIMILAR_BODY_LENGTH]+ )enddef self.update_posts_index(post_id, topic_title, category_name, topic_tags, cooked)- update_index(table: 'post', id: post_id, raw_data: [topic_title, category_name, topic_tags, scrub_html_for_search(cooked)])+ update_index(+ table: 'post',+ id: post_id,+ a_weight: topic_title,+ b_weight: category_name,+ c_weight: topic_tags,+ d_weight: scrub_html_for_search(cooked)+ )enddef self.update_users_index(user_id, username, name)- update_index(table: 'user', id: user_id, raw_data: [username, name])+ update_index(+ table: 'user',+ id: user_id,+ a_weight: username,+ b_weight: name+ )enddef self.update_categories_index(category_id, name)- update_index(table: 'category', id: category_id, raw_data: [name])+ update_index(+ table: 'category',+ id: category_id,+ a_weight: name+ )enddef self.update_tags_index(tag_id, name)- update_index(table: 'tag', id: tag_id, raw_data: [name.downcase])+ update_index(+ table: 'tag',+ id: tag_id,+ a_weight: name.downcase+ )enddef self.queue_category_posts_reindex(category_id)