Class: Wings::CustomQueries::FindIdsByModel

Inherits:
Object
  • Object
show all
Defined in:
lib/wings/services/custom_queries/find_ids_by_model.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FindIdsByModel.



12
13
14
15
# File 'lib/wings/services/custom_queries/find_ids_by_model.rb', line 12

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

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service.



17
18
19
# File 'lib/wings/services/custom_queries/find_ids_by_model.rb', line 17

def query_service
  @query_service
end

Class Method Details

.queriesObject



8
9
10
# File 'lib/wings/services/custom_queries/find_ids_by_model.rb', line 8

def self.queries
  [:find_ids_by_model]
end

Instance Method Details

#find_ids_by_model(model:, ids: :all) ⇒ Enumerable<Valkyrie::ID>

Note:

uses solr to do the lookup

Parameters:

  • model (Class)
  • ids (Enumerable<#to_s>, Symbol) (defaults to: :all)

Returns:

  • (Enumerable<Valkyrie::ID>)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wings/services/custom_queries/find_ids_by_model.rb', line 27

def find_ids_by_model(model:, ids: :all)
  return enum_for(:find_ids_by_model, model: model, ids: ids) unless block_given?
  model_name = ModelRegistry.lookup(model).model_name

  solr_query = "_query_:\"{!raw f=has_model_ssim}#{model_name}\""
  solr_response = Hyrax::SolrService.get(solr_query, fl: 'id', rows: @query_rows)['response']

  loop do
    response_docs = solr_response['docs']
    response_docs.select! { |doc| ids.include?(doc['id']) } unless ids == :all

    response_docs.each { |doc| yield doc['id'] }

    break if (solr_response['start'] + solr_response['docs'].count) >= solr_response['numFound']
    solr_response = Hyrax::SolrService.get(solr_query, fl: 'id', rows: @query_rows, start: solr_response['start'] + @query_rows)['response']
  end
end