Class: ActiveFedora::SolrService

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/solr_service.rb

Constant Summary collapse

MAX_ROWS =
10_000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SolrService

Returns a new instance of SolrService.



9
10
11
# File 'lib/active_fedora/solr_service.rb', line 9

def initialize(options = {})
  @options = { read_timeout: 120, open_timeout: 120, url: 'http://localhost:8080/solr' }.merge(options)
end

Instance Attribute Details

#connObject



13
14
15
# File 'lib/active_fedora/solr_service.rb', line 13

def conn
  @conn ||= RSolr.connect @options
end

Class Method Details

.add(doc, params = {}) ⇒ Object

Parameters:

  • doc (Hash)

    the document to index

  • params (Hash) (defaults to: {})

    :commit => commits immediately :softCommit => commit to memory, but don’t flush to disk



71
72
73
# File 'lib/active_fedora/solr_service.rb', line 71

def add(doc, params = {})
  SolrService.instance.conn.add(doc, params: params)
end

.commitObject



75
76
77
# File 'lib/active_fedora/solr_service.rb', line 75

def commit
  SolrService.instance.conn.commit
end

.count(query, args = {}) ⇒ Integer

Get the count of records that match the query

Parameters:

  • query (String)

    a solr query

  • args (Hash) (defaults to: {})

    arguments to pass through to ‘args’ param of SolrService.query (note that :rows will be overwritten to 0)

Returns:

  • (Integer)

    number of records matching



62
63
64
65
# File 'lib/active_fedora/solr_service.rb', line 62

def count(query, args = {})
  args = args.merge(rows: 0)
  SolrService.get(query, args)['response']['numFound'].to_i
end

.delete(id) ⇒ Object



54
55
56
# File 'lib/active_fedora/solr_service.rb', line 54

def delete(id)
  SolrService.instance.conn.delete_by_id(id, params: { 'softCommit' => true })
end

.get(query, args = {}) ⇒ Object



41
42
43
44
# File 'lib/active_fedora/solr_service.rb', line 41

def get(query, args = {})
  args = args.merge(q: query, qt: 'standard')
  SolrService.instance.conn.get(select_path, params: args)
end

.instanceObject



31
32
33
34
35
36
37
38
39
# File 'lib/active_fedora/solr_service.rb', line 31

def instance
  # Register Solr

  unless ActiveFedora::RuntimeRegistry.solr_service
    register(ActiveFedora.solr_config)
  end

  ActiveFedora::RuntimeRegistry.solr_service
end

.query(query, args = {}) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/active_fedora/solr_service.rb', line 46

def query(query, args = {})
  Base.logger.warn "Calling ActiveFedora::SolrService.get without passing an explicit value for ':rows' is not recommended. You will end up with Solr's default (usually set to 10)\nCalled by #{caller[0]}" unless args.key?(:rows)
  result = get(query, args)
  result['response']['docs'].map do |doc|
    ActiveFedora::SolrHit.new(doc)
  end
end

.register(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})


19
20
21
# File 'lib/active_fedora/solr_service.rb', line 19

def register(options = {})
  ActiveFedora::RuntimeRegistry.solr_service = new(options)
end

.reset!Object



23
24
25
# File 'lib/active_fedora/solr_service.rb', line 23

def reset!
  ActiveFedora::RuntimeRegistry.solr_service = nil
end

.select_pathObject



27
28
29
# File 'lib/active_fedora/solr_service.rb', line 27

def select_path
  ActiveFedora.solr_config.fetch(:select_path, 'select')
end