Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Commit

Permalink
Raise QueryException if one of search queries contains an error key
Browse files Browse the repository at this point in the history
  • Loading branch information
saulius committed Jan 20, 2014
1 parent 0b0d7b0 commit 4cfbbcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 17 additions & 10 deletions app/services/search_service/fuzzy_search/fuzzy_search_result.rb
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/trade_tariff_backend/search_client.rb
Expand Up @@ -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
Expand Down

0 comments on commit 4cfbbcb

Please sign in to comment.