Sha256: 9de38f2a8f8b7662ea0d135b5f82359956b923f093809d1d48f17de3cd9efbc3

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module Geoblacklight
  class SearchBuilder  < Blacklight::Solr::SearchBuilder
    self.default_processor_chain += [:add_spatial_params]

    def initialize(processor_chain, scope)
      super(processor_chain, scope)
      @processor_chain += [:add_spatial_params] unless @processor_chain.include?(:add_spatial_params)
    end

    ##
    # Adds spatial parameters to a Solr query if :bbox is present.
    # @param [Blacklight::Solr::Request] solr_params :bbox should be in Solr
    # :bbox should be passed in using Solr lat-lon rectangle format e.g.
    # "minX minY maxX maxY"
    # @return [Blacklight::Solr::Request]
    def add_spatial_params(solr_params)
      if blacklight_params[:bbox]
        solr_params[:bq] ||= []
        solr_params[:bq] = ["#{Settings.GEOMETRY_FIELD}:\"IsWithin(#{envelope_bounds})\"^10"]
        solr_params[:fq] ||= []
        solr_params[:fq] << "#{Settings.GEOMETRY_FIELD}:\"Intersects(#{envelope_bounds})\""
      end
      solr_params
    rescue Geoblacklight::Exceptions::WrongBoundingBoxFormat
      # TODO: Potentially delete bbox params here so that its not rendered as search param
      solr_params
    end

    ##
    # @return [String]
    def envelope_bounds
      bounding_box.to_envelope
    end

    ##
    # Returns a Geoblacklight::BoundingBox built from the blacklight_params
    # @return [Geoblacklight::BoundingBox]
    def bounding_box
      Geoblacklight::BoundingBox.from_rectangle(blacklight_params[:bbox])
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geoblacklight-0.11.1 lib/geoblacklight/search_builder.rb
geoblacklight-0.11.0 lib/geoblacklight/search_builder.rb
geoblacklight-0.10.2 lib/geoblacklight/search_builder.rb
geoblacklight-0.10.1 lib/geoblacklight/search_builder.rb