Class: Hyrax::CustomQueries::FindByPropertyValue

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/custom_queries/find_by_property_value.rb

Overview

Finds resources for arbitrary properties using the Solr index.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_service:, query_rows: 1_000) ⇒ FindByPropertyValue

Returns a new instance of FindByPropertyValue.



16
17
18
19
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 16

def initialize(query_service:, query_rows: 1_000)
  @query_service = query_service
  @query_rows = query_rows
end

Class Method Details

.queriesObject



9
10
11
12
13
14
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 9

def self.queries
  [:find_ids_by_property_pairs,
   :find_many_by_property_pairs,
   :find_many_by_property_value,
   :find_by_property_value]
end

Instance Method Details

#find_by_property_value(property:, value:, field_suffix: :ssim, search_field: nil, **options) ⇒ NilClass, Valkyrie::Resource

Returns one resource matching the specified property and value.

Parameters:

  • property (#to_s)

    the name of the property we're attempting to query.

  • value (#to_s)

    the property value we're trying to match.

  • field_suffix (defaults to: :ssim)

    Symbol which solr field suffix to use

  • search_field (defaults to: nil)

    String alt way to set property and suffix for compatibility

  • options

    Hash options to pass to solr

Returns:

  • (NilClass)

    when no record was found

  • (Valkyrie::Resource)

    when a resource was found



82
83
84
85
86
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 82

def find_by_property_value(property:, value:, field_suffix: :ssim, search_field: nil, **options)
  options[:rows] = 1
  find_many_by_property_value(property: property, value: value,
                              field_suffix: field_suffix, search_field: search_field, **options).first
end

#find_ids_by_property_pairs(pairs:, field_suffix: :ssim, **options) ⇒ Array<String>

Returns ids for resources matching all specified property/value pairs.

Parameters:

  • pairs (Hash<String, String>)

    properties and values to match

  • field_suffix (defaults to: :ssim)

    Hash,Symbol which solr field suffix to use; Use a hash to set per field pair

  • options

    Hash options to pass to solr

Returns:

  • (Array<String>)

    found ids, if any



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 28

def find_ids_by_property_pairs(pairs:, field_suffix: :ssim, **options)
  options = { rows: @query_rows }.merge(options)
  query = pairs.map do |k, v|
    suffix = if field_suffix.is_a? Hash
               field_suffix[k] || :ssim
             else
               field_suffix
             end
    "+#{k}_#{suffix}:\"#{RSolr.solr_escape(v)}\""
  end
  response = Hyrax::SolrService.query_result(query.join(' '), fl: 'id', **options)
  response['response']['docs'].map { |doc| doc['id'] }
end

#find_many_by_property_pairs(pairs:, field_suffix: :ssim, **options) ⇒ Array<Valkyrie::Resource>

Returns resources matching all specified property/value pairs.

Parameters:

  • pairs (Hash<String, String>)

    properties and values to match

  • field_suffix (defaults to: :ssim)

    Hash,Symbol which solr field suffix to use; Use a hash to set per field pair

  • options

    Hash options to pass to solr

Returns:

  • (Array<Valkyrie::Resource>)

    found resources, if any



49
50
51
52
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 49

def find_many_by_property_pairs(pairs:, field_suffix: :ssim, **options)
  found_ids = find_ids_by_property_pairs(pairs: pairs, field_suffix: field_suffix, **options)
  @query_service.find_many_by_ids(ids: found_ids).to_a # Wings emits an Enumerator
end

#find_many_by_property_value(property:, value:, field_suffix: :ssim, search_field: nil, **options) ⇒ Array<Valkyrie::Resource>

Returns resources matching the specified property and value.

Parameters:

  • property (#to_s)

    the name of the property we're attempting to query.

  • value (#to_s)

    the property value we're trying to match.

  • field_suffix (defaults to: :ssim)

    Symbol which solr field suffix to use

  • search_field (defaults to: nil)

    String alt way to set property and suffix for compatibility

  • options

    Hash options to pass to solr

Returns:

  • (Array<Valkyrie::Resource>)

    found resources, if any



63
64
65
66
67
68
69
70
# File 'app/services/hyrax/custom_queries/find_by_property_value.rb', line 63

def find_many_by_property_value(property:, value:, field_suffix: :ssim, search_field: nil, **options)
  if search_field
    search_field_array = search_field.split('_')
    field_suffix = search_field_array.last
    property = search_field_array[0...-1].join('_')
  end
  find_many_by_property_pairs(pairs: { property => value }, field_suffix: field_suffix, **options)
end