Module: ActiveFedora::Querying

Extended by:
Deprecation
Defined in:
lib/active_fedora/querying.rb

Class Method Summary collapse

Instance Method Summary collapse

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_paramsObject



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

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

Returns:

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

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



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

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



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.

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: nil)

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



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

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



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

#relationObject



11
12
13
# File 'lib/active_fedora/querying.rb', line 11

def relation
  Relation.new(self)
end