为什么思考sphinx在rails上一次只限制一个属性的过滤?
我做错了什么?为什么思考sphinx在rails上一次只限制一个属性的过滤?
如果我没有带选项的附加位置工程,并且只要进入我的网站的浏览器页面就会显示结果。
当我打开页面时,所有用户每页显示20个。如果我添加with_all性别=> PARAMS [:性别],位置仍然有效和我在按性别的地点的过滤器的结果输入和结果成功返回。
如果我将种族添加到with_all散列,那么种族将起作用,并且结果已打开,但性别和位置不再有效。
这就像它只允许1个属性进行过滤。
我已经重建了几次索引,所以我不知道发生了什么。
我已经得到了位置文本搜索和2个过滤器设置1.性别,种族2.
这里是我的存储上面所有的属性配置文件表简介型号:
define_index do indexes location
has ethnicity, :type => :integer
has gender, :type => :integer
end
这里是我的控制器:
class BrowsersController < ApplicationController def index
@default_image = "/assets/default_avatar.jpg"
#gender = params[:gender].to_i
@users = Profile.search params[:location],
:page => params[:page],
:per_page => 20,
:with_all =>
{
:gender => params[:gender],
:ethnicity => params[:ethnicity]
}
end
end
我的看法形式:
<%= form_tag browsers_path, :method => 'get' do %> <p>
Location: <%= text_field_tag :location, params[:location] %><br />
Gender: <%= select_tag :gender,
options_for_select([["Select", nil],
["Male", 1],
["Female", 2]], params[:gender]) %>
<br />
Ethnicity: <%= select_tag :ethnicity,
options_for_select([["Select", nil],['Black', 1 ],['White/Caucasian', 2 ],['European', 3 ],['Asian', 4 ],['Indian', 5 ],['Middle Eastern', 6 ],['Native American', 7 ],['Hispanic', 8 ],['Mixed Race', 9 ],['Other Ethnicity', 10 ]], params[:ethnicity]) %>
<br />
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
回答:
有很多你的问题来消化,但这里的几件事情要注意的 - 也许他们会帮助:
:with_all
是在一个单一的多值属性匹配多个值 - 例如,相匹配的文章,有三个标签ID都会使用这个::with_all => {:tag_ids => [1, 2, 3]}
。然而,对于具有多个属性的过滤器来说这是完全正确的 - 这就是你看起来似乎在后面(尽管with_all与单个过滤器值的行为方式相同)。
Sphinx将nils/NULLs视为0 - 因此,如果按性别过滤但不是种族,那么控制器代码正在执行的操作是搜索具有给定性别和0种族的配置文件。也许试试这样的代替:
filters = {} filters[:gender] = params[:gender].to_i if params[:gender].present?
filters[:ethnicity] = params[:ethnicity].to_i if params[:ethnicity].present?
@users = Profile.search params[:location],
:page => params[:page],
:per_page => 20,
:with => filters
最后 - 性别和种族列是整数,是吗?如果是这样,你不需要在你的索引定义指定:type => :integer
- 即会自动完成。
以上是 为什么思考sphinx在rails上一次只限制一个属性的过滤? 的全部内容, 来源链接: utcz.com/qa/258681.html