How do I geo-search multiple models with ThinkingSphinx?

前提是你 提交于 2019-12-06 14:18:15

问题


I have two models indexed for searching (User and Item). I'm trying to do a geo-search across models:

ThinkingSphinx::Search.search('keywords', :geo => [ degrees_to_radians(params[:lat].to_f), degrees_to_radians(params[:lon].to_f) ], )

But I only get an error:

Sphinx Error: index item_core,item_delta,user_core,user_delta: unknown latitude attribute ''

Searching each model individually works fine... but I've no idea what the problem here is. Here are the indexes:

User Index:

define_index do
    indexes [:first_name, :last_name], :as => :name
    indexes login
    indexes email
    indexes headline
    indexes description
    indexes business.name, :as => :business_name
    indexes [addresses.street_1, addresses.street_2, addresses.city, addresses.postal_code], :as => :address

    has created_at, :sortable => true
    has addresses.latitude, :as => :latitude, :type => :float
    has addresses.longitude, :as => :longitude, :type => :float    

    set_property :delta => true
  end    

Item index:

define_index do
    indexes title, :sortable => true
    indexes description
    indexes [address.street_1, address.street_2, address.city, address.postal_code], :as => :address
    indexes images.title, :as => :image_titles
    indexes images.description, :as => :image_descriptions
    indexes categories(:name), :as => :category_names    

    has price, :sortable => true
    has created_at, :sortable => true
    has address.latitude, :as => :latitude, :type => :float
    has address.longitude, :as => :longitude, :type => :float    
    has categories(:id), :as => :category_ids

    where "`items`.`state` = 'for_sale'"

    set_property :delta => true    
  end

回答1:


This is a late response, but better than nothing, hopefully:

When you're not searching on a specific model, Thinking Sphinx has no reference point for knowing what attributes are available, so you need to explicitly tell it the lat and long attributes to use:

ThinkingSphinx::Search.search('keywords',
  :geo => [
    degrees_to_radians(params[:lat].to_f),
    degrees_to_radians(params[:lon].to_f)
  ],
  :latitude_attr  => "latitude",
  :longitude_attr => "longitude"
)


来源:https://stackoverflow.com/questions/266048/how-do-i-geo-search-multiple-models-with-thinkingsphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!