Sha256: c29ae1251ab6e793e3733309b4b0c534fad9f7596ea2d509d79983e3c5e5fb94

Contents?: true

Size: 1.84 KB

Versions: 7

Compression:

Stored size: 1.84 KB

Contents

require File.expand_path('../spec_helper', File.dirname(__FILE__))

describe 'local search' do
  ORIGIN = [40.7246062, -73.9969018]
  LOCATIONS = [
    [40.7246062, -73.9969018], # dr5rsjtn50yf
    [40.724606, -73.996902],   # dr5rsjtn50y9
    [40.724606, -73.996901],   # dr5rsjtn50z3
    [40.72461, -73.996906]     # dr5rsjtn51ec
  ].map { |lat, lng| Sunspot::Util::Coordinates.new(lat, lng) }

  before :each do
    Sunspot.remove_all
  end

  describe 'without fulltext' do
    before :each do
      @posts = LOCATIONS.map do |location|
        Post.new(:coordinates => location)
      end
      Sunspot.index!(@posts)
      @search = Sunspot.search(Post) do
        with(:coordinates).near(ORIGIN[0], ORIGIN[1], :precision_factor => 4.0)
      end
    end

    it 'should return results in geo order' do
      expect(@search.results).to eq(@posts)
    end

    it 'should asssign higher score to closer locations' do
      hits = @search.hits
      hits[1..-1].each_with_index do |hit, i|
        expect(hit.score).to be < hits[i].score
      end
    end
  end

  describe 'with fulltext' do
    before :each do
      @posts = [
        Post.new(:title => 'pizza', :coordinates => LOCATIONS[0]),
        Post.new(:title => 'pizza', :coordinates => LOCATIONS[1]),
        Post.new(:title => 'pasta calzone pizza antipasti', :coordinates => LOCATIONS[1])
      ]
      Sunspot.index!(@posts)
      @search = Sunspot.search(Post) do
        keywords 'pizza'
        with(:coordinates).near(ORIGIN[0], ORIGIN[1])
      end
    end

    it 'should take both fulltext and distance into account in ordering' do
      expect(@search.results).to eq(@posts)
    end

    it 'should take both fulltext and distance into account in scoring' do
      hits = @search.hits
      hits[1..-1].each_with_index do |hit, i|
        expect(hit.score).to be < hits[i].score
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sunspot-2.7.1 spec/integration/local_search_spec.rb
sunspot-2.7.0 spec/integration/local_search_spec.rb
sunspot-2.6.0 spec/integration/local_search_spec.rb
sunspot-2.5.0 spec/integration/local_search_spec.rb
sunspot-2.4.0 spec/integration/local_search_spec.rb
sunspot-2.3.0 spec/integration/local_search_spec.rb
sunspot-2.2.8 spec/integration/local_search_spec.rb