Module: Ansr::Blacklight::RequestBuilders

Extended by:
ActiveSupport::Concern
Included in:
Arel::Visitors::QueryBuilder
Defined in:
lib/ansr_blacklight/request_builders.rb

Overview

This module contains methods that transform user parameters into parameters that are sent as a request to Solr when RequestBuilders#solr_search_params is called.

Instance Method Summary collapse

Instance Method Details

#add_filter_fq_to_solr(solr_request, user_params) ⇒ Object

Add any existing facet limits, stored in app-level HTTP query as :f, to solr as appropriate :fq query.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ansr_blacklight/request_builders.rb', line 80

def add_filter_fq_to_solr(solr_request, user_params)   

  # convert a String value into an Array
  if solr_request[:fq].is_a? String
    solr_request[:fq] = [solr_request[:fq]]
  end

  # :fq, map from :f. 
  if ( user_params[:f])
    f_request_params = user_params[:f] 
    
    f_request_params.each_pair do |facet_field, value_list|
      opts = local_field_params(facet_field).merge(user_params.fetch(:opts,{}))
      Array(value_list).each do |value|
        solr_request.append_filter_query filter_value_to_fq_string(facet_field, value, user_params[:opts])
      end              
    end      
  end
end

#add_query_to_solr(field_key, value, opts = {}) ⇒ Object

Take the user-entered query, and put it in the solr params, including config’s “search field” params for current search field. also include setting spellcheck.q.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ansr_blacklight/request_builders.rb', line 35

def add_query_to_solr(field_key, value, opts={})
  ###
  # Merge in search field configured values, if present, over-writing general
  # defaults
  ###
  
  if (::Arel::Nodes::As === field_key)     
    solr_request[:qt] = field_key.right.to_s
    field_key = field_key.left
  end

  search_field = table[field_key]
  ##
  # Create Solr 'q' including the user-entered q, prefixed by any
  # solr LocalParams in config, using solr LocalParams syntax. 
  # http://wiki.apache.org/solr/LocalParams
  ##
  if (Ansr::Arel::ConfiguredField === search_field && !search_field.config.empty?)
    local_params = search_field.config.fetch(:local,{}).merge(opts).collect do |key, val|
      key.to_s + "=" + solr_param_quote(val, :quote => "'")
    end.join(" ")
    solr_request[:q] = local_params.empty? ? value : "{!#{local_params}}#{value}"
    search_field.config.fetch(:query,{}).each do |k,v|
      solr_request[k] = v
    end
  else
    solr_request[:q] = value if value
  end

  ##
  # Set Solr spellcheck.q to be original user-entered query, without
  # our local params, otherwise it'll try and spellcheck the local
  # params! Unless spellcheck.q has already been set by someone,
  # respect that.
  #
  # TODO: Change calling code to expect this as a symbol instead of
  # a string, for consistency? :'spellcheck.q' is a symbol. Right now
  # rspec tests for a string, and can't tell if other code may
  # insist on a string. 
  solr_request["spellcheck.q"] = value unless solr_request["spellcheck.q"]
end

#local_field_params(facet_field) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/ansr_blacklight/request_builders.rb', line 9

def local_field_params(facet_field)
  cf = table[facet_field]
  if (cf.is_a? Ansr::Arel::ConfiguredField)
    return cf.config.fetch(:local, {})
  else
    return {}
  end
end

#solr_param_quote(val, options = {}) ⇒ Object

A helper method used for generating solr LocalParams, put quotes around the term unless it’s a bare-word. Escape internal quotes if needed.



20
21
22
23
24
25
26
27
28
29
# File 'lib/ansr_blacklight/request_builders.rb', line 20

def solr_param_quote(val, options = {})
  options[:quote] ||= '"'
  unless val =~ /^[a-zA-Z0-9$_\-\^]+$/
    val = options[:quote] +
      # Yes, we need crazy escaping here, to deal with regexp esc too!
      val.gsub("'", "\\\\\'").gsub('"', "\\\\\"") + 
      options[:quote]
  end
  return val
end

#with_ex_local_param(ex, value) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/ansr_blacklight/request_builders.rb', line 100

def with_ex_local_param(ex, value)
  if ex
    "{!ex=#{ex}}#{value}"
  else
    value
  end
end