Class: Stretcher::Index

Inherits:
EsComponent show all
Defined in:
lib/stretcher/index.rb

Overview

Represents an Index context in elastic search. Generally should be instantiated via Server#index(name).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from EsComponent

#do_alias, #do_delete_query, #do_refresh, #do_search, #request

Constructor Details

#initialize(server, name, options = {}) ⇒ Index

Returns a new instance of Index.



8
9
10
11
12
# File 'lib/stretcher/index.rb', line 8

def initialize(server, name, options={})
  @server = server
  @name = name
  @logger = options[:logger] || server.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/stretcher/index.rb', line 6

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/stretcher/index.rb', line 6

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/stretcher/index.rb', line 6

def server
  @server
end

Instance Method Details

#alias(name, &block) ⇒ Object

Returns a Stretcher::Alias object for the alias name. Optionally takes a block, which will be passed a single arg with the Alias obj The block syntax returns the evaluated value of the block

my_server.alias('user_1') # Stretcher::Alias
my_server.alias { |alias| 1 } # 1


31
32
33
34
# File 'lib/stretcher/index.rb', line 31

def alias(name, &block)
  al = Alias.new(self, name, :logger => logger)
  block ? block.call(al) : al
end

#analyze(text, analysis_params) ⇒ Object

Implements the Analyze API EX:

index.analyze("Candles", analyzer: :snowball)
# => #<Hashie::Mash tokens=[#<Hashie::Mash end_offset=7 position=1 start_offset=0 token="candl" type="<ALPHANUM>">]>


162
163
164
165
166
# File 'lib/stretcher/index.rb', line 162

def analyze(text, analysis_params)
  request(:get, "_analyze", analysis_params) do |req|
    req.body = text
  end
end

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

Perform a raw bulk operation. You probably want to use Stretcher::Index#bulk_index which properly formats a bulk index request.



192
193
194
# File 'lib/stretcher/index.rb', line 192

def bulk(data, options={})
  request(:post, "_bulk", options, data)
end

#bulk_action(action, documents, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stretcher/index.rb', line 52

def bulk_action(action, documents, options={})
  action=action.to_sym
  
  body = documents.reduce("") {|post_data, d_raw|
    d = Hashie::Mash.new(d_raw)
    index_meta = { :_id => (d[:id] || d.delete(:_id)) }

    system_fields = %w{_type _parent _routing}
    d.keys.reduce(index_meta) do |memo, key|
      index_meta[key] = d.delete(key) if system_fields.include?(key.to_s)
    end

    post_data << (MultiJson.dump({action => index_meta}) << "\n")
    post_data << (MultiJson.dump(d) << "\n") unless action == :delete
    post_data
  }
  bulk body, options
end

#bulk_delete(documents, options = {}) ⇒ Object

Given a hash of documents, will bulk delete

docs = [{"_type" => "tweet", "_id" => 91011}]
server.index(:foo).bulk_delete(docs)


48
49
50
# File 'lib/stretcher/index.rb', line 48

def bulk_delete(documents, options={})
  bulk_action(:delete, documents, options)
end

#bulk_index(documents, options = {}) ⇒ Object

Given a hash of documents, will bulk index

docs = [{"_type" => "tweet", "_id" => 91011, "text" => "Bulked"}]
server.index(:foo).bulk_index(docs)


40
41
42
# File 'lib/stretcher/index.rb', line 40

def bulk_index(documents, options={})
  bulk_action(:index, documents, options)
end

#create(options = nil) ⇒ Object

Creates the index, with the supplied hash as the options body (usually mappings: and settings:))



72
73
74
# File 'lib/stretcher/index.rb', line 72

def create(options=nil)
  request(:put, nil, nil, options)
end

#deleteObject

Deletes the index



77
78
79
# File 'lib/stretcher/index.rb', line 77

def delete
  request :delete
end

#delete_percolator_query(query_name) ⇒ Object

Deletes a percolate query from the index www.elasticsearch.org/guide/reference/api/percolate/



186
187
188
# File 'lib/stretcher/index.rb', line 186

def delete_percolator_query(query_name)
  server.request(:delete, percolator_query_path(query_name))
end

#delete_query(query) ⇒ Object

Delete documents by a given query. Per: www.elasticsearch.org/guide/reference/api/delete-by-query.html



124
125
126
# File 'lib/stretcher/index.rb', line 124

def delete_query(query)
  do_delete_query(query)
end

#exists?Boolean

Check if the index has been created on the remote server

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/stretcher/index.rb', line 114

def exists?
  # Unless the exception is hit we know its a 2xx response
  request(:head)
  true
rescue Stretcher::RequestError::NotFound
  false
end

#get_mappingObject

Retrieve the mapping for this index



99
100
101
# File 'lib/stretcher/index.rb', line 99

def get_mapping
  request :get, "_mapping"
end

#get_settingsObject

Retrieve settings for this index



104
105
106
# File 'lib/stretcher/index.rb', line 104

def get_settings
  request :get, "_settings"
end

#mget(meta_docs) ⇒ Object

Takes a collection of hashes of the form => ‘foo’, :_id => 123 And issues an mget for them within the current index



83
84
85
86
# File 'lib/stretcher/index.rb', line 83

def mget(meta_docs)
  merge_data = {:_index => name}
  @server.mget(meta_docs.map {|d| d.merge(merge_data) })
end

#msearch(queries = []) ⇒ Object

Searches a list of queries against only this index This deviates slightly from the official API in that ONLY queries are requried, the empty {} preceding them are not See: www.elasticsearch.org/guide/reference/api/multi-search.html

server.index(:foo).msearch([{query: {match_all: {}}}])
# => Returns an array of Stretcher::SearchResults

Raises:

  • (ArgumentError)


148
149
150
151
152
153
154
155
156
# File 'lib/stretcher/index.rb', line 148

def msearch(queries=[])
  raise ArgumentError, "msearch takes an array!" unless queries.is_a?(Array)
  req_body = queries.reduce([]) {|acc,q|
    acc << {:index => name}
    acc << q
    acc
  }
  @server.msearch req_body
end

#optimize(options = nil) ⇒ Object

Perform an optimize on the index to merge and reduce the number of segments



174
175
176
# File 'lib/stretcher/index.rb', line 174

def optimize(options=nil)
  request(:post, "_optimize", options)
end

#path_uri(path = "/") ⇒ Object

Full path to this index



204
205
206
207
# File 'lib/stretcher/index.rb', line 204

def path_uri(path="/")
  p = @server.path_uri("/#{name}")
  path ? p << "/#{path}" : p
end

#refreshObject

Perform a refresh making all items in this index available instantly



169
170
171
# File 'lib/stretcher/index.rb', line 169

def refresh
  do_refresh
end

#register_percolator_query(query_name, options = {}) ⇒ Object

Registers a percolate query against the index www.elasticsearch.org/guide/reference/api/percolate/



180
181
182
# File 'lib/stretcher/index.rb', line 180

def register_percolator_query(query_name, options = {})
  server.request(:put, percolator_query_path(query_name), nil, options)
end

#search(generic_opts = {}, explicit_body = nil) ⇒ Object

Issues a search with the given query opts and body, both should be hashes

res = server.index('foo').search(size: 12, {query: {match_all: {}}})
es.class   # Stretcher::SearchResults
res.total   # => 1
res.facets  # => nil
res.results # => [#<Hashie::Mash _id="123" text="Hello">]
res.raw     # => #<Hashie::Mash ...> Raw JSON from the search


136
137
138
139
# File 'lib/stretcher/index.rb', line 136

def search(generic_opts={}, explicit_body=nil)
  # Written this way to be more RDoc friendly
  do_search(generic_opts, explicit_body)
end

#statsObject

Retrieves stats for this index



89
90
91
# File 'lib/stretcher/index.rb', line 89

def stats
  request :get, "_stats"
end

#statusObject

Retrieves status for this index



94
95
96
# File 'lib/stretcher/index.rb', line 94

def status
  request :get, "_status"
end

#suggest(name, text, completion = {}) ⇒ Object

Takes the name, text, and completion options to craft a completion query. suggest(“band_complete”, “a”, field: :suggest) Use the new completion suggest API per www.elasticsearch.org/guide/reference/api/search/completion-suggest/



199
200
201
# File 'lib/stretcher/index.rb', line 199

def suggest(name, text, completion={})
  request(:post, "_suggest", nil, {name => {:text => text, :completion => completion}})
end

#type(name, &block) ⇒ Object

Returns a Stretcher::IndexType object for the type name. Optionally takes a block, which will be passed a single arg with the Index obj The block syntax returns the evaluated value of the block

my_index.index(:foo) # => #<Stretcher::Index ...>
my_index.index(:foo) {|idx| 1+1} # => 2


20
21
22
23
# File 'lib/stretcher/index.rb', line 20

def type(name, &block)
  t = IndexType.new(self, name)
  block ? block.call(t) : t
end

#update_settings(settings) ⇒ Object

Update settings for this index



109
110
111
# File 'lib/stretcher/index.rb', line 109

def update_settings(settings)
  request :put, "_settings", nil, settings
end