lib/inquisitio/search_url_builder.rb in inquisitio-2.0.0 vs lib/inquisitio/search_url_builder.rb in inquisitio-2.1.0
- old
+ new
@@ -4,79 +4,79 @@
def self.build(*args)
new(*args).build
end
def initialize(options = {})
- @query = options[:query]
- @named_fields = options[:named_fields] || {}
+ @query_terms = options[:query][:terms]
+ @query_named_fields = options[:query][:named_fields] || {}
+ @filter_query = options[:filter_query] || {}
+ @filter_query_terms = @filter_query[:terms] || []
+ @filter_query_named_fields = @filter_query[:named_fields] || {}
+ @facets = options[:facets] || {}
@q_options = options[:q_options] || {}
@expressions = options[:expressions] || {}
- @arguments = options[:arguments] || {}
@return_fields = options[:return_fields] || []
@size = options[:size] || Inquisitio.config.default_search_size
@start = options[:start] || 0
@sort = options[:sort] || {}
- @q_parser = options[:q_parser] || (is_simple? ? nil : :structured)
+ @q_parser = options[:q_parser] || (is_simple?(@query_terms, @query_named_fields) ? nil : :structured)
end
def build
- components = [url_root]
- components << (is_simple? ? simple_query : boolean_query)
- components << "&q.parser=#{@q_parser}" if @q_parser && Inquisitio.config.api_version == '2013-01-01'
- components << "&return=#{CGI::escape(@return_fields.join(',').gsub('\'', ''))}" unless @return_fields.empty?
- components << arguments
- components << '&q.options=' + CGI::escape(@q_options.to_json) unless @q_options.empty?
+ components = []
+ components << 'q=' + CGI::escape(build_query(@query_terms, @query_named_fields))
+ if !@filter_query_terms.empty? || !@filter_query_named_fields.empty?
+ components << 'fq=' + CGI::escape(build_query(@filter_query_terms, @filter_query_named_fields))
+ end
+ components << "q.parser=#{@q_parser}" if @q_parser
+ components << "return=#{CGI::escape(@return_fields.join(',').gsub('\'', ''))}" unless @return_fields.empty?
+ # components << arguments
+ @facets.each do |field,settings|
+ components << "facet.#{field}=#{CGI::escape(settings.to_json)}"
+ end
+ components << 'q.options=' + CGI::escape(@q_options.to_json) unless @q_options.empty?
@expressions.each do |name, expression|
- components << "&expr.#{name}=" + CGI::escape(expression)
+ components << "expr.#{name}=" + CGI::escape(expression)
end
- 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.join('')
+ components << "size=#{@size}"
+ components << "start=#{@start}" unless @start == 0 || @start == '0'
+ components << 'sort=' + @sort.map { |k, v| "#{k}%20#{v}" }.join(',') unless @sort.empty?
+ url_root + components.join('&')
end
- def is_simple?
- @named_fields.empty? && Array(@query).size == 1
+ def is_simple?(terms, named_fields)
+ Array(terms).size == 1 && named_fields.empty?
end
private
- def simple_query
- "q=#{CGI::escape(@query.first)}"
- end
- def boolean_query
-
+ def build_query(terms, named_fields)
+ return terms.first if is_simple?(terms, named_fields)
query_blocks = []
+ query_blocks << '(and' unless terms.empty? || named_fields.empty?
- 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(' ')})"
+ if terms.size == 1
+ query_blocks << "'#{dequote(terms.first)}'"
+ elsif terms.size > 1
+ query_blocks << "(or #{terms.map { |q| "'#{dequote(q)}'" }.join(' ')})"
end
- query_blocks += @named_fields.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(" ")})"
- else
- raise InquisitioError.new('Filter values must be strings or arrays.')
- end
+ return query_blocks.join(' ') if named_fields.empty?
+
+ query_blocks += named_fields.map do |key, value|
+ raise InquisitioError.new('Named field values must be strings or arrays.') unless value.is_a?(String) || value.is_a?(Array)
+ block = "#{dequote(key)}:'#{dequote(value)}'" if value.is_a?(String)
+ block = "#{dequote(key)}:'#{dequote(value.first)}'" if value.is_a?(Array) && value.size == 1
+ block = "(or #{value.map { |v| "#{dequote(key)}:'#{dequote(v)}'" }.join(' ')})" if value.is_a?(Array) && value.size > 1
+ block
end
- "q=#{CGI::escape("(and #{query_blocks.join(' ')})").gsub('&', '%26')}"
+ query_blocks << ')' unless terms.empty? || named_fields.empty?
+ query_blocks.join(' ')
end
- def sanitise(value)
+ def dequote(value)
value.to_s.gsub('\'', '')
- end
-
-
- def arguments
- return '' if @arguments.nil?
- @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