Class: Hyrax::SolrQueryService
- Inherits:
-
SearchBuilder
- Object
- SearchBuilder
- Hyrax::SolrQueryService
- Defined in:
- app/services/hyrax/solr_query_service.rb
Overview
Methods in this class are providing functionality previously supported by ActiveFedora::SolrQueryBuilder.
Supports building and executing a solr query.
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#solr_service ⇒ Object
readonly
Returns the value of attribute solr_service.
Class Method Summary collapse
-
.document_model ⇒ Class
private
The model class to use for solr documents.
Instance Method Summary collapse
-
#accessible_by(ability:, action: :index) ⇒ SolrQueryService
The existing service with access filters query appended.
-
#build(join_with: 'AND') ⇒ String
The combined query that can be submitted to solr.
-
#count ⇒ Integer
The number of results that match the query in solr.
-
#get(**args) ⇒ Hash
execute the query using a GET request.
-
#get_ids ⇒ Array<String>
Ids of documents matching the current query.
-
#get_objects(use_valkyrie: Hyrax.config.use_valkyrie?) ⇒ Array<Valkyrie::Resource|ActiveFedora::Base>
Objects matching the current query.
-
#initialize(query: [], solr_service: Hyrax::SolrService) ⇒ SolrQueryService
constructor
A new instance of SolrQueryService.
-
#query_result(**args) ⇒ Hash
execute the solr query and return results.
-
#reset ⇒ Hyrax::SolrQueryService
The existing service with the query reset to empty.
- #solr_documents(**args) ⇒ Enumerable<SolrDocument>
-
#with_field_pairs(field_pairs: {}, join_with: default_join_with, type: 'field') ⇒ SolrQueryService
The existing service with field_pair query appended.
-
#with_generic_type(generic_type: 'Work') ⇒ SolrQueryService
The existing service with model query appended.
-
#with_ids(ids: []) ⇒ Hyrax::SolrQueryService
The existing service with id query appended.
-
#with_model(model:) ⇒ SolrQueryService
The existing service with model query appended.
Constructor Details
#initialize(query: [], solr_service: Hyrax::SolrService) ⇒ SolrQueryService
Returns a new instance of SolrQueryService.
14 15 16 17 |
# File 'app/services/hyrax/solr_query_service.rb', line 14 def initialize(query: [], solr_service: Hyrax::SolrService) @query = query @solr_service = solr_service end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
12 13 14 |
# File 'app/services/hyrax/solr_query_service.rb', line 12 def query @query end |
#solr_service ⇒ Object (readonly)
Returns the value of attribute solr_service.
12 13 14 |
# File 'app/services/hyrax/solr_query_service.rb', line 12 def solr_service @solr_service end |
Class Method Details
.document_model ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the model class to use for solr documents.
24 25 26 |
# File 'app/services/hyrax/solr_query_service.rb', line 24 def self.document_model CatalogController.blacklight_config.document_model end |
Instance Method Details
#accessible_by(ability:, action: :index) ⇒ SolrQueryService
Returns the existing service with access filters query appended.
133 134 135 136 137 |
# File 'app/services/hyrax/solr_query_service.rb', line 133 def accessible_by(ability:, action: :index) access_filters_query = construct_query_for_ability(ability, action) @query += [access_filters_query] if access_filters_query.present? self end |
#build(join_with: 'AND') ⇒ String
Returns the combined query that can be submitted to solr.
72 73 74 75 |
# File 'app/services/hyrax/solr_query_service.rb', line 72 def build(join_with: 'AND') return 'id:NEVER_USE_THIS_ID' if @query.blank? # forces this method to always return a valid solr query @query.join(padded_join_with(join_with)) end |
#count ⇒ Integer
Returns the number of results that match the query in solr.
65 66 67 |
# File 'app/services/hyrax/solr_query_service.rb', line 65 def count solr_service.count(build) end |
#get(**args) ⇒ Hash
execute the query using a GET request
31 32 33 |
# File 'app/services/hyrax/solr_query_service.rb', line 31 def get(**args) solr_service.get(build, **args) end |
#get_ids ⇒ Array<String>
Returns ids of documents matching the current query.
50 51 52 53 |
# File 'app/services/hyrax/solr_query_service.rb', line 50 def get_ids # rubocop:disable Naming/AccessorMethodName results = query_result results['response']['docs'].map { |doc| doc['id'] } end |
#get_objects(use_valkyrie: Hyrax.config.use_valkyrie?) ⇒ Array<Valkyrie::Resource|ActiveFedora::Base>
Returns objects matching the current query.
57 58 59 60 61 |
# File 'app/services/hyrax/solr_query_service.rb', line 57 def get_objects(use_valkyrie: Hyrax.config.use_valkyrie?) ids = get_ids return ids.map { |id| ActiveFedora::Base.find(id) } unless use_valkyrie query_service.find_many_by_ids(ids: ids) end |
#query_result(**args) ⇒ Hash
execute the solr query and return results
38 39 40 |
# File 'app/services/hyrax/solr_query_service.rb', line 38 def query_result(**args) solr_service.query_result(build, **args) end |
#reset ⇒ Hyrax::SolrQueryService
Returns the existing service with the query reset to empty.
79 80 81 82 |
# File 'app/services/hyrax/solr_query_service.rb', line 79 def reset @query = [] self end |
#solr_documents(**args) ⇒ Enumerable<SolrDocument>
44 45 46 |
# File 'app/services/hyrax/solr_query_service.rb', line 44 def solr_documents(**args) query_result(**args)['response']['docs'].map { |doc| self.class.document_model.new(doc) } end |
#with_field_pairs(field_pairs: {}, join_with: default_join_with, type: 'field') ⇒ SolrQueryService
Returns the existing service with field_pair query appended.
122 123 124 125 126 127 |
# File 'app/services/hyrax/solr_query_service.rb', line 122 def with_field_pairs(field_pairs: {}, join_with: default_join_with, type: 'field') pairs_query = construct_query_for_pairs(field_pairs, join_with, type) return self if pairs_query.blank? @query += [pairs_query] self end |
#with_generic_type(generic_type: 'Work') ⇒ SolrQueryService
Returns the existing service with model query appended.
107 108 109 110 111 112 113 114 115 |
# File 'app/services/hyrax/solr_query_service.rb', line 107 def with_generic_type(generic_type: 'Work') # TODO: Generic type was originally stored as `sim`. Since it is never multi-valued, it is moving to being stored # as `si`. Until a migration is created to correct existing solr docs, this query searches in both fields. # @see https://github.com/samvera/hyrax/issues/6086 field_pairs = { generic_type_si: generic_type, generic_type_sim: generic_type } type_query = construct_query_for_pairs(field_pairs, ' OR ', 'field') @query += [type_query] self end |
#with_ids(ids: []) ⇒ Hyrax::SolrQueryService
Returns the existing service with id query appended.
87 88 89 90 91 92 93 |
# File 'app/services/hyrax/solr_query_service.rb', line 87 def with_ids(ids: []) ids = ids.reject(&:blank?) raise ArgumentError, "Expected there to be at least one non-blank id." if ids.blank? id_query = construct_query_for_ids(ids) @query += [id_query] self end |
#with_model(model:) ⇒ SolrQueryService
Returns the existing service with model query appended.
98 99 100 101 102 |
# File 'app/services/hyrax/solr_query_service.rb', line 98 def with_model(model:) model_query = construct_query_for_model(model) @query += [model_query] self end |