使用 ThinkingSphinx,如何查询最小和最大重量

Using ThinkingSphinx, How to query min and max weight

我想使用 ThinkingSphinx 检索 min(weight())max(weight())。我试过这个:

results = Work.search(
            'example', :ranker => "expr('sum((4*lcs+2*(min_hit_pos==1)+exact_hit)*user_weight)*1000+bm25*20')",
            :select => 'min(weight()) as min_weight, max(weight()) as max_weight')
results.context[:panes] << ThinkingSphinx::Panes::WeightPane

转换为这个狮身人面像表达式:

SELECT min(weight()) as min_weight, max(weight()) as max_weight 
FROM `work` 
WHERE MATCH('example') 
LIMIT 0, 20 
OPTION ranker=expr('sum((4*lcs+2*(min_hit_pos==1)+exact_hit)*user_weight)*1000+bm25*20')

如果我 运行 上面的 sphinx 表达式直接针对 sphinx 它有效,但是 Work.search 调用错误:

undefined method `constantize' for nil:NilClass

查看代码库后,我发现该错误与 :select 子句中没有 sphinx_internal_class 相关,此外,我现在明白这将是一个问题,因为ThinkingSphinx 想查询 ActiveRecord 等等

所以我的问题是:

ThinkingSphinx 是否可以简单地 return 返回原始 sphinx 查询结果并避免在 sphinx 查询后查询 ActiveRecord

正如所指出的here:

Perform a standard TS search, but use the ‘raw’ middleware set (which doesn’t translate Sphinx results into ActiveRecord objects):

results = Model.search ‘foo’, :middleware =>
              ThinkingSphinx::Middlewares::RAW_ONLY

所以改成这个给了我原始结果:

results = Work.search(
        'example', :ranker => "expr('sum((4*lcs+2*(min_hit_pos==1)+exact_hit)*user_weight)*1000+bm25*20')",
        :select => 'min(weight()) as min_weight, max(weight()) as max_weight'),
        :middleware => ThinkingSphinx::Middlewares::RAW_ONLY