Module: ActiveFedora::SolrQueryBuilder

Defined in:
lib/active_fedora/solr_query_builder.rb

Class Method Summary collapse

Class Method Details

.construct_query(field_pairs, join_with = default_join_with, type = 'field'.freeze) ⇒ 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 (Array<Array>)

    a list of pairs of property name and values

  • join_with (String) (defaults to: default_join_with)

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

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

    (‘field’) The type of query to run. Either ‘raw’ or ‘field’

Returns:

  • (String)

    a solr query



35
36
37
38
39
40
# File 'lib/active_fedora/solr_query_builder.rb', line 35

def construct_query(field_pairs, join_with = default_join_with, type = 'field'.freeze)
  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



8
9
10
11
12
# File 'lib/active_fedora/solr_query_builder.rb', line 8

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

.construct_query_for_rel(field_pairs, join_with = default_join_with) ⇒ Object

Create a raw query with a clause for each key, value

Examples:

construct_query_for_rel [[:has_model, "ComplexCollection"], [:has_model, "ActiveFedora_Base"]], 'OR'
# => _query_:"{!raw f=has_model_ssim}ComplexCollection" OR _query_:"{!raw f=has_model_ssim}ActiveFedora_Base"

construct_query_for_rel [[Book._reflect_on_association(:library), "foo/bar/baz"]]

Parameters:

  • field_pairs (Hash, Array<Array<String>>)

    key is the predicate, value is the target_uri

  • join_with (String) (defaults to: default_join_with)

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



22
23
24
25
# File 'lib/active_fedora/solr_query_builder.rb', line 22

def construct_query_for_rel(field_pairs, join_with = default_join_with)
  field_pairs = field_pairs.to_a if field_pairs.is_a? Hash
  construct_query(property_values_to_solr(field_pairs), join_with, 'raw'.freeze)
end

.default_join_withObject



42
43
44
# File 'lib/active_fedora/solr_query_builder.rb', line 42

def default_join_with
  ' AND '
end