Search code examples
ruby-on-railsthinking-sphinx

ThinkingSphinx: Viewing Result Weights when using BatchedSearch?


On ThiningSphinx v3 how do I retrieve search result weights in combination with BatchedSearch?

Im using a BatchedSearch as described here:

batch = ThinkingSphinx::BatchedSearch.new
batch.searches << Article.search('foo', :select => '*, weight()')
batch.searches << Article.search(:conditions => {:name => 'bar'}, :select => '*, weight()')
batch.searches << Article.search_for_ids('baz', :select => '*, weight()')
batch.populate
res = batch.searches #=> [[foo results], [bar results], [baz results]]

In each of these 3 search result sets, i want to access the Result Weights but it seems that the weights can only be accessed through a single search object? After the above code block, i have tried:

res.each do |w|
 w.context[:panes] << ThinkingSphinx::Panes::WeightPane
end

res.first.first #is a valid search result

res.first.first.weight
#returns error: 
#<NoMethodError: undefined method `weight' for #<Work:0xb2a9a648>>

Solution

  • You need to add the pane to each search object before you populate the results. So, try the following instead:

    batch = ThinkingSphinx::BatchedSearch.new
    batch.searches << Article.search('foo', :select => '*, weight()')
    batch.searches << Article.search(:conditions => {:name => 'bar'}, :select => '*, weight()')
    batch.searches << Article.search_for_ids('baz', :select => '*, weight()')
    batch.searches.each do |search|
      search.context[:panes] << ThinkingSphinx::Panes::WeightPane
    end
    batch.populate
    
    res = batch.searches
    res.first.first.weight