Browse files

Raise QueryException if one of search queries contains an error key

  • Loading branch information...
1 parent 0b0d7b0 commit 4cfbbcb56d38063683ced4a55b9e02de10d9440f @saulius saulius committed Jan 20, 2014
View
27 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
View
4 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

0 comments on commit 4cfbbcb

Please sign in to comment.