Sha256: caf39f9e7502fcb78762d4fb5902cc98d19a74b9e21800794063228b768ba492
Contents?: true
Size: 1.99 KB
Versions: 9
Compression:
Stored size: 1.99 KB
Contents
require 'bigdecimal' shared_examples_for 'geohash query' do it 'searches for nearby points with defaults' do search do with(:coordinates).near(40.7, -73.5) end connection.should have_last_search_including(:q, build_geo_query) end it 'searches for nearby points with non-Float arguments' do search do with(:coordinates).near(BigDecimal.new('40.7'), BigDecimal.new('-73.5')) end connection.should have_last_search_including(:q, build_geo_query) end it 'searches for nearby points with given precision' do search do with(:coordinates).near(40.7, -73.5, :precision => 10) end connection.should have_last_search_including(:q, build_geo_query(:precision => 10)) end it 'searches for nearby points with given precision factor' do search do with(:coordinates).near(40.7, -73.5, :precision_factor => 1.5) end connection.should have_last_search_including(:q, build_geo_query(:precision_factor => 1.5)) end it 'searches for nearby points with given boost' do search do with(:coordinates).near(40.7, -73.5, :boost => 2.0) end connection.should have_last_search_including(:q, build_geo_query(:boost => 2.0)) end it 'performs both dismax search and location search' do search do fulltext 'pizza', :fields => :title with(:coordinates).near(40.7, -73.5) end expected = %Q((_query_:"{!edismax qf='title_text'}pizza" AND (#{build_geo_query}))) connection.should have_last_search_including(:q, expected) end private def build_geo_query(options = {}) precision = options[:precision] || 7 precision_factor = options[:precision_factor] || 16.0 boost = options[:boost] || 1.0 hash = 'dr5xx3nytvgs' (precision..12).map do |i| phrase = if i == 12 then hash else "#{hash[0, i]}*" end precision_boost = Sunspot::Util.format_float(boost*precision_factor**(i-12.0), 3) "coordinates_s:#{phrase}^#{precision_boost}" end.reverse.join(' OR ') end end
Version data entries
9 entries across 9 versions & 2 rubygems