Module: ActiveFedora::SolrQueryBuilder

Defined in:
lib/active_fedora/solr_query_builder.rb

Constant Summary collapse

PARSED_SUFFIX =
'_tesim'.freeze

Class Method Summary collapse

Class Method Details

.construct_query(field_pairs, join_with = ' AND ') ⇒ 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_:\"{!raw f=library_id_ssim}123\" AND _query_:\"{!raw f=owner_ssim}Fred\""


46
47
48
# File 'lib/active_fedora/solr_query_builder.rb', line 46

def self.construct_query(field_pairs, join_with = ' AND ')
  pairs_to_clauses(field_pairs).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



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

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

.construct_query_for_rel(field_pairs, join_with = ' AND ') ⇒ Object

Create a query with a clause for each key, value

Examples:

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

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


34
35
36
37
# File 'lib/active_fedora/solr_query_builder.rb', line 34

def self.construct_query_for_rel(field_pairs, join_with = ' AND ')
  field_pairs = field_pairs.to_a if field_pairs.is_a? Hash
  construct_query(property_values_to_solr(field_pairs), join_with)
end

.raw_query(key, value) ⇒ Object

Create a raw query clause suitable for sending to solr as an fq element



18
19
20
# File 'lib/active_fedora/solr_query_builder.rb', line 18

def self.raw_query(key, value)
  "_query_:\"{!raw f=#{key}}#{value.gsub('"', '\"')}\""
end

.solr_name(*args) ⇒ Object



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

def self.solr_name(*args)
  Solrizer.default_field_mapper.solr_name(*args)
end