Class: Searchkick::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/searchkick/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Index

Returns a new instance of Index.



5
6
7
# File 'lib/searchkick/index.rb', line 5

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/searchkick/index.rb', line 3

def name
  @name
end

Instance Method Details

#create(options = {}) ⇒ Object



9
10
11
# File 'lib/searchkick/index.rb', line 9

def create(options = {})
  client.indices.create index: name, body: options
end

#deleteObject



13
14
15
# File 'lib/searchkick/index.rb', line 13

def delete
  client.indices.delete index: name
end

#exists?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/searchkick/index.rb', line 17

def exists?
  client.indices.exists index: name
end

#import(records) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/searchkick/index.rb', line 42

def import(records)
  records.group_by{|r| document_type(r) }.each do |type, batch|
    client.bulk(
      index: name,
      type: type,
      body: batch.map{|r| {index: {_id: search_id(r), data: search_data(r)}} }
    )
  end
end

#klass_document_type(klass) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/searchkick/index.rb', line 60

def klass_document_type(klass)
  if klass.respond_to?(:document_type)
    klass.document_type
  else
    klass.model_name.to_s.underscore
  end
end

#refreshObject



21
22
23
# File 'lib/searchkick/index.rb', line 21

def refresh
  client.indices.refresh index: name
end

#remove(record) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/searchkick/index.rb', line 34

def remove(record)
  client.delete(
    index: name,
    type: document_type(record),
    id: search_id(record)
  )
end

#retrieve(record) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/searchkick/index.rb', line 52

def retrieve(record)
  client.get(
    index: name,
    type: document_type(record),
    id: record.id
  )["_source"]
end

#store(record) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/searchkick/index.rb', line 25

def store(record)
  client.index(
    index: name,
    type: document_type(record),
    id: search_id(record),
    body: search_data(record)
  )
end

#tokens(text, options = {}) ⇒ Object



68
69
70
# File 'lib/searchkick/index.rb', line 68

def tokens(text, options = {})
  client.indices.analyze({text: text, index: name}.merge(options))["tokens"].map{|t| t["token"] }
end