Module: ElasticSearch::Transport::IndexProtocol

Included in:
BaseProtocol
Defined in:
lib/elasticsearch/transport/base_protocol.rb

Instance Method Summary collapse

Instance Method Details

#bulk(actions, options = {}) ⇒ Object



86
87
88
89
90
91
# 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 = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# 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 = {}) ⇒ Object



28
29
30
31
32
# 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 = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# 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 = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# 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 = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# 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 = {}) ⇒ Object

Uses a post request so we can send ids in content



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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 = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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 = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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