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
Returns a new instance of 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
64 65 66 |
# File 'lib/mongoid/elasticsearch/es.rb', line 64 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
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mongoid/elasticsearch/es.rb', line 96 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
92 93 94 |
# File 'lib/mongoid/elasticsearch/es.rb', line 92 def completion_supported? @version > Gem::Version.new('0.90.2') end |
#custom_type_options(options) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/mongoid/elasticsearch/es.rb', line 72 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 41 42 43 44 45 46 |
# File 'lib/mongoid/elasticsearch/es.rb', line 21 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()) if block_given? yield steps, step end end end |
#index_item(obj) ⇒ Object
84 85 86 |
# File 'lib/mongoid/elasticsearch/es.rb', line 84 def index_item(obj) client.index({body: obj.as_indexed_json}.merge((obj))) end |
#options_for(obj) ⇒ Object
68 69 70 |
# File 'lib/mongoid/elasticsearch/es.rb', line 68 def (obj) {id: obj.id.to_s}.merge end |
#remove_item(obj) ⇒ Object
88 89 90 |
# File 'lib/mongoid/elasticsearch/es.rb', line 88 def remove_item(obj) client.delete((obj).merge(ignore: 404)) end |
#search(query, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mongoid/elasticsearch/es.rb', line 48 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, [:scope] || klass, ) end |
#type_options ⇒ Object
80 81 82 |
# File 'lib/mongoid/elasticsearch/es.rb', line 80 def {index: index.name, type: index.type} end |