Class: AgnosticBackend::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/agnostic_backend/indexer.rb

Direct Known Subclasses

Cloudsearch::Indexer, Elasticsearch::Indexer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Indexer

Returns a new instance of Indexer.



9
10
11
# File 'lib/agnostic_backend/indexer.rb', line 9

def initialize(index)
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/agnostic_backend/indexer.rb', line 7

def index
  @index
end

Instance Method Details

#delete(document_id) ⇒ Object

Deletes the specified document from the index

Parameters:

  • the (document_id)

    document id of the indexed document



38
39
40
# File 'lib/agnostic_backend/indexer.rb', line 38

def delete(document_id)
  delete_all([document_id])
end

#delete_all(document_ids) ⇒ Object

Deletes the specified documents from the index. This is an abstract method which concrete index classes must implement in order to provide its functionality.

Parameters:

  • an (document_ids)

    array of document ids

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/agnostic_backend/indexer.rb', line 46

def delete_all(document_ids)
  raise NotImplementedError
end

#generate_document(indexable) ⇒ Object

Parameters:



32
33
34
# File 'lib/agnostic_backend/indexer.rb', line 32

def generate_document(indexable)
  transform(prepare(indexable.generate_document))
end

#publish(document) ⇒ Object



50
51
52
# File 'lib/agnostic_backend/indexer.rb', line 50

def publish(document)
  publish_all([document])
end

#publish_all(documents) ⇒ Object

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/agnostic_backend/indexer.rb', line 54

def publish_all(documents)
  raise NotImplementedError
end

#put(indexable) ⇒ Object

Sends the specified document to the remote backend.

Parameters:



15
16
17
# File 'lib/agnostic_backend/indexer.rb', line 15

def put(indexable)
  put_all([indexable])
end

#put_all(indexables) ⇒ Object

Sends the specified documents to the remote backend using bulk upload (if supported by the backend)

Parameters:



22
23
24
25
26
27
28
29
# File 'lib/agnostic_backend/indexer.rb', line 22

def put_all(indexables)
  documents = indexables.map do |indexable|
    generate_document(indexable)
  end
  documents.reject!(&:empty?)

  publish_all(documents) unless documents.empty?
end