在 Thinking Sphinx v1/v2 中,它并不是特别优雅,但这是交易:
bundle = ThinkingSphinx::BundledSearch.new
bundle.search 'foo'
bundle.search 'bar', :classes => [Article]
bundle.search 'baz', :classes => [User, Article], :with => {:active => true}
# as soon as you call `searches` on the bundle, the group of queries is sent
# through to Sphinx.
foo_search, bar_search, baz_search = bundle.searches
Thinking Sphinx v3 有点不同:
batch = ThinkingSphinx::BatchedSearch.new
foo_search = ThinkingSphinx.search 'foo'
bar_search = Article.search 'bar'
baz_search = ThinkingSphinx.search 'baz', :classes => [User, Article],
:with => {:active => true}
batch.searches += [foo_search, bar_search, baz_search]
batch.populate
# Use each of your search results objects now as you normally would.
# If you use any of them to access results before the batch.populate call,
# then that will be a separate call to Sphinx.
除此之外 - 如果您暂时要坚持使用 v2 版本,我强烈建议您升级到 Thinking Sphinx v2.1.0,因为这仍然是旧语法,但使用了连接池,因此即使您没有将所有这些查询批处理在一起,也会尽可能减少套接字设置开销。