Module: ActiveFedora::Querying
- Extended by:
- Deprecation
- Defined in:
- lib/active_fedora/querying.rb
Class Method Summary collapse
Instance Method Summary collapse
- #calculate(calculation, conditions, opts = {}) ⇒ Object
- #default_sort_params ⇒ Object
- #exists?(pid) ⇒ Boolean
-
#find_each(conditions = {}, opts = {}) ⇒ Object
Yields the found ActiveFedora::Base object to the passed block.
-
#find_in_batches(conditions, opts = {}) ⇒ Object
Yields each batch of solr records that was found by the find
options
as an array. -
#find_one(pid, cast = nil) ⇒ Object
Retrieve the Fedora object with the given pid, explore the returned object, determine its model using #ContentModel.known_models_for and cast to that class.
-
#find_with_conditions(conditions, opts = {}) ⇒ Object
Returns a solr result matching the supplied conditions @param conditions can either be specified as a string, or hash representing the query part of an solr statement.
- #quote_for_solr(value) ⇒ Object
- #relation ⇒ Object
Class Method Details
.extended(base) ⇒ Object
5 6 7 8 |
# File 'lib/active_fedora/querying.rb', line 5 def self.extended(base) base.class_attribute :solr_query_handler base.solr_query_handler = 'standard' end |
Instance Method Details
#calculate(calculation, conditions, opts = {}) ⇒ Object
49 50 51 |
# File 'lib/active_fedora/querying.rb', line 49 def calculate(calculation, conditions, opts={}) SolrService.query(create_query(conditions), :raw=>true, :rows=>0)['response']['numFound'] end |
#default_sort_params ⇒ Object
53 54 55 |
# File 'lib/active_fedora/querying.rb', line 53 def default_sort_params [ActiveFedora::SolrService.solr_name(:system_create, :stored_sortable, type: :date)+' asc'] end |
#exists?(pid) ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/active_fedora/querying.rb', line 79 def exists?(pid) return false if pid.nil? || pid.empty? !!DigitalObject.find(self, pid) rescue ActiveFedora::ObjectNotFoundError false end |
#find_each(conditions = {}, opts = {}) ⇒ Object
Yields the found ActiveFedora::Base object to the passed block
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_fedora/querying.rb', line 62 def find_each( conditions={}, opts={}) cast = opts.delete(:cast) find_in_batches(conditions, opts.merge({:fl=>SOLR_DOCUMENT_ID})) do |group| group.each do |hit| begin yield(find_one(hit[SOLR_DOCUMENT_ID], cast)) rescue ActiveFedora::ObjectNotFoundError logger.error "When trying to find_each #{hit[SOLR_DOCUMENT_ID]}, encountered an ObjectNotFoundError. Solr may be out of sync with Fedora" end end end end |
#find_in_batches(conditions, opts = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_fedora/querying.rb', line 30 def find_in_batches conditions, opts={} opts[:q] = create_query(conditions) opts[:qt] = solr_query_handler #set default sort to created date ascending unless opts[:sort].present? opts[:sort]= default_sort_params end batch_size = opts.delete(:batch_size) || 1000 counter = 0 begin counter += 1 response = ActiveFedora::SolrService.instance.conn.paginate counter, batch_size, "select", :params => opts docs = response["response"]["docs"] yield docs end while docs.has_next? end |
#find_one(pid, cast = nil) ⇒ Object
Retrieve the Fedora object with the given pid, explore the returned object, determine its model using #ContentModel.known_models_for and cast to that class. Raises a ObjectNotFoundError if the object is not found.
118 119 120 121 122 123 124 125 126 |
# File 'lib/active_fedora/querying.rb', line 118 def find_one(pid, cast=nil) if self == ActiveFedora::Base && cast.nil? cast = false Deprecation.warn(Querying, "find_one's cast parameter will default to true in ActiveFedora 7.0.0. If you want to maintain your existing behavior set `false' as the second parameter.", caller) end inner = DigitalObject.find(self, pid) af_base = self.allocate.init_with_object(inner) cast ? af_base.adapt_to_cmodel : af_base end |
#find_with_conditions(conditions, opts = {}) ⇒ Object
Returns a solr result matching the supplied conditions @param conditions can either be specified as a string, or hash representing the query part of an solr statement. If a hash is provided, this method will generate conditions based simple equality combined using the boolean AND operator. @param options
94 95 96 97 98 99 100 |
# File 'lib/active_fedora/querying.rb', line 94 def find_with_conditions(conditions, opts={}) #set default sort to created date ascending unless opts.include?(:sort) opts[:sort]=default_sort_params end SolrService.query(create_query(conditions), opts) end |
#quote_for_solr(value) ⇒ Object
102 103 104 |
# File 'lib/active_fedora/querying.rb', line 102 def quote_for_solr(value) '"' + value.gsub(/(:)/, '\\:').gsub(/(\/)/, '\\/').gsub(/"/, '\\"') + '"' end |