如何使用Thinking Sphinx执行sphinx多查询

时间:2013-05-20 11:32:22

标签: search sphinx thinking-sphinx

我的主要目标是一次执行多个Sphinx查询。 它们可以在不同的模型/表格或某些公共表格上。 最终结果应按查询分组。

这似乎在Sphinx中支持多重查询:http://sphinxsearch.com/docs/2.0.7/multi-queries.html

将ThinkingSphinx与Rails应用程序一起使用,有什么办法可以使用这个功能。?

(仅供参考我的TS版本是2.0.11,但我想知道是否可以使用版本3.x进行,如果不是2.x)

1 个答案:

答案 0 :(得分:2)

在思考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,因为它仍然是旧语法,但使用连接池,即使你没有将所有这些查询一起批处理,套接字设置开销也会尽可能地减少。