Class: BaseIndexer::Solr::Client

Inherits:
Object
  • Object
show all
Includes:
DiscoveryIndexer::Logging
Defined in:
lib/base_indexer/solr/client.rb

Overview

Processes adds and deletes to the solr core

Class Method Summary collapse

Class Method Details

.add(id, solr_doc, solr_connector, max_retries = 10) ⇒ Object

Add the document to solr, retry if an error occurs. See github.com/ooyala/retries for docs on with_retries.

Parameters:

  • id (String)

    the document id, usually it will be druid.

  • solr_doc (Hash)

    a Hash representation of the solr document

  • solr_connector (RSolr::Client)

    is an open connection with the solr core

  • max_retries (Integer) (defaults to: 10)

    the maximum number of tries before fail



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/base_indexer/solr/client.rb', line 22

def self.add(id, solr_doc, solr_connector, max_retries = 10)
  with_retries(max_tries: max_retries, handler: logging_handler(id), base_sleep_seconds: 1, max_sleep_seconds: 5) do |attempt|
    DiscoveryIndexer::Logging.logger.debug "Attempt #{attempt} for #{id}"

    if allow_update?(solr_connector) && doc_exists?(id, solr_connector)
      update_solr_doc(id, solr_doc, solr_connector)
      DiscoveryIndexer::Logging.logger.info "Updating #{id} on attempt #{attempt}"
    else
      solr_connector.add(solr_doc, :add_attributes => {:commitWithin => 10000})
      DiscoveryIndexer::Logging.logger.info "Indexing #{id} on attempt #{attempt}"
    end

    DiscoveryIndexer::Logging.logger.info "Completing #{id} successfully on attempt #{attempt}"
  end
end

.allow_update?(solr_connector) ⇒ Boolean

Returns true if the solr core allowing update feature.

Parameters:

  • solr_connector (RSolr::Client)

    is an open connection with the solr core

Returns:

  • (Boolean)

    true if the solr core allowing update feature



55
56
57
# File 'lib/base_indexer/solr/client.rb', line 55

def self.allow_update?(solr_connector)
  solr_connector.options.include?(:allow_update) ? solr_connector.options[:allow_update] : false
end

.delete(id, solr_connector, max_retries = 10) ⇒ Object

Add the document to solr, retry if an error occurs. See github.com/ooyala/retries for docs on with_retries.

Parameters:

  • id (String)

    the document id, usually it will be druid.

  • solr_connector (RSolr::Client)

    is an open connection with the solr core

  • max_retries (Integer) (defaults to: 10)

    the maximum number of tries before fail



43
44
45
46
47
48
49
50
51
# File 'lib/base_indexer/solr/client.rb', line 43

def self.delete(id, solr_connector, max_retries = 10)
  with_retries(max_tries: max_retries, handler: logging_handler(id), base_sleep_seconds: 1, max_sleep_seconds: 5) do |attempt|
    DiscoveryIndexer::Logging.logger.debug "Attempt #{attempt} for #{id}"

    solr_connector.delete_by_id(id, :add_attributes => {:commitWithin => 10000})
    DiscoveryIndexer::Logging.logger.info "Deleting #{id} on attempt #{attempt}"
    DiscoveryIndexer::Logging.logger.info "Completing #{id} successfully on attempt #{attempt}"
  end
end

.doc_exists?(id, solr_connector) ⇒ Boolean

Returns true if the solr doc defined by this id exists.

Parameters:

  • id (String)

    the document id, usually it will be druid.

  • solr_connector (RSolr::Client)

    is an open connection with the solr core

Returns:

  • (Boolean)

    true if the solr doc defined by this id exists



62
63
64
65
# File 'lib/base_indexer/solr/client.rb', line 62

def self.doc_exists?(id, solr_connector)
  response = solr_connector.get 'select', params: { q: 'id:"' + id + '"', defType: 'lucene' }
  response['response']['numFound'] == 1
end

.logging_handler(id) ⇒ Object



10
11
12
13
14
# File 'lib/base_indexer/solr/client.rb', line 10

def self.logging_handler(id)
  proc do |exception, attempt_number, _total_delay|
    DiscoveryIndexer::Logging.logger.error "#{exception.class} on attempt #{attempt_number} for #{id}"
  end
end

.update_solr_doc(id, solr_doc, solr_connector) ⇒ Object

It is an internal method that updates the solr doc instead of adding a new one.

Parameters:

  • id (String)

    the document id, usually it will be druid.

  • solr_doc (Hash)

    is the solr doc in hash format

  • solr_connector (RSolr::Client)

    is an open connection with the solr core



71
72
73
74
75
76
77
78
79
# File 'lib/base_indexer/solr/client.rb', line 71

def self.update_solr_doc(id, solr_doc, solr_connector)
  # update_solr_doc can't used RSolr because updating hash doc is not supported
  #  so we need to build the json input manually
  hash = { id: id }
  solr_doc.each do |k,v|
    hash[k] = { set: v } unless k.to_sym == :id
  end
  solr_connector.update params: { commitWithin: 10000 }, data: Array.wrap(hash).to_json, headers: { 'Content-Type' => 'application/json' }
end