DEV: Improve readability of setting weights in SearchIndexer.

This commit is contained in:
Guo Xiang Tan 2020-08-14 23:10:08 +08:00
parent a3e2152265
commit 8b811533b1
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20

View File

@ -21,7 +21,9 @@ class SearchIndexer
HtmlScrubber.scrub(html, strip_diacritics: strip_diacritics) HtmlScrubber.scrub(html, strip_diacritics: strip_diacritics)
end 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_data = raw_data.map do |data|
Search.prepare_data(data || "", :index) Search.prepare_data(data || "", :index)
end end
@ -85,6 +87,9 @@ class SearchIndexer
table_name.camelize.constantize.upsert(params) table_name.camelize.constantize.upsert(params)
rescue => e rescue => e
if Rails.env.test?
raise
else
# TODO is there any way we can safely avoid this? # 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 # best way is probably pushing search indexer into a dedicated process so it no longer happens on save
# instead in the post processor # instead in the post processor
@ -93,29 +98,53 @@ class SearchIndexer
message: "Unexpected error while indexing #{table} for search" message: "Unexpected error while indexing #{table} for search"
) )
end end
end
def self.update_topics_index(topic_id, title, cooked) def 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 # a bit inconsitent that we use title as A and body as B when in
# the post index body is D # 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]
)
end end
def self.update_posts_index(post_id, topic_title, category_name, topic_tags, cooked) def 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)
)
end end
def self.update_users_index(user_id, username, name) def 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
)
end end
def self.update_categories_index(category_id, name) def 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
)
end end
def self.update_tags_index(tag_id, name) def 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
)
end end
def self.queue_category_posts_reindex(category_id) def self.queue_category_posts_reindex(category_id)