diff --git a/app/services/search_service/fuzzy_search/fuzzy_search_result.rb b/app/services/search_service/fuzzy_search/fuzzy_search_result.rb index 0c003569..5357dd7d 100644 --- a/app/services/search_service/fuzzy_search/fuzzy_search_result.rb +++ b/app/services/search_service/fuzzy_search/fuzzy_search_result.rb @@ -3,20 +3,22 @@ class FuzzySearch < BaseSearch class FuzzySearchResult include Enumerable + def initialize(query_string, date) + @query_string = query_string + @date = date + end + # We craft Elasticsearch queries in streamlined way, but # certain queries need additional query details to be provided. # These details can be specified in QUERY_OPTIONS # # See #each_query for more details - QUERY_OPTIONS = { - goods_nomenclature_match: { - 'tariff-sections' => { fields: ["title"] } + def query_options + { + goods_nomenclature_match: { + "#{TradeTariffBackend.search_namespace}-sections" => { fields: ['title'] } + } } - } - - def initialize(query_string, date) - @query_string = query_string - @date = date end # Elasticsearch multisearch endpoint returns results in the same @@ -26,8 +28,13 @@ def initialize(query_string, date) # More here http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html def each return to_enum(:each) unless block_given? + each_query.each_with_index do |(match_type, search_index, _), idx| - yield match_type, search_index, search_results[idx].hits.hits + search_results[idx].tap do |search_result| + raise TradeTariffBackend::SearchClient::QueryError.new(search_result.error) if search_result.error? + + yield match_type, search_index, search_results[idx].hits!.hits! + end end end @@ -48,7 +55,7 @@ def each_query(&block) yield search_query.match_type, search_query.index, search_query.query( - QUERY_OPTIONS.fetch(search_query.match_type, {}).fetch(search_query.index.name, {}) + query_options.fetch(search_query.match_type, {}).fetch(search_query.index.name, {}) ) end end diff --git a/lib/trade_tariff_backend/search_client.rb b/lib/trade_tariff_backend/search_client.rb index de0e1712..29b972eb 100644 --- a/lib/trade_tariff_backend/search_client.rb +++ b/lib/trade_tariff_backend/search_client.rb @@ -2,6 +2,10 @@ module TradeTariffBackend class SearchClient < SimpleDelegator + + # Raised if Elasticsearch returns an error from query + QueryError = Class.new(StandardError) + attr_reader :indexed_models attr_reader :namespace attr_reader :index_page_size