Class: Hyrax::SolrQueryService

Inherits:
SearchBuilder
  • Object
show all
Defined in:
app/services/hyrax/solr_query_service.rb

Overview

Note:

Methods in this class are providing functionality previously supported by ActiveFedora::SolrQueryBuilder.

Supports building and executing a solr query.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#queryObject (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_serviceObject (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_modelClass

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.

Returns:

  • (Class)

    the model class to use for solr documents

See Also:

  • Blacklight::Configuration#document_model

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.

Parameters:

  • ability (???)

    the user's abilities

  • action (Symbol) (defaults to: :index)

    the action the user is taking (e.g. :index, :edit, :show, etc.) (default: :index)

Returns:

  • (SolrQueryService)

    the existing service with access filters query appended


123
124
125
126
127
# File 'app/services/hyrax/solr_query_service.rb', line 123

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

#buildString

Returns the combined query that can be submitted to solr.

Returns:

  • (String)

    the combined query that can be submitted to solr


63
64
65
66
# File 'app/services/hyrax/solr_query_service.rb', line 63

def build
  return 'id:NEVER_USE_THIS_ID' if @query.blank? # forces this method to always return a valid solr query
  @query.join(' AND ')
end

#countInteger

Returns the number of results that match the query in solr.

Returns:

  • (Integer)

    the number of results that match the query in solr


57
58
59
# File 'app/services/hyrax/solr_query_service.rb', line 57

def count
  solr_service.count(build)
end

#getHash

Returns the results returned from solr for the current query.

Returns:

  • (Hash)

    the results returned from solr for the current query


30
31
32
# File 'app/services/hyrax/solr_query_service.rb', line 30

def get
  solr_service.get(build)
end

#get_idsArray<String>

Returns ids of documents matching the current query.

Returns:

  • (Array<String>)

    ids of documents matching the current query


42
43
44
45
# File 'app/services/hyrax/solr_query_service.rb', line 42

def get_ids # rubocop:disable Naming/AccessorMethodName
  results = get
  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.

Returns:


49
50
51
52
53
# File 'app/services/hyrax/solr_query_service.rb', line 49

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

#resetHyrax::SolrQueryService

Returns the existing service with the query reset to empty.

Returns:


70
71
72
73
# File 'app/services/hyrax/solr_query_service.rb', line 70

def reset
  @query = []
  self
end

#solr_documentsEnumerable<SolrDocument>

Returns:


36
37
38
# File 'app/services/hyrax/solr_query_service.rb', line 36

def solr_documents
  get['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.

Parameters:

  • field_pairs (Hash) (defaults to: {})

    a list of pairs of property name and values (e.g. { field1: values, field2: values })

  • join_with (String) (defaults to: default_join_with)

    the connector used to join the field pairs (default: ' AND ')

  • type (String) (defaults to: 'field')

    type of query to run. Either 'raw' or 'field' (default: 'field')

Returns:


112
113
114
115
116
117
# File 'app/services/hyrax/solr_query_service.rb', line 112

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.

Parameters:

  • generic_type (String) (defaults to: 'Work')

    (Default: Work)

Returns:


98
99
100
101
102
103
104
105
# File 'app/services/hyrax/solr_query_service.rb', line 98

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

Parameters:

  • ids (Array) (defaults to: [])

    id_array the ids that you want included in the query

Returns:

Raises:

  • (ArgumentError)

78
79
80
81
82
83
84
# File 'app/services/hyrax/solr_query_service.rb', line 78

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.

Parameters:

  • model (#to_s)

    a class from the model (e.g. Hyrax::Work, Hyrax::FileSet, etc.)

Returns:


89
90
91
92
93
# File 'app/services/hyrax/solr_query_service.rb', line 89

def with_model(model:)
  model_query = construct_query_for_model(model)
  @query += [model_query]
  self
end