Class: Valkyrie::Persistence::Solr::QueryService

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/solr/query_service.rb

Overview

Query Service for Solr MetadataAdapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:, resource_factory:, adapter:) ⇒ QueryService



9
10
11
12
13
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 9

def initialize(connection:, resource_factory:, adapter:)
  @connection = connection
  @resource_factory = resource_factory
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 6

def adapter
  @adapter
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 6

def connection
  @connection
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



6
7
8
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 6

def resource_factory
  @resource_factory
end

Instance Method Details

#count_all_of_model(model:) ⇒ Object

Count all of the Valkyrie Resources of a model persisted in the Solr index



52
53
54
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 52

def count_all_of_model(model:)
  Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory, model: model).count
end

#custom_queriesValkyrie::Persistence::CustomQueryContainer

Get the set of custom queries configured for this query service.



94
95
96
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 94

def custom_queries
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
end

#find_allArray<Valkyrie::Resource>

Get all objects.



40
41
42
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 40

def find_all
  Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory).run
end

#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>

Get all objects of a given model.



45
46
47
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 45

def find_all_of_model(model:)
  Valkyrie::Persistence::Solr::Queries::FindAllQuery.new(connection: connection, resource_factory: resource_factory, model: model).run
end

#find_by(id:) ⇒ Valkyrie::Resource

Get a single resource by ID.

Raises:



16
17
18
19
20
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 16

def find_by(id:)
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
  validate_id(id)
  Valkyrie::Persistence::Solr::Queries::FindByIdQuery.new(id, connection: connection, resource_factory: resource_factory).run
end

#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource

Get a single resource by ‘alternate_identifier`. Alternate identifiers are identifiers (like NOIDs, DOIs, ARKs, etc.) that are not the system-generated ID, but might be used to identify a resource in an application (e.g., to make shorter URLs).

Raises:

  • (Valkyrie::Persistence::ObjectNotFoundError)

    Raised when the alternate identifier isn’t in the persistence backend.

  • (ArgumentError)

    Raised when alternate identifier is not a String or a Valkyrie::ID



23
24
25
26
27
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 23

def find_by_alternate_identifier(alternate_identifier:)
  alternate_identifier = Valkyrie::ID.new(alternate_identifier.to_s) if alternate_identifier.is_a?(String)
  validate_id(alternate_identifier)
  Valkyrie::Persistence::Solr::Queries::FindByAlternateIdentifierQuery.new(alternate_identifier, connection: connection, resource_factory: resource_factory).run
end

#find_inverse_references_by(resource: nil, id: nil, property:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources which link to a resource with a given property.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



84
85
86
87
88
89
90
91
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 84

def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
  raise ArgumentError, "Provide resource or id" unless resource || id
  ensure_persisted(resource) if resource
  id ||= resource.id
  result = Valkyrie::Persistence::Solr::Queries::FindInverseReferencesQuery.new(id: id, property: property, connection: connection, resource_factory: resource_factory).run
  return result unless model
  result.select { |obj| obj.is_a?(model) }
end

#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>

Get a batch of resources by ID.

Raises:

  • (ArgumentError)

    Raised when any ID is not a String or a Valkyrie::ID



30
31
32
33
34
35
36
37
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 30

def find_many_by_ids(ids:)
  ids.map! do |id|
    id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
    validate_id(id)
    id
  end
  Valkyrie::Persistence::Solr::Queries::FindManyByIdsQuery.new(ids, connection: connection, resource_factory: resource_factory).run
end

#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all members of a given resource.



62
63
64
65
66
67
68
69
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 62

def find_members(resource:, model: nil)
  Valkyrie::Persistence::Solr::Queries::FindMembersQuery.new(
    resource: resource,
    model: model,
    connection: connection,
    resource_factory: resource_factory
  ).run
end

#find_parents(resource:) ⇒ Array<Valkyrie::Resource>

Find all parents of a given resource.



57
58
59
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 57

def find_parents(resource:)
  find_inverse_references_by(resource: resource, property: :member_ids)
end

#find_references_by(resource:, property:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources referenced from a resource with a given property.



72
73
74
75
76
77
78
79
80
81
# File 'lib/valkyrie/persistence/solr/query_service.rb', line 72

def find_references_by(resource:, property:, model: nil)
  result =
    if ordered_property?(resource: resource, property: property)
      Valkyrie::Persistence::Solr::Queries::FindOrderedReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
    else
      Valkyrie::Persistence::Solr::Queries::FindReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run
    end
  return result unless model
  result.select { |obj| obj.is_a?(model) }
end