【发布时间】:2012-06-10 19:00:42
【问题描述】:
将 Elasticsearch 与 Rails 3 和轮胎 gem 结合使用。
我在几个领域都有工作需要,但我现在有一个特殊要求,不确定是否可行。 我的模型项目中有两个字段都存储相同的值:Country1 和 Country2
用户最多可以为一个项目存储两个国家/地区。两者的下拉菜单相同。两个字段都不是必需的。
我想要的是一个“合并”来自 Country1 和 Country2 的值并智能地处理点击这些方面的单一方面(即无论是在 1 还是在 2 中都会找到它)
到目前为止,这是我的模型:(注意 Country1/2 可以是多个单词)
class Project < ActiveRecord::Base
mapping do
indexes :id
indexes :title, :boost => 100
indexes :subtitle
indexes :country1, :type => 'string', :index => 'not_analyzed'
indexes :country2, :type => 'string', :index => 'not_analyzed'
end
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 10) do
query do
boolean do
must { string params[:query], default_operator: "AND" } if params[:query].present?
must { term :country1, params[:country] } if params[:country].present?
end
end
sort { by :display_type, "desc" }
facet "country" do
terms :country1
end
end
end
非常感谢任何提示!
【问题讨论】:
标签: ruby-on-rails search elasticsearch tire