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

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

#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



64
65
66
# File 'lib/mongoid/elasticsearch/es.rb', line 64

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



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

Returns:

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

#options_for(obj) ⇒ Object



68
69
70
# File 'lib/mongoid/elasticsearch/es.rb', line 68

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

#remove_item(obj) ⇒ Object



88
89
90
# File 'lib/mongoid/elasticsearch/es.rb', line 88

def remove_item(obj)
  client.delete(options_for(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, 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, options[:scope] || klass, options)
end

#type_optionsObject



80
81
82
# File 'lib/mongoid/elasticsearch/es.rb', line 80

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