class Mongoid::Elasticsearch::Es
Constants
- INDEX_STEP
Attributes
klass[R]
version[R]
Public Class Methods
new(klass)
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 11 def initialize(klass) @klass = klass @version = Gem::Version.new(client.info['version']['number']) end
Public Instance Methods
all(options = {})
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 78 def all(options = {}) search({body: {query: {match_all: {}}}}, options) end
client()
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 16 def client if klass.es_create_aws_client @client ||= ::Elasticsearch::Client.new(host: ENV["AWS_ES_HOST"], port: ENV["AWS_ES_PORT"]) do |f| f.request :aws_sigv4, credentials: Aws::Credentials.new(ENV["AWS_KEY"],ENV["AWS_SECRET"]), service: ENV["AWS_ES_SERVICE"], region: ENV["AWS_ES_REGION"] f.response :logger f.adapter Faraday.default_adapter end else @client ||= ::Elasticsearch::Client.new klass.es_client_options.dup end end
completion(text, field = "suggest")
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 123 def completion(text, field = "suggest") raise "Completion not supported in ES #{@version}" unless completion_supported? body = { q: { text: Utils.clean(text), completion: { field: field } } } results = client.suggest(index: index.name, body: body) results['q'][0]['options'] end
completion_supported?()
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 119 def completion_supported? @version > Gem::Version.new('0.90.2') end
custom_type_options(options)
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 87 def custom_type_options(options) if !options[:include_type].nil? && options[:include_type] == false {index: index.name} else type_options end end
index()
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 31 def index @index ||= Index.new(self) end
index_all(step_size = INDEX_STEP) { |steps, step| ... }
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 35 def index_all(step_size = INDEX_STEP) index.reset q = klass.asc(:id) steps = (q.count / step_size) + 1 last_id = nil steps.times do |step| if last_id docs = q.gt(id: last_id).limit(step_size).to_a else docs = q.limit(step_size).to_a end last_id = docs.last.try(:id) docs = docs.map do |obj| if obj.es_index? { index: {data: obj.as_indexed_json}.merge(_id: obj.id.to_s) } else nil end end.reject { |obj| obj.nil? } next if docs.empty? client.bulk({body: docs}.merge(type_options)) if block_given? yield steps, step end end end
index_item(obj)
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 99 def index_item(obj) response = client.index({body: obj.as_indexed_json}.merge(options_for(obj))) end
options_for(obj)
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 83 def options_for(obj) {id: obj.id.to_s}.merge type_options end
remove_item(obj)
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 115 def remove_item(obj) client.delete(options_for(obj).merge(ignore: 404)) end
search(query, options = {})
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 62 def search(query, options = {}) if query.is_a?(String) query = {q: Utils.clean(query)} end page = options[:page] per_page = options[:per_page].nil? ? options[:per] : options[:per_page] query[:size] = ( per_page.to_i ) if per_page query[:from] = ( page.to_i <= 1 ? 0 : (per_page.to_i * (page.to_i-1)) ) if page && per_page options[:wrapper] ||= klass.es_wrapper Response.new(client, query.merge(custom_type_options(options)), false, klass, options) end
type_options()
click to toggle source
# File lib/mongoid/elasticsearch/es.rb, line 95 def type_options {index: index.name, type: index.type} end
update_item(obj)
click to toggle source
ADDED THIS METHOD TO UPDATE A RECORD IN THE DATABASE.
# File lib/mongoid/elasticsearch/es.rb, line 111 def update_item(obj) end