Class: ActiveFedora::SolrService

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Includes:
Loggable
Defined in:
lib/active_fedora/solr_service.rb

Constant Summary collapse

HAS_MODEL_SOLR_FIELD =
solr_name("has_model", :symbol).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, args) ⇒ SolrService

Returns a new instance of SolrService.



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

def initialize(host, args)
  host = 'http://localhost:8080/solr' unless host
  args = {:read_timeout => 120, :open_timeout => 120}.merge(args.dup)
  args.merge!(:url=>host)
  @conn = RSolr.connect args
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
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



142
143
144
# File 'lib/active_fedora/solr_service.rb', line 142

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

.class_from_solr_document(hit, opts = {}) ⇒ Object

Returns the best singular class for the solr object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_fedora/solr_service.rb', line 60

def class_from_solr_document(hit, opts = {})
  #Set the default starting point to the class specified, if available.
  best_model_match = Model.from_class_uri(opts[:class]) unless opts[:class].nil?
  hit[HAS_MODEL_SOLR_FIELD].each do |value|

    model_value = Model.from_class_uri(value)

    if model_value

      # Set as the first model in case opts[:class] was nil
      best_model_match ||= model_value

      # If there is an inheritance structure, use the most specific case.
      if best_model_match > model_value
        best_model_match = model_value
      end
    end
  end

  logger.warn "Could not find a model for #{hit["id"]}, defaulting to ActiveFedora::Base" unless best_model_match
  best_model_match || ActiveFedora::Base
end

.commitObject



146
147
148
# File 'lib/active_fedora/solr_service.rb', line 146

def commit
  SolrService.instance.conn.commit
end

.construct_query_for_pids(pid_array) ⇒ Object

Construct a solr query for a list of pids This is used to get a solr response based on the list of pids in an object’s RELS-EXT relationhsips If the pid_array is empty, defaults to a query of “id:NEVER_USE_THIS_ID”, which will return an empty solr response

Parameters:

  • pid_array (Array)

    the pids that you want included in the query



87
88
89
90
91
# File 'lib/active_fedora/solr_service.rb', line 87

def construct_query_for_pids(pid_array)
  q = pid_array.reject { |x| x.empty? }.map { |pid| raw_query(SOLR_DOCUMENT_ID, pid) }

  q.empty? ? "id:NEVER_USE_THIS_ID" : q.join(" OR ".freeze)
end

.construct_query_for_rel(field_pairs, join_with = 'AND') ⇒ Object

Create a query with a clause for each key, value

Examples:

construct_query_for_rel [[:has_model, "info:fedora/afmodel:ComplexCollection"], [:has_model, "info:fedora/afmodel:ActiveFedora_Base"]], 'OR'
# => _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ComplexCollection" OR _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ActiveFedora_Base"

Parameters:

  • args (Hash, Array<Array<String>>)

    key is the predicate, value is the target_uri

  • join_with (String) (defaults to: 'AND')

    (‘AND’) the value we’re joining the clauses with



115
116
117
118
119
# File 'lib/active_fedora/solr_service.rb', line 115

def construct_query_for_rel(field_pairs, join_with = 'AND')
  field_pairs = field_pairs.to_a if field_pairs.kind_of? Hash
  clauses = field_pairs.map { |(predicate, target_uri)| raw_query(solr_name(predicate, :symbol), target_uri) }
  clauses.join(" #{join_with} ".freeze)
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



133
134
135
136
# File 'lib/active_fedora/solr_service.rb', line 133

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

.escape_uri_for_query(uri) ⇒ Object



104
105
106
107
# File 'lib/active_fedora/solr_service.rb', line 104

def escape_uri_for_query(uri)
  Deprecation.warn SolrService, "escape_uri_for_query is deprecated and will be removed in active-fedora 8.0.0. Use RSolr.escape instead."
  RSolr.escape(uri)
end

.instanceObject

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/active_fedora/solr_service.rb', line 27

def instance
# Register Solr
    
  unless Thread.current[:solr_service]
    register(ActiveFedora.solr_config[:url])
  end

  raise SolrNotInitialized unless Thread.current[:solr_service]
  Thread.current[:solr_service]
end

.lazy_reify_solr_results(solr_results, opts = {}) ⇒ Object



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

def lazy_reify_solr_results(solr_results, opts = {})
  Enumerator.new do |yielder|
    solr_results.each do |hit|
      yielder.yield(reify_solr_result(hit, opts))
    end
  end
end

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



121
122
123
124
125
126
127
# File 'lib/active_fedora/solr_service.rb', line 121

def query(query, args={})
  raw = args.delete(:raw)
  args = args.merge(:q=>query, :qt=>'standard')
  result = SolrService.instance.conn.get('select', :params=>args)
  return result if raw
  result['response']['docs']
end

.raw_query(key, value) ⇒ Object

Create a raw query clause suitable for sending to solr as an fq element

Parameters:

  • key (String)
  • value (String)


96
97
98
# File 'lib/active_fedora/solr_service.rb', line 96

def raw_query(key, value)
  "_query_:\"{!raw f=#{key}}#{value.gsub('"', '\"')}\""
end

.register(host = nil, args = {}) ⇒ Object



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

def register(host=nil, args={})
  Thread.current[:solr_service] = new(host, args)
end

.reify_solr_result(hit, opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_fedora/solr_service.rb', line 50

def reify_solr_result(hit, opts = {})
  klass = class_from_solr_document(hit)
  if opts[:load_from_solr]
    klass.load_instance_from_solr(hit[SOLR_DOCUMENT_ID], hit)
  else
    klass.find(hit[SOLR_DOCUMENT_ID], cast: true)
  end
end

.reify_solr_results(solr_results, opts = {}) ⇒ Object



46
47
48
# File 'lib/active_fedora/solr_service.rb', line 46

def reify_solr_results(solr_results, opts = {})
  solr_results.collect {|hit| reify_solr_result(hit, opts)}
end

.reset!Object



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

def reset!
  Thread.current[:solr_service] = nil
end

.solr_name(*args) ⇒ Object



100
101
102
# File 'lib/active_fedora/solr_service.rb', line 100

def solr_name(*args)
  Solrizer.default_field_mapper.solr_name(*args)
end