Class: Hyrax::SolrQueryBuilderService

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

Overview

Methods in this class are from/based on ActiveFedora::SolrQueryBuilder

Class Method Summary collapse

Class Method Details

.construct_query(field_pairs, join_with = default_join_with, type = 'field') ⇒ String

Construct a solr query from a list of pairs (e.g. [field name, values])

Examples:

construct_query([['library_id_ssim', '123'], ['owner_ssim', 'Fred']])
# => "_query_:\"{!field f=library_id_ssim}123\" AND _query_:\"{!field f=owner_ssim}Fred\""

Parameters:

  • field_pairs (Hash)

    a list of pairs of property name and values

  • join_with (String) (defaults to: default_join_with)

    the value we’re joining the clauses with (default: ‘ AND ’)

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

    of query to run. Either ‘raw’ or ‘field’ (default: ‘field’)

Returns:

  • (String)

    a solr query



24
25
26
27
28
29
# File 'app/services/hyrax/solr_query_builder_service.rb', line 24

def construct_query(field_pairs, join_with = default_join_with, type = 'field')
  clauses = pairs_to_clauses(field_pairs, type)
  return "" if clauses.count.zero?
  return clauses.first if clauses.count == 1
  "(#{clauses.join(join_with)})"
end

.construct_query_for_ids(id_array) ⇒ Object

Construct a solr query for a list of ids This is used to get a solr response based on the list of ids in an object’s RELS-EXT relationhsips If the id_array is empty, defaults to a query of “id:NEVER_USE_THIS_ID”, which will return an empty solr response

Parameters:

  • id_array (Array)

    the ids that you want included in the query



10
11
12
13
14
# File 'app/services/hyrax/solr_query_builder_service.rb', line 10

def construct_query_for_ids(id_array)
  ids = id_array.reject(&:blank?)
  return "id:NEVER_USE_THIS_ID" if ids.empty?
  "{!terms f=#{Hyrax.config.id_field}}#{ids.join(',')}"
end

.construct_query_for_model(model, field_pairs, join_with = default_join_with, type = 'field') ⇒ String

Construct a solr query from a list of pairs (e.g. [field name, values]) including the model (e.g. Collection, Monograph)

Examples:

construct_query(Collection, [['library_id_ssim', '123'], ['owner_ssim', 'Fred']])
# => "_query_:\"{!field f=has_model_ssim}Collection\" AND _query_:\"{!field f=library_id_ssim}123\" AND _query_:\"{!field f=owner_ssim}Fred\""

Parameters:

  • model (Class)

    class

  • field_pairs (Hash)

    a list of pairs of property name and values

  • join_with (String) (defaults to: default_join_with)

    the value we’re joining the clauses with (default: ‘ AND ’)

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

    of query to run. Either ‘raw’ or ‘field’ (default: ‘field’)

Returns:

  • (String)

    a solr query



44
45
46
47
# File 'app/services/hyrax/solr_query_builder_service.rb', line 44

def construct_query_for_model(model, field_pairs, join_with = default_join_with, type = 'field')
  field_pairs["has_model_ssim"] = model.to_s
  construct_query(field_pairs, join_with, type)
end

.default_join_withObject



31
32
33
# File 'app/services/hyrax/solr_query_builder_service.rb', line 31

def default_join_with
  ' AND '
end