【问题标题】:Thinking Sphinx and searching with multiple fields in ruby on rails思考 Sphinx 并在 ruby​​ on rails 中搜索多个字段
【发布时间】:2017-12-26 08:36:04
【问题描述】:

我是 sphinx 的新手,我想使用 sphinx 在模型中进行搜索,我做到了,效果很好,但只有一个字段

这是搜索字段

<%= form_tag welcome_result_path, target: '_blank' , method: :get do%>
    <%= text_field_tag :search, params[:search]%>
    <%= submit_tag 'Search',name: nil %>
<%end%> 

这是我的行动

def result
@firms = ThinkingSphinx.search(params[:search],classes:[Firm])
end

这是我的索引

ThinkingSphinx::Index.define :firm, with: :active_record do

# fields
indexes corporate_name, :sortable => true, :as => :rs
indexes id, :as => :newid
indexes external_id, :as => :oldid
indexes gruik_code, :as => :gruik
#indexes country_id, :as => :country  ====> not sure we need this (?)
# indexes usual_corporate_name, :as => :rsu
indexes [contacts.first_name, contacts.last_name], :as => :nom_contact
indexes contacts.role, :as => :role_contact
indexes contacts.contact_methods.value, :as => :second
indexes contacts.contact_methods.method_type, :as => :contact_method
indexes categories(:id), :as => :categorie_id
indexes categories.label, :as => :categorie
indexes [addresses.city.region.name, addresses.free_region], :as => :wilaya
indexes [addresses.street_1, addresses.street_2, addresses.street_3], :as => :adresse
indexes [addresses.city.region.name, addresses.city.name, addresses.street_1, addresses.street_2, addresses.street_3], :as => :ou
indexes [addresses.city.name, addresses.free_city], :as => :ville
indexes tags.name, :as => :tag
indexes categories.tags.name, :as => :categorie_tag
indexes properties.value, :as => :prop
indexes keywords, :as => :keywords
indexes vip_keywords, :as => :vip_keywords
indexes description, :as => :description
end

我想为该位置再添加一个字段

【问题讨论】:

    标签: ruby-on-rails-5 thinking-sphinx


    【解决方案1】:

    如果您想搜索 Sphinx 索引中的特定字段,可以在搜索调用中使用 :conditions 选项:

    ThinkingSphinx.search(
      params[:search],
      classes:    [Firm],
      conditions: {:location => params[:location]}
    )
    

    另外,与您的问题无关,只是您共享的代码中的一些内容:如果您只搜索一个模型,您可以在该模型上使用 search 方法,而不是通过 @987654324 @选项:

    Firm.search(
      params[:search],
      conditions: {:location => params[:location]}
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      相关资源
      最近更新 更多