From 4cade9d7f6c0e65a2749140c9318faf0164fbcc7 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Mon, 4 Oct 2021 11:40:22 +0800 Subject: [PATCH] FIX: `Topic.similar_to` results in invalid query for certain locales. (#14497) For `zh_CN`, we use the `cppjieba_rb` gem to remove stop words so calling `Search.prepare_data` may result in an empty string. --- app/models/topic.rb | 8 ++++---- spec/models/topic_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 6f155fec2f9..cf6b285db8c 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -581,11 +581,11 @@ class Topic < ActiveRecord::Base def self.similar_to(title, raw, user = nil) return [] if title.blank? raw = raw.presence || "" + search_data = Search.prepare_data(title.strip) - tsquery = Search.set_tsquery_weight_filter( - Search.prepare_data(title.strip), - 'A' - ) + return [] if search_data.blank? + + tsquery = Search.set_tsquery_weight_filter(search_data, 'A') if raw.present? cooked = SearchIndexer::HtmlScrubber.scrub( diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index a84253d9c0b..cbc5bfba370 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -540,6 +540,12 @@ describe Topic do expect(Topic.similar_to('some title', 'https://discourse.org/#INCORRECT#URI')).to be_empty end + it 'does not result in invalid statement when title is all stopwords for zh_CN' do + SiteSetting.default_locale = "zh_CN" + + expect(Topic.similar_to("怎么上自己的", '')).to eq([]) + end + context 'with a similar topic' do fab!(:post) { SearchIndexer.enable