【发布时间】:2017-01-30 18:06:16
【问题描述】:
设置:
- 导轨 4
- MySQL
- 思考狮身人面像
我的应用中有一个模型 (Record),有近 5 亿行。这个模型有 32 个字段,但我只关心特定 Sphinx 搜索的两个字段是 name 和 token。 name 是我使用 Sphinx 搜索的对象,token 是我想要返回以在 Rails 中执行其他操作的对象。
我的索引设置是:
ThinkingSphinx::Index.define :records, :with => :real_time do
# fields
indexes name
indexes token
# attributes
has token, as: :token_attr, type: :string
# < several additional attributes >
end
我想做的是在 :records 上查询 Sphinx 与 name 匹配并让它返回 distinct token 数组中的字符串。
这是我所拥有的:
Record.search("red", indices: %w(records), max_matches: num_tokens_i_need, group_by: :token_attr)
...其中num_tokens_i_need 通常为数千(小于 10,000)
上述查询需要 5-8 分钟才能完成。但是,当我这样做时:
Record.search("red", indices: %w(records), max_matches: num_tokens_i_need).map(&:token).uniq
搜索速度非常快(在几百毫秒内返回数百万条记录),但由于.uniq 调用,我没有回复num_tokens_i_need。
基本上我需要做的是进行快速的 Sphinx 搜索,它会为我返回给定术语(例如“红色”)的不同标记的确切数量。
如果查看我的 sphinx.conf 或其他任何内容会有所帮助,请告诉我。
【问题讨论】:
-
如果有人愿意解释为什么投反对票,我将不胜感激。
标签: mysql ruby-on-rails sphinx thinking-sphinx