Fix search error when ElasticSearch is enabled but not available (#11954)

* Fallback to Database search when ES not available

* Prevent double work if ES gives 0 result

* Apply suggestion from code review
pull/11962/head
Jeong Arm 2019-09-27 01:06:08 +09:00 committed by Eugen Rochko
parent 5034418e2c
commit 00d7bdcc2a
2 changed files with 11 additions and 10 deletions

View File

@ -42,11 +42,9 @@ class AccountSearchService < BaseService
return [] if limit_for_non_exact_results.zero? return [] if limit_for_non_exact_results.zero?
@search_results ||= begin @search_results ||= begin
if Chewy.enabled? results = from_elasticsearch if Chewy.enabled?
from_elasticsearch results ||= from_database
else results
from_database
end
end end
end end
@ -92,6 +90,8 @@ class AccountSearchService < BaseService
ActiveRecord::Associations::Preloader.new.preload(records, :account_stat) ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
records records
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
nil
end end
def reputation_score_function def reputation_score_function

View File

@ -6,11 +6,10 @@ class TagSearchService < BaseService
@offset = options[:offset].to_i @offset = options[:offset].to_i
@limit = options[:limit].to_i @limit = options[:limit].to_i
if Chewy.enabled? results = from_elasticsearch if Chewy.enabled?
from_elasticsearch results ||= from_database
else
from_database results
end
end end
private private
@ -74,6 +73,8 @@ class TagSearchService < BaseService
} }
TagsIndex.query(query).filter(filter).limit(@limit).offset(@offset).objects.compact TagsIndex.query(query).filter(filter).limit(@limit).offset(@offset).objects.compact
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
nil
end end
def from_database def from_database