Sha256: 3a82734407c9a6cd7f88d1aa61b7c9f23f1fd8e0e5289906be209aac38583025

Contents?: true

Size: 1.16 KB

Versions: 36

Compression:

Stored size: 1.16 KB

Contents

begin
  require 'geohash'
rescue LoadError => e
  require 'pr_geohash'
end

module Sunspot
  module Query
    class Geo
      MAX_PRECISION = 12
      DEFAULT_PRECISION = 7
      DEFAULT_PRECISION_FACTOR = 16.0

      def initialize(field, lat, lng, options)
        @field, @options = field, options
        @geohash = GeoHash.encode(lat.to_f, lng.to_f, MAX_PRECISION)
      end

      def to_params
        { :q => to_boolean_query }
      end

      def to_subquery
        "(#{to_boolean_query})"
      end

      private

      def to_boolean_query
        queries = []
        MAX_PRECISION.downto(precision) do |i|
          star = i == MAX_PRECISION ? '' : '*'
          precision_boost = Util.format_float(
            boost * precision_factor ** (i-MAX_PRECISION).to_f, 3)
          queries << "#{@field.indexed_name}:#{@geohash[0, i]}#{star}^#{precision_boost}"
        end
        queries.join(' OR ')
      end

      def precision
        @options[:precision] || DEFAULT_PRECISION
      end

      def precision_factor
        @options[:precision_factor] || DEFAULT_PRECISION_FACTOR
      end

      def boost
        @options[:boost] || 1.0
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 9 rubygems

Version Path
gojee-sunspot-2.0.5 lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.120925 lib/sunspot/query/geo.rb
sunspot_solr-2.0.0.pre.120924 sunspot/lib/sunspot/query/geo.rb
sunspot_rails-2.0.0.pre.120924 sunspot/lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.120924 sunspot/lib/sunspot/query/geo.rb
gojee-sunspot-2.0.4 lib/sunspot/query/geo.rb
gojee-sunspot-2.0.2 lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.120720 lib/sunspot/query/geo.rb
sunspot-1.3.3 lib/sunspot/query/geo.rb
sunspot-1.3.2 lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.120417 lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.120415 lib/sunspot/query/geo.rb
sunspot-1.3.1 lib/sunspot/query/geo.rb
erichummel-sunspot-2.0.0.pre.111215d lib/sunspot/query/geo.rb
erichummel-sunspot-2.0.0.pre.111215c lib/sunspot/query/geo.rb
erichummel-sunspot-2.0.0.pre.111215b lib/sunspot/query/geo.rb
erichummel-sunspot-2.0.0.pre.111215a lib/sunspot/query/geo.rb
erichummel-sunspot-2.0.0.pre.111215 lib/sunspot/query/geo.rb
sunspot-2.0.0.pre.111215 lib/sunspot/query/geo.rb
cb_sunspot-2.0.0.pre.5 lib/sunspot/query/geo.rb