lib/inquisitio/search_url_builder.rb in inquisitio-1.3.1 vs lib/inquisitio/search_url_builder.rb in inquisitio-1.4.0
- old
+ new
@@ -4,13 +4,14 @@
def self.build(*args)
new(*args).build
end
def initialize(options = {})
- @query = options[:query]
- @filters = options[:filters] || {}
- @arguments = options[:arguments] || {}
+ @query = options[:query]
+ @filters = options[:filters] || {}
+ @q_options = options[:q_options] || {}
+ @arguments = options[:arguments] || {}
@return_fields = options[:return_fields]
@size = options[:size] || Inquisitio.config.default_search_size
@start = options[:start] || 0
@sort = options[:sort] || {}
end
@@ -19,19 +20,20 @@
components = [url_root]
is_simple = @filters.empty? && Array(@query).size == 1
components << (is_simple ? simple_query : boolean_query)
components << return_fields_query_string
components << arguments
+ components << '&q.options=' + CGI::escape(@q_options.map { |k, v| "{#{k}:#{v}}" }.join('')) unless @q_options.empty?
components << "&size=#{@size}" unless @arguments[:size]
components << "&start=#{@start}" unless @arguments[:start] || @start == 0 || @start == '0'
- components << '&sort=' + @sort.map {|k,v| "#{k}%20#{v}"}.join(',') unless @sort.empty?
+ components << '&sort=' + @sort.map { |k, v| "#{k}%20#{v}" }.join(',') unless @sort.empty?
components.join('')
end
private
def simple_query
- "q=#{URI.encode(sanitise(@query.first)).gsub('&', '%26')}"
+ "q=#{CGI::escape(sanitise(@query.first)).gsub('&', '%26')}"
end
def boolean_query
query_blocks = []
@@ -39,45 +41,45 @@
if Array(@query).empty?
# query_blocks = []
elsif @query.size == 1
query_blocks << "'#{sanitise(@query.first)}'"
else
- query_blocks << "(or #{@query.map{|q| "'#{sanitise(q)}'"}.join(' ')})"
+ query_blocks << "(or #{@query.map { |q| "'#{sanitise(q)}'" }.join(' ')})"
end
- query_blocks += @filters.map do |key,value|
+ query_blocks += @filters.map do |key, value|
if value.is_a?(String)
"#{sanitise(key)}:'#{sanitise(value)}'"
elsif value.is_a?(Array)
- "(or #{value.map {|v| "#{sanitise(key)}:'#{sanitise(v)}'" }.join(" ")})"
+ "(or #{value.map { |v| "#{sanitise(key)}:'#{sanitise(v)}'" }.join(" ")})"
else
raise InquisitioError.new('Filter values must be strings or arrays.')
end
end
if Inquisitio.config.api_version == '2011-02-01'
- "bq=#{URI.encode("(and #{query_blocks.join(' ')})").gsub('&', '%26')}"
+ "bq=#{CGI::escape("(and #{query_blocks.join(' ')})").gsub('&', '%26')}"
elsif Inquisitio.config.api_version == '2013-01-01'
- "q=#{URI.encode("(and #{query_blocks.join(' ')})").gsub('&', '%26')}&q.parser=structured"
+ "q=#{CGI::escape("(and #{query_blocks.join(' ')})").gsub('&', '%26')}&q.parser=structured"
end
end
def sanitise(value)
- value.to_s.gsub('\'','')
+ value.to_s.gsub('\'', '')
end
def return_fields_query_string
return '' if @return_fields.nil?
if Inquisitio.config.api_version == '2011-02-01'
- "&return-fields=#{URI::encode(@return_fields.join(',').gsub('\'',''))}"
+ "&return-fields=#{CGI::escape(@return_fields.join(',').gsub('\'', ''))}"
elsif Inquisitio.config.api_version == '2013-01-01'
- "&return=#{URI::encode(@return_fields.join(',').gsub('\'',''))}"
+ "&return=#{CGI::escape(@return_fields.join(',').gsub('\'', ''))}"
end
end
def arguments
return '' if @arguments.nil?
- @arguments.map{|key,value| "&#{key.to_s.gsub('\'','')}=#{value.to_s.gsub('\'','')}"}.join("")
+ @arguments.map { |key, value| "&#{key.to_s.gsub('\'', '')}=#{value.to_s.gsub('\'', '')}" }.join("")
end
def url_root
"#{Inquisitio.config.search_endpoint}/#{Inquisitio.config.api_version}/search?"
end