如何使用Thinking sphinx执行sphinx multi查询



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

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

使用ThinkingSphinx与Rails应用程序,是否有任何方法我可以使用这个功能?

(仅供参考,我的TS版本是2.0.11,但我想知道它是否可以做无论如何版本3。X

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

最新更新