Module: ElasticSearchable

Includes:
HTTParty
Defined in:
lib/elastic_searchable.rb,
lib/elastic_searchable/version.rb,
lib/elastic_searchable/paginator.rb,
lib/elastic_searchable/pagination/kaminari.rb,
lib/elastic_searchable/active_record_extensions.rb,
lib/elastic_searchable/pagination/will_paginate.rb

Defined Under Namespace

Modules: ActiveRecordExtensions, Pagination Classes: ElasticError, Paginator

Constant Summary collapse

VERSION =
'3.0.2'
@@offline =
false

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.index_nameObject

Returns the value of attribute index_name.



13
14
15
# File 'lib/elastic_searchable.rb', line 13

def index_name
  @index_name
end

.index_settingsObject

Returns the value of attribute index_settings.



13
14
15
# File 'lib/elastic_searchable.rb', line 13

def index_settings
  @index_settings
end

.loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/elastic_searchable.rb', line 13

def logger
  @logger
end

Class Method Details

.backgrounded_optionsObject

options for automatic active record indexing



47
48
49
# File 'lib/elastic_searchable.rb', line 47

def backgrounded_options
  {:queue => 'elasticsearch'}
end

.create_indexObject



58
59
60
61
62
# File 'lib/elastic_searchable.rb', line 58

def create_index
  options = {}
  options[:settings] = self.index_settings if self.index_settings
  self.request :put, self.request_path, :json_body => options
end

.delete_indexObject



74
75
76
# File 'lib/elastic_searchable.rb', line 74

def delete_index
  self.request :delete, self.request_path
end

.encode_json(options = {}) ⇒ Object

encapsulate encoding hash into json string support Yajl encoder if installed



29
30
31
# File 'lib/elastic_searchable.rb', line 29

def encode_json(options = {})
  MultiJson.encode options
end

.escape_query(string) ⇒ Object

escape lucene special characters



52
53
54
# File 'lib/elastic_searchable.rb', line 52

def escape_query(string)
  string.to_s.gsub(/([\(\)\[\]\{\}\?\\\"!\^\+\-\*:~])/,'\\\\\1')
end

.offline(&block) ⇒ Object

execute a block of work without reindexing objects



17
18
19
20
21
22
23
# File 'lib/elastic_searchable.rb', line 17

def offline(&block)
  original_value = offline?
  @@offline = true
  yield
ensure
  @@offline = original_value
end

.offline?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/elastic_searchable.rb', line 24

def offline?
  !!@@offline
end

.refresh_indexObject

explicitly refresh the index, making all operations performed since the last refresh available for search

www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/refresh/



68
69
70
# File 'lib/elastic_searchable.rb', line 68

def refresh_index
  self.request :post, self.request_path('_refresh')
end

.request(method, url, options = {}) ⇒ Object

perform a request to the elasticsearch server configuration: ElasticSearchable.base_uri ‘host:port’ controls where to send request to ElasticSearchable.debug_output outputs all http traffic to console



36
37
38
39
40
41
42
43
44
# File 'lib/elastic_searchable.rb', line 36

def request(method, url, options = {})
  options.merge! :headers => {'Content-Type' => 'application/json'}
  options.merge! :body => self.encode_json(options.delete(:json_body)) if options[:json_body]

  response = self.send(method, url, options)
  logger.debug "elasticsearch request: #{method} #{url} #{"took #{response['took']}ms" if response['took']}"
  validate_response response
  response
end

.request_path(action = nil) ⇒ Object

helper method to generate elasticsearch url for this index



79
80
81
# File 'lib/elastic_searchable.rb', line 79

def request_path(action = nil)
  ['', index_name, action].compact.join('/')
end