2013-05-20 38 views
0

我的主要目标是一次执行多个狮身人面像查询。 他们可以在不同的模型/表格或一些共同的。 最终结果应该按查询方式进行分组。如何使用思维狮身人面像执行狮身人面像多查询

这似乎在狮身人面像多查询得到支持:http://sphinxsearch.com/docs/2.0.7/multi-queries.html

使用ThinkingSphinx与Rails应用程序,有没有什么办法,我可以使用这个功能?

(FYI我的TS版本是2.0.11,但是我想知道是否可以用反正3.x版本,如果不是2.x版完成)

回答

2

在思考狮身人面像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 

的思维狮身人面像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,因为这仍然是旧的语法,但是使用连接池,所以即使你没有将所有这些查询集中在一起,套接字设置开销也会尽可能地减少尽可能。