A platform for community discussion. Free, open, simple.
PERF: Avoid parsing `Post#cooked` with Nokogiri for every search.
Guo Xiang Tan181c4eb76083
diff --git a/app/controllers/similar_topics_controller.rb b/app/controllers/similar_topics_controller.rbindex a42677893dc..e2f33146576 100644--- a/app/controllers/similar_topics_controller.rb+++ b/app/controllers/similar_topics_controller.rb@@ -10,7 +10,7 @@ class SimilarTopicsController < ApplicationControllerattr_reader :topicdef blurb- Search::GroupedSearchResults.blurb_for(@topic.try(:blurb))+ Search::GroupedSearchResults.blurb_for(cooked: @topic.try(:blurb))endenddiff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rbindex 27a18f7dbb5..a73585e5791 100644--- a/app/services/search_indexer.rb+++ b/app/services/search_indexer.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueclass SearchIndexer- POST_INDEX_VERSION = 3+ POST_INDEX_VERSION = 4MIN_POST_REINDEX_VERSION = 3TOPIC_INDEX_VERSION = 3CATEGORY_INDEX_VERSION = 3@@ -39,8 +39,6 @@ class SearchIndexersetweight(to_tsvector('#{stemmer}', coalesce(:d,'')), 'D')SQL- indexed_data = search_data.select { |d| d.length > 0 }.join(' ')-ranked_params = {a: search_data[0],b: search_data[1],@@ -48,6 +46,13 @@ class SearchIndexerd: search_data[3],}+ indexed_data =+ if table.to_s == "post"+ ranked_params[:d]+ else+ search_data.select { |d| d.length > 0 }.join(' ')+ end+tsvector = DB.query_single("SELECT #{ranked_index}", ranked_params)[0]additional_lexemes = []@@ -105,7 +110,7 @@ class SearchIndexerscrubbed_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 C+ # the post index body is Dupdate_index(table: 'topic', id: topic_id, raw_data: [title, scrubbed_cooked])end@@ -165,9 +170,11 @@ class SearchIndexerendcategory_name = topic.category&.name if topic+if topic- tags = topic.tags.select(:id, :name)- unless tags.empty?+ tags = topic.tags.select(:id, :name).to_a++ if tags.present?tag_names = (tags.map(&:name) + Tag.where(target_tag_id: tags.map(&:id)).pluck(:name)).join(' ')endenddiff --git a/lib/search.rb b/lib/search.rbindex 0caf6e31335..02462cded77 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -1128,7 +1128,7 @@ class Searchenddef posts_eager_loads(query)- query = query.includes(:user)+ query = query.includes(:user, :post_search_data)topic_eager_loads = [:category]if SiteSetting.tagging_enableddiff --git a/lib/search/grouped_search_results.rb b/lib/search/grouped_search_results.rbindex 51413dccba0..b6059f0ec87 100644--- a/lib/search/grouped_search_results.rb+++ b/lib/search/grouped_search_results.rb@@ -58,7 +58,19 @@ class Searchenddef blurb(post)- GroupedSearchResults.blurb_for(post.cooked, @blurb_term, @blurb_length)+ opts = {+ term: @blurb_term,+ blurb_length: @blurb_length+ }++ if post.post_search_data.version > SearchIndexer::MIN_POST_REINDEX_VERSION+ opts[:cooked] = post.post_search_data.raw_data+ opts[:scrub] = false+ else+ opts[:cooked] = post.cooked+ end++ GroupedSearchResults.blurb_for(**opts)enddef add(object)@@ -73,9 +85,9 @@ class Searchendend- def self.blurb_for(cooked, term = nil, blurb_length = BLURB_LENGTH)+ def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true)blurb = nil- cooked = SearchIndexer.scrub_html_for_search(cooked)+ cooked = SearchIndexer.scrub_html_for_search(cooked) if scruburls = Set.newcooked.scan(URI.regexp(%w{http https})) { urls << $& }diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex f8c6faed2da..e37367bee2e 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -38,7 +38,7 @@ describe Search dolink to a video file: https://somesite.com/content/somethingelse.MOVRAW- result = Search::GroupedSearchResults.blurb_for(cooked)+ result = Search::GroupedSearchResults.blurb_for(cooked: cooked)expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}")end@@ -51,7 +51,7 @@ describe Search dohttp://localhost/uploads/default/original/1X/90adc0092b30c04b761541bc0322d0dce3d896e7.m4aRAW- result = Search::GroupedSearchResults.blurb_for(cooked)+ result = Search::GroupedSearchResults.blurb_for(cooked: cooked)expect(result).to eq("Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{I18n.t("search.audio")}")end@@ -59,7 +59,7 @@ describe Search docooked = <<~RAWinvalid URL: http:error] should not trip up blurb generation.RAW- result = Search::GroupedSearchResults.blurb_for(cooked)+ result = Search::GroupedSearchResults.blurb_for(cooked: cooked)expect(result).to eq("invalid URL: http:error] should not trip up blurb generation.")endenddiff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex c947528b692..82592684b52 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -3,10 +3,22 @@require 'rails_helper'describe SearchController do+ fab!(:awesome_topic) do+ topic = Fabricate(:topic)+ tag = Fabricate(:tag)+ topic.tags << tag+ Fabricate(:tag, target_tag_id: tag.id)+ topic+ endfab!(:awesome_post) doSearchIndexer.enable- Fabricate(:post, raw: 'this is my really awesome post')+ Fabricate(:post, topic: awesome_topic, raw: 'this is my really awesome post')+ end++ fab!(:awesome_post_2) do+ SearchIndexer.enable+ Fabricate(:post, raw: 'this is my really awesome post 2')endfab!(:user) do@@ -95,10 +107,14 @@ describe SearchController dodata = response.parsed_body- expect(data['posts'].length).to eq(1)- expect(data['posts'][0]['id']).to eq(awesome_post.id)- expect(data['posts'][0]['blurb']).to eq(awesome_post.raw)- expect(data['topics'][0]['id']).to eq(awesome_post.topic_id)+ expect(data['posts'].length).to eq(2)+ expect(data['posts'][0]['id']).to eq(awesome_post_2.id)+ expect(data['posts'][0]['blurb']).to eq(awesome_post_2.raw)+ expect(data['topics'][0]['id']).to eq(awesome_post_2.topic_id)++ expect(data['posts'][1]['id']).to eq(awesome_post.id)+ expect(data['posts'][1]['blurb']).to eq(awesome_post.raw)+ expect(data['topics'][1]['id']).to eq(awesome_post.topic_id)endit "can search correctly with advanced search filters" dodiff --git a/spec/services/search_indexer_spec.rb b/spec/services/search_indexer_spec.rbindex d23c171d066..b814949ce0f 100644--- a/spec/services/search_indexer_spec.rb+++ b/spec/services/search_indexer_spec.rb@@ -20,12 +20,13 @@ describe SearchIndexer doit 'correctly indexes chinese' doSiteSetting.default_locale = 'zh_CN'data = "你好世界"- expect(data.split(" ").length).to eq(1)- SearchIndexer.update_posts_index(post_id, "你好世界", "", "", nil)+ SearchIndexer.update_posts_index(post_id, "", "", "", data)- raw_data = PostSearchData.where(post_id: post_id).pluck(:raw_data)[0]- expect(raw_data.split(' ').length).to eq(2)+ post_search_data = PostSearchData.find_by(post_id: post_id)++ expect(post_search_data.raw_data).to eq("你好 世界")+ expect(post_search_data.search_data).to eq("'世界':2 '你好':1")endit 'extract youtube title' do@@ -104,11 +105,6 @@ describe SearchIndexer doexpect(raw_data).to eq("This is a test")expect(locale).to eq(SiteSetting.default_locale)expect(version).to eq(SearchIndexer::POST_INDEX_VERSION)-- SearchIndexer.update_posts_index(post_id, "tester", "", nil, nil)-- raw_data = PostSearchData.where(post_id: post_id).pluck(:raw_data)[0]- expect(raw_data).to eq("tester")enddescribe '.index' do@@ -118,10 +114,10 @@ describe SearchIndexer doexpect { post }.to change { PostSearchData.count }.by(1)expect { post.update!(raw: "this is new content") }- .to change { post.reload.post_search_data.raw_data }+ .to change { post.reload.post_search_data.search_data }expect { post.update!(topic_id: Fabricate(:topic).id) }- .to change { post.reload.post_search_data.raw_data }+ .to change { post.reload.post_search_data.search_data }endit 'should not index posts with empty raw' do@@ -141,7 +137,7 @@ describe SearchIndexer dotopic = post.topicexpect(post.post_search_data.raw_data).to eq(- "#{topic.title} #{topic.category.name} https://meta.discourse.org/some.png"+ "https://meta.discourse.org/some.png")end@@ -158,7 +154,7 @@ describe SearchIndexer dotopic = post.topicexpect(post.post_search_data.raw_data).to eq(- "#{topic.title} #{category.name} a https://cnn.com , http://stuff.com.au b http://abc.net/xyz=1 abc.net/xyz=1"+ "a https://cnn.com , http://stuff.com.au b http://abc.net/xyz=1 abc.net/xyz=1")expect(post.post_search_data.search_data).to eq(@@ -190,7 +186,7 @@ describe SearchIndexer do)expect(post.post_search_data.raw_data).to eq(- "#{topic.title} #{topic.category.name} Let me see how I can fix this image white walkers GOT"+ "Let me see how I can fix this image white walkers GOT")endend