思考Sphinx并在轨道上的红宝石中搜索多个字段

问题描述 投票:1回答:1

我是狮身人面像的新手,我想使用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个回答
0
投票

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

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

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

Firm.search(
  params[:search],
  conditions: {:location => params[:location]}
)
© www.soinside.com 2019 - 2024. All rights reserved.