Module: ActiveFedora::Querying
- Defined in:
- lib/active_fedora/querying.rb
Instance Method Summary collapse
-
#count(args = {}) ⇒ Object
Get a count of the number of objects from solr Takes :conditions as an argument.
- #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 = false) ⇒ 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
Instance Method Details
#count(args = {}) ⇒ Object
Get a count of the number of objects from solr Takes :conditions as an argument
66 67 68 69 70 |
# File 'lib/active_fedora/querying.rb', line 66 def count(args = {}) q = search_model_clause ? [search_model_clause] : [] q << "#{args[:conditions]}" if args[:conditions] SolrService.query(q.join(' AND '), :raw=>true, :rows=>0)['response']['numFound'] end |
#exists?(pid) ⇒ Boolean
59 60 61 62 |
# File 'lib/active_fedora/querying.rb', line 59 def exists?(pid) inner = DigitalObject.find_or_initialize(self, pid) !inner.new? end |
#find_each(conditions = {}, opts = {}) ⇒ Object
Yields the found ActiveFedora::Base object to the passed block
47 48 49 50 51 52 53 |
# File 'lib/active_fedora/querying.rb', line 47 def find_each( conditions={}, opts={}) find_in_batches(conditions, opts.merge({:fl=>SOLR_DOCUMENT_ID})) do |group| group.each do |hit| yield(find_one(hit[SOLR_DOCUMENT_ID], opts[:cast])) end end end |
#find_in_batches(conditions, opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_fedora/querying.rb', line 23 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]=[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc'] 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 = false) ⇒ 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.
100 101 102 103 104 |
# File 'lib/active_fedora/querying.rb', line 100 def find_one(pid, cast=false) inner = DigitalObject.find(self, pid) af_base = self.allocate.init_with(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
80 81 82 83 84 85 86 |
# File 'lib/active_fedora/querying.rb', line 80 def find_with_conditions(conditions, opts={}) #set default sort to created date ascending unless opts.include?(:sort) opts[:sort]=[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc'] end SolrService.query(create_query(conditions), opts) end |
#quote_for_solr(value) ⇒ Object
88 89 90 |
# File 'lib/active_fedora/querying.rb', line 88 def quote_for_solr(value) '"' + value.gsub(/(:)/, '\\:').gsub(/(\/)/, '\\/').gsub(/"/, '\\"') + '"' end |