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



84
85
86
87
88
89
# File 'lib/elasticsearch/transport/base_protocol.rb', line 84

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/elasticsearch/transport/base_protocol.rb', line 73

def count(index, type, query, options={})
  if query.is_a?(Hash)
    # patron 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



26
27
28
29
30
# File 'lib/elasticsearch/transport/base_protocol.rb', line 26

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/elasticsearch/transport/base_protocol.rb', line 32

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



15
16
17
18
19
20
21
22
23
24
# File 'lib/elasticsearch/transport/base_protocol.rb', line 15

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



4
5
6
7
8
9
10
11
12
13
# File 'lib/elasticsearch/transport/base_protocol.rb', line 4

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/elasticsearch/transport/base_protocol.rb', line 92

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/elasticsearch/transport/base_protocol.rb', line 60

def scroll(scroll_id, options={})
  # patron 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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elasticsearch/transport/base_protocol.rb', line 43

def search(index, type, query, options={})
  if query.is_a?(Hash)
    # patron 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 => "_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