ThinkinSphinx查询不使用sphinx_select有四个条件

时间:2012-12-21 04:56:51

标签: sphinx thinking-sphinx

我正在尝试使用ThinkingSphinx返回一个范围内的开始日期或相同范围内的结束日期的记录,基本上是在此范围内开始或结束的任何记录。

为此,我使用计算属性和sphinx_select作为per the documentation结合this post建议的日期范围,如下(假设有两条记录,record_a在范围之外开始,但是在范围内结束并且record_b在范围内开始和结束):

with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " + 
                  "IF(start_at <= #{range_end.to_i}, 1, 0) + " + 
                  "IF(end_at   >= #{range_start.to_i}, 10, 0) + " + 
                  "IF(end_at   <= #{range_end.to_i}, 10, 0) AS display"
{
  sphinx_select: with_display,
  with: {'display' =>  [2, 20, 22]},
}
=> [record_b]

但是,如果我只使用start_at条件,我会得到一条记录,如果我只使用end_at条件,它会返回两条记录。

with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " + 
                  "IF(start_at <= #{range_end.to_i}, 1, 0)  AS display" 
=> [record_b]

with_display = "*, IF(end_at >= #{range_start.to_i}, 10, 0) + " + 
                  "IF(end_at <= #{range_end.to_i}, 10, 0) AS display"
=> [record_a, record_b]

如果我正确理解这一点,拥有所有四个条件,都会导致返回record_a和record_b,因为record_a的display值应为20,而record_b应该有display价值22。

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

考虑到我想要处理的案例,我刚刚意识到我的数学错误了:

  • record_a的display为21
  • record_b的display为22

我需要做的是将我的数组更改为:

{
  sphinx_select: with_display,
  with: {'display' =>  [12, 21, 22]},
} 

按顺序处理在范围(21)内结束的记录的情况,在范围(12)内开始的记录,以及比起始的记录在范围(22)内结束

相关问题