title: Geo searching with thinking sphinx
body: <p><strong>How do you pass multiple parameters into a search using thinking sphinx? Or how can you create advanced search fields using thinking sphinx?</strong></p>
  
  <p><em>Background: I am creating a website that allows manufacturers to post freight that they need moved across the country. Carriers need to be able to search through the posted loads based upon their current location and have a list returned of the most profitable loads which they can then bid on. Carriers will have two inputs, their current location and the radius in which they are willing to travel to pick up a load. Thinking Sphinx looks like it shoud be able to handle all of these problems, however the documentation does not go into much detail for complex search queries.</em></p>
  
  <p>I have a model called Load which has the following attributes and their respective types:</p>
  
  <pre><code># :latitude =&gt; float
  # :longitude =&gt; float
  # :delivery_date =&gt; date
  # :delivery_latitude =&gt; float
  # :delivery_longitude =&gt; float
  # :weight =&gt; integer
  # :truck_type =&gt; string
  # :price =&gt; decimal
  # :distance =&gt; float
  # :delta =&gt; boolean
  </code></pre>
  
  <p>I have included an index on the model as follows:</p>
  
  <pre><code>define_index do
   indexes :commodity
   indexes :truck_type
   set_property :delta =&gt; true
   has weight, distance      
   has "Radians(latitude)", :as =&gt; :latitude, :type =&gt; :float
   has "Radians(longitude)", :as =&gt; :longitude, :type =&gt; :float
  end
  </code></pre>
  
  <p>I have also defined a search class into my lib folder which uses the <a href="http://www.rubygeocoder.com/" rel="nofollow">ruby geocoder</a> gem for all of my geocoding needs. This code has not been tested so I am unsure of its correctness, but I am including it to see if my thought process is correct.</p>
  
  <pre><code>class Search 
  
    #Inputs for the search will be a current_location string, and a distance integer (miles)
  
    # Variable needed to convert to imperial values
    METERS_PER_MILE = 1609.344 
  
    # Expression used to rank the loads
    SORT_EXPRESSION = "Sorting function tbd, combination of weight, distance, and price"
  
  def self.execute(keyword, var = {}) 
    @search_options = { :page =&gt; var[:page] || 1, 
                      :per_page =&gt; Load::PER_PAGE }
  
    unless (keyword).blank? #I am unsure if this is correct
    @geocode = Geocoder.coordinates(keyword) #returns an array ex.[keyword_latitude, keyword_longitude]
  
  
      if @geocode.success and @geocode.accuracy &gt; 1 
      lat = (@geocode.first / 180.0) * Math::PI 
      lng = (@geocode.last / 180.0) * Math::PI 
  
      @search_options.merge!(:geo =&gt; [lat, lng], 
                             :sort_mode =&gt; :expr, 
                             :sort_by =&gt; SORT_EXPRESSION, 
                             :with =&gt; { "@geodist" =&gt; 0.0..(travel_distance * METERS_PER_MILE) }) 
      end 
    end
   Load.search(keyword, travel_distance, @search_options)
  end
  </code></pre>
  
  <p>The goal is to be able to go into any controller and define an instance variable and call this search module to return the values. ( @loads = Search.execute(params[:search] ) I am also do not know how I would go about displaying the results of the search</p>
  
  <p><strong>Has anyone had to run a similar type of geo-search with thinking sphinx that could share their setup? I need to run delayed deltas as well but the documentation is clearer on that subject.</strong> </p>