lib/search_flip/criteria.rb in search_flip-4.0.0.beta8 vs lib/search_flip/criteria.rb in search_flip-4.0.0.beta9
- old
+ new
@@ -24,11 +24,12 @@
include Aggregatable
extend Forwardable
attr_accessor :target, :profile_value, :source_value, :suggest_values, :includes_values,
:eager_load_values, :preload_values, :failsafe_value, :scroll_args, :terminate_after_value,
- :timeout_value, :preference_value, :search_type_value, :routing_value, :track_total_hits_value
+ :timeout_value, :preference_value, :search_type_value, :routing_value, :track_total_hits_value,
+ :http_timeout_value
# Creates a new criteria while merging the attributes (constraints,
# settings, etc) of the current criteria with the attributes of another one
# passed as argument. For multi-value contstraints the resulting criteria
# will include constraints of both criterias. For single-value constraints,
@@ -45,11 +46,11 @@
fresh.tap do |criteria|
[
:profile_value, :failsafe_value, :terminate_after_value, :timeout_value, :offset_value,
:limit_value, :scroll_args, :source_value, :preference_value, :search_type_value,
- :routing_value, :track_total_hits_value, :explain_value
+ :routing_value, :track_total_hits_value, :explain_value, :http_timeout_value
].each do |name|
criteria.send(:"#{name}=", other.send(name)) unless other.send(name).nil?
end
[
@@ -146,10 +147,26 @@
fresh.tap do |criteria|
criteria.timeout_value = value
end
end
+ # Specifies a http timeout, such that a SearchFlip::TimeoutError will be
+ # thrown when the request times out.
+ #
+ # @example
+ # ProductIndex.http_timeout(3).search("hello world")
+ #
+ # @param value [Fixnum] The timeout value
+ #
+ # @return [SearchFlip::Criteria] A newly created extended criteria
+
+ def http_timeout(value)
+ fresh.tap do |criteria|
+ criteria.http_timeout_value = value
+ end
+ end
+
# Specifies early query termination, such that the processing will be
# stopped after the specified number of results has been accumulated.
#
# @example
# ProductIndex.terminate_after(10_000).search("hello world")
@@ -328,14 +345,17 @@
def delete(params = {})
dupped_request = request.dup
dupped_request.delete(:from)
dupped_request.delete(:size)
+ http_request = connection.http_client
+ http_request = http_request.timeout(http_timeout_value) if http_timeout_value
+
if connection.version.to_i >= 5
- connection.http_client.post("#{target.type_url}/_delete_by_query", params: request_params.merge(params), json: dupped_request)
+ http_request.post("#{target.type_url}/_delete_by_query", params: request_params.merge(params), json: dupped_request)
else
- connection.http_client.delete("#{target.type_url}/_query", params: request_params.merge(params), json: dupped_request)
+ http_request.delete("#{target.type_url}/_query", params: request_params.merge(params), json: dupped_request)
end
target.refresh if SearchFlip::Config[:auto_refresh]
true
@@ -499,12 +519,12 @@
end
end
end
# Executes the search request for the current criteria, ie sends the
- # request to Elasticsearch and returns the response. Connection and
- # response errors will be rescued if you specify the criteria to be
+ # request to Elasticsearch and returns the response. Connection, timeout
+ # and response errors will be rescued if you specify the criteria to be
# #failsafe, such that an empty response is returned instead.
#
# @example
# response = CommentIndex.search("hello world").execute
#
@@ -588,10 +608,11 @@
private
def execute!
http_request = connection.http_client.headers(accept: "application/json")
+ http_request = http_request.timeout(http_timeout_value) if http_timeout_value
http_response =
if scroll_args && scroll_args[:id]
http_request.post(
"#{connection.base_url}/_search/scroll",
@@ -607,10 +628,10 @@
else
http_request.post("#{target.type_url}/_search", params: request_params, json: request)
end
SearchFlip::Response.new(self, SearchFlip::JSON.parse(http_response.to_s))
- rescue SearchFlip::ConnectionError, SearchFlip::ResponseError => e
+ rescue SearchFlip::ConnectionError, SearchFlip::TimeoutError, SearchFlip::ResponseError => e
raise e unless failsafe_value
SearchFlip::Response.new(self, "took" => 0, "hits" => { "total" => 0, "hits" => [] })
end