使用思维狮身人面像进行地理搜索

发布于 2024-12-20 07:39:43 字数 2393 浏览 1 评论 0原文

如何使用 Think Sphinx 将多个参数传递到搜索中?或者如何使用思考狮身人面像创建高级搜索字段?

背景:我正在创建一个网站,允许制造商发布他们需要在全国范围内运输的货物。承运人需要能够根据其当前位置搜索发布的负载,并返回最有利可图的负载列表,然后他们可以对其进行投标。承运人将有两个输入:他们的当前位置以及他们愿意行驶以拾取货物的半径。认为 Sphinx 看起来应该能够处理所有这些问题,但是文档没有详细介绍复杂的搜索查询。

我有一个名为 Load 的模型,它具有以下属性及其各自的类型:

# :latitude => float
# :longitude => float
# :delivery_date => date
# :delivery_latitude => float
# :delivery_longitude => float
# :weight => integer
# :truck_type => string
# :price => decimal
# :distance => float
# :delta => boolean

I已在模型上包含索引,如下所示:

define_index do
 indexes :commodity
 indexes :truck_type
 set_property :delta => true
 has weight, distance      
 has "Radians(latitude)", :as => :latitude, :type => :float
 has "Radians(longitude)", :as => :longitude, :type => :float
end

我还在 lib 文件夹中定义了一个搜索类,该文件夹使用 ruby geocoder 为我所有的宝石地理编码需求。该代码尚未经过测试,因此我不确定其正确性,但我将其包含在内是为了看看我的思维过程是否正确。

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 => var[:page] || 1, 
                    :per_page => 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 > 1 
    lat = (@geocode.first / 180.0) * Math::PI 
    lng = (@geocode.last / 180.0) * Math::PI 

    @search_options.merge!(:geo => [lat, lng], 
                           :sort_mode => :expr, 
                           :sort_by => SORT_EXPRESSION, 
                           :with => { "@geodist" => 0.0..(travel_distance * METERS_PER_MILE) }) 
    end 
  end
 Load.search(keyword, travel_distance, @search_options)
end

目标是能够进入任何控制器并定义实例变量并调用此搜索模块以返回值。 ( @loads = Search.execute(params[:search] ) 我也不知道如何显示搜索结果

有没有人必须运行类似类型的地理搜索并思考 sphinx可以分享他们的设置吗?我也需要运行延迟增量,但文档在该主题上更清晰。

How do you pass multiple parameters into a search using thinking sphinx? Or how can you create advanced search fields using thinking sphinx?

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.

I have a model called Load which has the following attributes and their respective types:

# :latitude => float
# :longitude => float
# :delivery_date => date
# :delivery_latitude => float
# :delivery_longitude => float
# :weight => integer
# :truck_type => string
# :price => decimal
# :distance => float
# :delta => boolean

I have included an index on the model as follows:

define_index do
 indexes :commodity
 indexes :truck_type
 set_property :delta => true
 has weight, distance      
 has "Radians(latitude)", :as => :latitude, :type => :float
 has "Radians(longitude)", :as => :longitude, :type => :float
end

I have also defined a search class into my lib folder which uses the ruby geocoder 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.

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 => var[:page] || 1, 
                    :per_page => 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 > 1 
    lat = (@geocode.first / 180.0) * Math::PI 
    lng = (@geocode.last / 180.0) * Math::PI 

    @search_options.merge!(:geo => [lat, lng], 
                           :sort_mode => :expr, 
                           :sort_by => SORT_EXPRESSION, 
                           :with => { "@geodist" => 0.0..(travel_distance * METERS_PER_MILE) }) 
    end 
  end
 Load.search(keyword, travel_distance, @search_options)
end

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身泪倾城 2024-12-27 07:39:44

这里有很多东西需要理解,但如果我理解正确的话,这一切都归结为搜索调用,您希望在距某个地理点一定距离内加载,对吧?如果是这样,那么只需从您的搜索请求中删除 Travel_distance 作为第二个参数,应该没问题:

Load.search keyword,
  :geo       => [lat, lng], 
  :sort_mode => :expr, 
  :sort_by   => SORT_EXPRESSION, 
  :with      => { "@geodist" => 0.0..(travel_distance * METERS_PER_MILE) }

但是考虑到我的建议实际上与您已经得到的并没有那么不同,我想知道我是否正确理解问题...

There's a lot here to grok, but if I'm understanding this right, it all comes down to the search call, where you want Loads within a certain distance from a geographical point, right? If so, then just remove travel_distance as the second argument from your search request, and it should be fine:

Load.search keyword,
  :geo       => [lat, lng], 
  :sort_mode => :expr, 
  :sort_by   => SORT_EXPRESSION, 
  :with      => { "@geodist" => 0.0..(travel_distance * METERS_PER_MILE) }

But given what I'm suggesting really isn't all that different to what you've already got, I'm wondering if I understand the question correctly...

若无相欠,怎会相见 2024-12-27 07:39:44

您可以使用geocoder gem 让您的货运/列表模型具有位置感知功能,而不是尝试重新发明轮子并欺骗 Sphinx 来做您想做的事情。这会将货运清单的纬度/经度存储在您的数据库中,并向您的模型添加地理位置查询方法。这允许您在指定距离内找到恐怖列表,如下所示:

@results = FreightListing.near('77033', 50) # Finds all listings that are within 50 miles of the zip code 77033

更多信息如下:http://www.rubygeocoder.com/< /a>

Instead of trying to reinvent the wheel and finagle Sphinx into doing what you want, you could make your Freight/Listing model location-aware using the geocoder gem. This will store the lat/long of the the freight listing in your database and adds geolocation query methods to your model. This allows you find fright listings within a specified distance like so:

@results = FreightListing.near('77033', 50) # Finds all listings that are within 50 miles of the zip code 77033

More info here: http://www.rubygeocoder.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文