Class: Mongoid::Elasticsearch::Es

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/elasticsearch/es.rb

Constant Summary collapse

INDEX_STEP =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/mongoid/elasticsearch/es.rb', line 5

def klass
  @klass
end

#versionObject (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(options = {})
  search({body: {query: {match_all: {}}}}, options)
end

#clientObject



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.es_client_options.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 custom_type_options(options)
  if !options[:include_type].nil? && options[:include_type] == false
    {index: index.name}
  else
    type_options
  end
end

#indexObject



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(type_options))
    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(options_for(obj)))
end

#options_for(obj) ⇒ Object



62
63
64
# File 'lib/mongoid/elasticsearch/es.rb', line 62

def options_for(obj)
  {id: obj.id.to_s}.merge type_options
end

#remove_item(obj) ⇒ Object



82
83
84
# File 'lib/mongoid/elasticsearch/es.rb', line 82

def remove_item(obj)
  client.delete(options_for(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, 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_optionsObject



74
75
76
# File 'lib/mongoid/elasticsearch/es.rb', line 74

def type_options
  {index: index.name, type: index.type}
end