module ElasticSearch::Transport::IndexProtocol
Public Instance Methods
bulk(actions, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 86 def bulk(actions, options={}) body = actions.inject("") { |a, s| a << encoder.encode(s) << "\n" } response = request(:post, {:op => '_bulk'}, options, body) handle_error(response) unless response.status == 200 encoder.decode(response.body) # {"items => [ {"delete"/"create" => {"_index", "_type", "_id", "ok"}} ] } end
count(index, type, query, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 75 def count(index, type, query, options={}) if query.is_a?(Hash) # Some http libraries cannot submit get requests with content, so if query is a hash, post it instead (assume a query hash is using the query dsl) response = request(:post, {:index => index, :type => type, :op => "_count"}, options, encoder.encode(query)) else response = request(:get, {:index => index, :type => type, :op => "_count"}, options.merge(:q => query)) end handle_error(response) unless response.status == 200 encoder.decode(response.body) # {"count", "_shards"=>{"failed", "total", "successful"}} end
delete(index, type, id, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 28 def delete(index, type, id, options={}) response = request(:delete,{:index => index, :type => type, :id => id}, options) handle_error(response) unless response.status == 200 # ElasticSearch always returns 200 on delete, even if the object doesn't exist encoder.decode(response.body) end
delete_by_query(index, type, query, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 34 def delete_by_query(index, type, query, options={}) # pass the query through the parameters in all the cases, since # DELETE with a body are ambiguously supported if query.is_a?(Hash) params = options.merge(:source => encoder.encode(query)) else params = options.merge(:q => query) end request(:delete, {:index => index, :type => type, :op => "_query"}, params) end
get(index, type, id, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 17 def get(index, type, id, options={}) response = request(:get, {:index => index, :type => type, :id => id}, options) return nil if response.status == 404 handle_error(response) unless response.status == 200 hit = encoder.decode(response.body) unescape_id!(hit) #TODO extract these two calls from here and search set_encoding!(hit) hit # { "_id", "_index", "_type", "_source" } end
index(index, type, id, document, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 6 def index(index, type, id, document, options={}) body = encoder.is_encoded?(document) ? document : encoder.encode(document) if id.nil? response = request(:post, {:index => index, :type => type}, options, body) else response = request(:put, {:index => index, :type => type, :id => id}, options, body) end handle_error(response) unless (response.status == 200 or response.status == 201) encoder.decode(response.body) end
multi_get(index, type, query, options={})
click to toggle source
Uses a post request so we can send ids in content
# File lib/elasticsearch/transport/base_protocol.rb, line 94 def multi_get(index, type, query, options={}) # { "docs" = [ {...}, {...}, ...]} if query.is_a?(Array) query = { "ids" => query } end results = standard_request(:post, { :index => index, :type => type, :op => "_mget"}, options, encoder.encode(query))['docs'] results.each do |hit| unescape_id!(hit) set_encoding!(hit) end results end
scroll(scroll_id, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 62 def scroll(scroll_id, options={}) # Some http libraries cannot submit get requests with content, so we pass the scroll_id in the parameters response = request(:get, {:op => "_search/scroll"}, options.merge(:scroll_id => scroll_id)) handle_error(response) unless response.status == 200 results = encoder.decode(response.body) # unescape ids results["hits"]["hits"].each do |hit| unescape_id!(hit) set_encoding!(hit) end results # {"hits"=>{"hits"=>[{"_id", "_type", "_source", "_index", "_score"}], "total"}, "_shards"=>{"failed", "total", "successful"}, "_scrollId"} end
search(index, type, query, options={})
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 45 def search(index, type, query, options={}) if query.is_a?(Hash) # Some http libraries cannot submit get requests with content, so if query is a hash, post it instead (assume a query hash is using the query dsl) response = request(:get, {:index => index, :type => type, :op => "_search"}, options, encoder.encode(query)) else response = request(:get, {:index => index, :type => type, :op => "_search"}, options.merge(:q => query)) end handle_error(response) unless response.status == 200 results = encoder.decode(response.body) # unescape ids results["hits"]["hits"].each do |hit| unescape_id!(hit) set_encoding!(hit) end results # {"hits"=>{"hits"=>[{"_id", "_type", "_source", "_index", "_score"}], "total"}, "_shards"=>{"failed", "total", "successful"}} end