Class: Mongoid::Elasticsearch::Es
- Inherits:
-
Object
- Object
- Mongoid::Elasticsearch::Es
- Defined in:
- lib/mongoid/elasticsearch/es.rb
Constant Summary collapse
- INDEX_STEP =
100
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #client ⇒ Object
- #completion(text, field = "suggest") ⇒ Object
- #completion_supported? ⇒ Boolean
- #custom_type_options(options) ⇒ Object
- #index ⇒ Object
- #index_all(step_size = INDEX_STEP) ⇒ Object
- #index_item(obj) ⇒ Object
-
#initialize(klass) ⇒ Es
constructor
A new instance of Es.
- #options_for(obj) ⇒ Object
- #remove_item(obj) ⇒ Object
- #search(query, options = {}) ⇒ Object
- #type_options ⇒ Object
Constructor Details
#initialize(klass) ⇒ Es
7 8 9 10 |
# File 'lib/mongoid/elasticsearch/es.rb', line 7 def initialize(klass) @klass = klass @version = Gem::Version.new(client.info['version']['number']) end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/mongoid/elasticsearch/es.rb', line 5 def klass @klass end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
5 6 7 |
# File 'lib/mongoid/elasticsearch/es.rb', line 5 def version @version end |
Instance Method Details
#all(options = {}) ⇒ Object
58 59 60 |
# File 'lib/mongoid/elasticsearch/es.rb', line 58 def all( = {}) search({body: {query: {match_all: {}}}}, ) end |
#client ⇒ Object
12 13 14 15 |
# File 'lib/mongoid/elasticsearch/es.rb', line 12 def client # dup is needed because Elasticsearch::Client.new changes options hash inplace @client ||= ::Elasticsearch::Client.new klass..dup end |
#completion(text, field = "suggest") ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongoid/elasticsearch/es.rb', line 90 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? ⇒ Boolean
86 87 88 |
# File 'lib/mongoid/elasticsearch/es.rb', line 86 def completion_supported? @version > Gem::Version.new('0.90.2') end |
#custom_type_options(options) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/mongoid/elasticsearch/es.rb', line 66 def () if ![:include_type].nil? && [:include_type] == false {index: index.name} else end end |
#index ⇒ Object
17 18 19 |
# File 'lib/mongoid/elasticsearch/es.rb', line 17 def index @index ||= Index.new(self) end |
#index_all(step_size = INDEX_STEP) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongoid/elasticsearch/es.rb', line 21 def index_all(step_size = INDEX_STEP) index.reset q = klass.order_by(_id: 1) steps = (q.count / step_size) + 1 steps.times do |step| docs = q.skip(step * step_size).limit(step_size) 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()) if block_given? yield steps, step end end end |
#index_item(obj) ⇒ Object
78 79 80 |
# File 'lib/mongoid/elasticsearch/es.rb', line 78 def index_item(obj) client.index({body: obj.as_indexed_json}.merge((obj))) end |
#options_for(obj) ⇒ Object
62 63 64 |
# File 'lib/mongoid/elasticsearch/es.rb', line 62 def (obj) {id: obj.id.to_s}.merge end |
#remove_item(obj) ⇒ Object
82 83 84 |
# File 'lib/mongoid/elasticsearch/es.rb', line 82 def remove_item(obj) client.delete((obj).merge(ignore: 404)) end |
#search(query, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mongoid/elasticsearch/es.rb', line 42 def search(query, = {}) if query.is_a?(String) query = {q: Utils.clean(query)} end page = [:page] per_page = [:per_page].nil? ? [:per] : [: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 [:wrapper] ||= klass.es_wrapper Response.new(client, query.merge(()), false, klass, ) end |
#type_options ⇒ Object
74 75 76 |
# File 'lib/mongoid/elasticsearch/es.rb', line 74 def {index: index.name, type: index.type} end |