Module: ElasticSearch::Transport::IndexProtocol

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

Instance Method Summary collapse

Instance Method Details

#count(index, type, query, options = {}) ⇒ Object



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

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})
  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

#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})
  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}, {}, body)
  else
    response = request(:put, {:index => index, :type => type, :id => id}, {}, body)
  end
  handle_error(response) unless response.status == 200
  encoder.decode(response.body)
end

#scroll(scroll_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/elasticsearch/transport/base_protocol.rb', line 49

def scroll(scroll_id)
  response = request(:get, {:op => "_search/scroll"}, {: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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/elasticsearch/transport/base_protocol.rb', line 32

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