Module: ActiveFedora::Querying

Defined in:
lib/active_fedora/querying.rb

Instance Method Summary collapse

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

Returns true if the pid exists in the repository @param pid @return

Returns:

  • (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

Parameters:

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

    the conditions for the solr search to match

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

Options Hash (opts):

  • :cast (Boolean)

    when true, examine the model and cast it to the first known cModel



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

Yields each batch of solr records that was found by the find options as an array. The size of each batch is set by the :batch_size option; the default is 1000.

Returns a solr result matching the supplied conditions @param conditions solr conditions to match @param options

Examples:

Person.find_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
group.each { |person| puts person['name_t'] }
end

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • :sort (Array)

    a list of fields to sort by

  • :rows (Array)

    number of rows to return



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.

Examples:

because the object hydra:dataset1 asserts it is a Dataset (hasModel info:fedora/afmodel:Dataset), return a Dataset object (not a Book).

Book.find_one("hydra:dataset1") 

Parameters:

  • pid (String)

    of the object to load

  • cast (Boolean) (defaults to: false)

    when true, cast the found object to the class of the first known model defined in it’s RELS-EXT



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

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • :sort (Array)

    a list of fields to sort by

  • :rows (Array)

    number of rows to return



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

#relationObject



4
5
6
# File 'lib/active_fedora/querying.rb', line 4

def relation
  Relation.new(self)
end