Module: ElasticSearch::Api::Index
- Included in:
- Client
- Defined in:
- lib/elasticsearch/client/index.rb
Instance Method Summary collapse
-
#bulk(options = {}) ⇒ Object
Starts a bulk operation batch and yields self.
-
#count(query, options = {}) ⇒ Object
df The default field to use when no field prefix is defined within the query.
- #delete(id, options = {}) ⇒ Object
- #delete_by_query(query, options = {}) ⇒ Object
- #get(id, options = {}) ⇒ Object
- #index(document, options = {}) ⇒ Object
-
#multi_get(ids, options = {}) ⇒ Object
minor multi get support.
-
#scroll(scroll_id, options = {}, &block) ⇒ Object
ids_only Return ids instead of hits pass a block to execute the block for each batch of hits.
-
#search(query, options = {}) ⇒ Object
df The default field to use when no field prefix is defined within the query.
Instance Method Details
#bulk(options = {}) ⇒ Object
Starts a bulk operation batch and yields self. Index and delete requests will be queued until the block closes, then sent as a single _bulk call.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/elasticsearch/client/index.rb', line 116 def bulk(={}) # allow nested bulk calls if @batch yield(self) else begin @batch = [] yield(self) response = execute(:bulk, @batch, ) ensure @batch = nil end end end |
#count(query, options = {}) ⇒ Object
df The default field to use when no field prefix is defined within the query. analyzer The analyzer name to be used when analyzing the query string. default_operator The default operator to be used, can be AND or OR. Defaults to OR.
107 108 109 110 111 112 |
# File 'lib/elasticsearch/client/index.rb', line 107 def count(query, ={}) index, type, = extract_scope() response = execute(:count, index, type, query, ) response["count"].to_i #TODO check if count is nil end |
#delete(id, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/elasticsearch/client/index.rb', line 43 def delete(id, ={}) index, type, = extract_required_scope() if @batch #TODO add routing, parent @batch << { :delete => { :_index => index, :_type => type, :_id => id }} else result = execute(:delete, index, type, id, ) result["ok"] end end |
#delete_by_query(query, options = {}) ⇒ Object
55 56 57 58 |
# File 'lib/elasticsearch/client/index.rb', line 55 def delete_by_query(query, = {}) index, type, = extract_scope() execute(:delete_by_query, index || '_all', type, query, ) end |
#get(id, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/elasticsearch/client/index.rb', line 30 def get(id, ={}) index, type, = extract_required_scope() # index # type # id # fields hit = execute(:get, index, type, id, ) if hit Hit.new(hit) end end |
#index(document, options = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/elasticsearch/client/index.rb', line 6 def index(document, ={}) index, type, = extract_required_scope() # type # index # id (optional) # op_type (optional) # timeout (optional) # document (optional) id = .delete(:id) if @batch #TODO add routing, parent @batch << { :index => { :_index => index, :_type => type, :_id => id }.merge()} @batch << document else result = execute(:index, index, type, id, document, ) if result["ok"] result["_id"] else false end end end |
#multi_get(ids, options = {}) ⇒ Object
minor multi get support
132 133 134 135 136 137 138 139 140 |
# File 'lib/elasticsearch/client/index.rb', line 132 def multi_get(ids, ={}) index, type, = extract_required_scope() results = execute(:multi_get, index, type, ids, ) if(results) hits = [] results.each { |hit| hits << Hit.new(hit) } hits end end |
#scroll(scroll_id, options = {}, &block) ⇒ Object
ids_only Return ids instead of hits pass a block to execute the block for each batch of hits
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/elasticsearch/client/index.rb', line 91 def scroll(scroll_id, ={}, &block) begin = .reject { |k, v| [:page, :per_page, :ids_only, :limit, :offset].include?(k) } response = execute(:scroll, scroll_id, ) hits = Hits.new(response, { :ids_only => [:ids_only] }) if block_given? && !hits.empty? yield hits scroll_id = hits.scroll_id end end until !block_given? || hits.empty? hits end |
#search(query, options = {}) ⇒ Object
df The default field to use when no field prefix is defined within the query. analyzer The analyzer name to be used when analyzing the query string. default_operator The default operator to be used, can be AND or OR. Defaults to OR. explain For each hit, contain an explanation of how to scoring of the hits was computed. fields The selective fields of the document to return for each hit (fields must be stored), comma delimited. Defaults to the internal _source field. field Same as fields above, but each parameter contains a single field name to load. There can be several field parameters. sort Sorting to perform. Can either be in the form of fieldName, or fieldName:desc (for reverse sorting). The fieldName can either be an actual field within the document, or the special score name to indicate sorting based on scores. There can be several sort parameters (order is important). from The starting from index of the hits to return. Defaults to 0. size The number of hits to return. Defaults to 10. search_type The type of the search operation to perform. Can be dfs_query_then_fetch, dfs_query_and_fetch, query_then_fetch, query_and_fetch. Defaults to query_then_fetch. scroll Get a scroll id to continue paging through the search results. Value is the time to keep a scroll request around, e.g. 5m ids_only Return ids instead of hits
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/elasticsearch/client/index.rb', line 72 def search(query, ={}) index, type, = extract_scope() [:size] ||= ([:per_page] || [:limit]) if [:per_page] || [:limit] [:from] ||= [:size] * ([:page].to_i-1) if [:page] && [:page].to_i > 1 [:from] ||= [:offset] if [:offset] [:fields] = "_id" if [:ids_only] # options that elasticsearch doesn't recognize: page, per_page, ids_only, limit, offset = .reject { |k, v| [:page, :per_page, :ids_only, :limit, :offset].include?(k) } response = execute(:search, index, type, query, ) Hits.new(response, {:per_page => [:per_page], :page => [:page], :ids_only => [:ids_only]}) #ids_only returns array of ids instead of hits end |