Class: ActiveFedora::SolrService
- Inherits:
-
Object
- Object
- ActiveFedora::SolrService
- Includes:
- Loggable
- Defined in:
- lib/active_fedora/solr_service.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Class Method Summary collapse
- .add(doc) ⇒ Object
- .class_from_solr_document(hit) ⇒ Object
- .commit ⇒ Object
-
.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.
-
.construct_query_for_rel(args) ⇒ Object
Create a query with a clause for each key, value.
-
.count(query, args = {}) ⇒ Integer
Get the count of records that match the query.
- .escape_uri_for_query(uri) ⇒ Object
- .instance ⇒ Object
- .query(query, args = {}) ⇒ Object
- .register(host = nil, args = {}) ⇒ Object
- .reify_solr_results(solr_result, opts = {}) ⇒ Object
- .reset! ⇒ Object
- .solr_name(*args) ⇒ Object
Instance Method Summary collapse
-
#initialize(host, args) ⇒ SolrService
constructor
A new instance of SolrService.
Constructor Details
#initialize(host, args) ⇒ SolrService
Returns a new instance of SolrService.
18 19 20 21 22 23 |
# File 'lib/active_fedora/solr_service.rb', line 18 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
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
8 9 10 |
# File 'lib/active_fedora/solr_service.rb', line 8 def conn @conn end |
Class Method Details
.add(doc) ⇒ Object
105 106 107 |
# File 'lib/active_fedora/solr_service.rb', line 105 def self.add(doc) SolrService.instance.conn.add(doc) end |
.class_from_solr_document(hit) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/active_fedora/solr_service.rb', line 49 def self.class_from_solr_document(hit) model_value = nil hit[solr_name("has_model", :symbol)].each {|value| model_value ||= Model.from_class_uri(value)} logger.warn "Could not find a model for #{hit["id"]}, defaulting to ActiveFedora::Base" unless model_value model_value || ActiveFedora::Base end |
.commit ⇒ Object
109 110 111 |
# File 'lib/active_fedora/solr_service.rb', line 109 def self.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
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_fedora/solr_service.rb', line 60 def self.construct_query_for_pids(pid_array) q = pid_array.reject { |x| x.empty? }.map do |pid| "_query_:\"{!raw f=#{SOLR_DOCUMENT_ID}}#{pid.gsub('"', '\"')}\"" end return "id:NEVER_USE_THIS_ID" if q.empty? return q.join(" OR ") end |
.construct_query_for_rel(args) ⇒ Object
Create a query with a clause for each key, value
81 82 83 84 85 86 |
# File 'lib/active_fedora/solr_service.rb', line 81 def self.construct_query_for_rel(args) clauses = args.map do |predicate, target_uri| "_query_:\"{!raw f=#{solr_name(predicate, :symbol)}}#{target_uri.gsub('"', '\"')}\"" end clauses.join(" AND ") end |
.count(query, args = {}) ⇒ Integer
Get the count of records that match the query
100 101 102 103 |
# File 'lib/active_fedora/solr_service.rb', line 100 def self.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
75 76 77 |
# File 'lib/active_fedora/solr_service.rb', line 75 def self.escape_uri_for_query(uri) return uri.gsub(/(:)/, '\\:').gsub(/(\/)/, '\\/') end |
.instance ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_fedora/solr_service.rb', line 25 def self.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 |
.query(query, args = {}) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/active_fedora/solr_service.rb', line 88 def self.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 |
.register(host = nil, args = {}) ⇒ Object
10 11 12 |
# File 'lib/active_fedora/solr_service.rb', line 10 def self.register(host=nil, args={}) Thread.current[:solr_service]=self.new(host, args) end |
.reify_solr_results(solr_result, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_fedora/solr_service.rb', line 36 def self.reify_solr_results(solr_result,opts={}) results = [] solr_result.each do |hit| classname = class_from_solr_document(hit) if opts[:load_from_solr] results << classname.load_instance_from_solr(hit[SOLR_DOCUMENT_ID]) else results << classname.find(hit[SOLR_DOCUMENT_ID]) end end return results end |
.reset! ⇒ Object
14 15 16 |
# File 'lib/active_fedora/solr_service.rb', line 14 def self.reset! Thread.current[:solr_service] = nil end |
.solr_name(*args) ⇒ Object
71 72 73 |
# File 'lib/active_fedora/solr_service.rb', line 71 def self.solr_name(*args) Solrizer.default_field_mapper.solr_name(*args) end |