lib/inquisitio/search_url_builder.rb in inquisitio-0.0.12 vs lib/inquisitio/search_url_builder.rb in inquisitio-0.0.13

- old
+ new

@@ -13,36 +13,48 @@ add_default_size if @arguments.nil? || @arguments[:size].nil? end def build components = [url_root] - components << (@filters.empty?? simple_query : boolean_query) + is_simple = @filters.empty? && Array(@query).size == 1 + components << (is_simple ? simple_query : boolean_query) components << return_fields_query_string components << arguments components.join("") end private def simple_query - "q=#{URI.encode(@query.gsub('\'',''))}" + "q=#{URI.encode(@query.first.gsub('\'',''))}" end def boolean_query - filters = @filters.map do |key,value| - key = key.to_s.gsub('\'','') + query_blocks = [] + + 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(' ')})" + end + + query_blocks += @filters.map do |key,value| if value.is_a?(String) - "#{key}:'#{value.to_s.gsub('\'','')}'" + "#{sanitise(key)}:'#{sanitise(value)}'" elsif value.is_a?(Array) - mapping = value.map {|v| "#{key}:'#{v.to_s.gsub('\'','')}'" }.join(" ") - "(or #{mapping})" + "(or #{value.map {|v| "#{sanitise(key)}:'#{sanitise(v)}'" }.join(" ")})" else raise InquisitioError.new("Filter values must be strings or arrays.") end end - queries = filters.join(" ") - queries = "'#{@query.to_s.gsub('\'','')}' #{queries}" if @query - "bq=#{URI.encode("(and #{queries})")}" + + "bq=#{URI.encode("(and #{query_blocks.join(' ')})")}" + end + + def sanitise(value) + value.to_s.gsub('\'',''); end def return_fields_query_string return "" if @return_fields.nil? "&return-fields=#{URI::encode(@return_fields.join(',').gsub('\'',''))}"