Commit 2f16c8daa835f07caef0ce31c67f9e9444753a30

Authored by Andrew Kane
1 parent 0dfa68fc

Created new methods to handle error and response

Showing 1 changed file with 48 additions and 41 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -67,48 +67,9 @@ module Searchkick
67 67 response = execute_search
68 68 end
69 69 rescue => e # TODO rescue type
70   - status_code = e.message[1..3].to_i
71   - if status_code == 404
72   - raise MissingIndexError, "Index missing - run #{reindex_command}"
73   - elsif status_code == 500 && (
74   - e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") ||
75   - e.message.include?("No query registered for [multi_match]") ||
76   - e.message.include?("[match] query does not support [cutoff_frequency]]") ||
77   - e.message.include?("No query registered for [function_score]]")
78   - )
79   -
80   - raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 1.0 or greater"
81   - elsif status_code == 400
82   - if e.message.include?("[multi_match] analyzer [searchkick_search] not found")
83   - raise InvalidQueryError, "Bad mapping - run #{reindex_command}"
84   - else
85   - raise InvalidQueryError, e.message
86   - end
87   - else
88   - raise e
89   - end
90   - end
91   -
92   - # apply facet limit in client due to
93   - # https://github.com/elasticsearch/elasticsearch/issues/1305
94   - @facet_limits.each do |field, limit|
95   - field = field.to_s
96   - facet = response["facets"][field]
97   - response["facets"][field]["terms"] = facet["terms"].first(limit)
98   - response["facets"][field]["other"] = facet["total"] - facet["terms"].sum { |term| term["count"] }
  70 + handle_error(e)
99 71 end
100   -
101   - opts = {
102   - page: @page,
103   - per_page: @per_page,
104   - padding: @padding,
105   - load: @load,
106   - includes: options[:include] || options[:includes],
107   - json: !options[:json].nil?,
108   - match_suffix: @match_suffix,
109   - highlighted_fields: @highlighted_fields || []
110   - }
111   - Searchkick::Results.new(searchkick_klass, response, opts)
  72 + handle_response(response)
112 73 end
113 74 end
114 75  
... ... @@ -125,6 +86,52 @@ module Searchkick
125 86  
126 87 private
127 88  
  89 + def handle_error(e)
  90 + status_code = e.message[1..3].to_i
  91 + if status_code == 404
  92 + raise MissingIndexError, "Index missing - run #{reindex_command}"
  93 + elsif status_code == 500 && (
  94 + e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") ||
  95 + e.message.include?("No query registered for [multi_match]") ||
  96 + e.message.include?("[match] query does not support [cutoff_frequency]]") ||
  97 + e.message.include?("No query registered for [function_score]]")
  98 + )
  99 +
  100 + raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 1.0 or greater"
  101 + elsif status_code == 400
  102 + if e.message.include?("[multi_match] analyzer [searchkick_search] not found")
  103 + raise InvalidQueryError, "Bad mapping - run #{reindex_command}"
  104 + else
  105 + raise InvalidQueryError, e.message
  106 + end
  107 + else
  108 + raise e
  109 + end
  110 + end
  111 +
  112 + def handle_response(response)
  113 + # apply facet limit in client due to
  114 + # https://github.com/elasticsearch/elasticsearch/issues/1305
  115 + @facet_limits.each do |field, limit|
  116 + field = field.to_s
  117 + facet = response["facets"][field]
  118 + response["facets"][field]["terms"] = facet["terms"].first(limit)
  119 + response["facets"][field]["other"] = facet["total"] - facet["terms"].sum { |term| term["count"] }
  120 + end
  121 +
  122 + opts = {
  123 + page: @page,
  124 + per_page: @per_page,
  125 + padding: @padding,
  126 + load: @load,
  127 + includes: options[:include] || options[:includes],
  128 + json: !options[:json].nil?,
  129 + match_suffix: @match_suffix,
  130 + highlighted_fields: @highlighted_fields || []
  131 + }
  132 + Searchkick::Results.new(searchkick_klass, response, opts)
  133 + end
  134 +
128 135 def reindex_command
129 136 searchkick_klass ? "#{searchkick_klass.name}.reindex" : "reindex"
130 137 end
... ...