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

Returns a new instance of QueryService.

Parameters:



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

Parameters:

  • model (Class, String)

    the Valkyrie::Resource Class

Returns:

  • integer



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.

Returns:



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.

Returns:



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.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



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.

Parameters:

Returns:

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).

Parameters:

  • alternate_identifier (Valkyrie::ID)

    The alternate identifier to query for.

Returns:

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.

Parameters:

  • resource (Valkyrie::Resource) (defaults to: nil)

    The resource which is being referenced by other resources. Requires either resource or id parameter to be specified.

  • id (Valkyrie::ID) (defaults to: nil)

    ID of the resource which is being reference by other resources. Requires either resource or id parameter to be specified.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

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.

Parameters:

  • ids (Array<Valkyrie::ID, String>)

    The IDs to query for.

Returns:

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.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose members are being searched for.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.



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.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.



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.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose property is being searched.

  • property (Symbol)

    Property which, on the ‘resource`, contains IDs which are to be de-referenced.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.



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