Permalink
Browse files

Searches across expliticly set classes are limited to the appropriate…

… indexes.
  • Loading branch information...
1 parent d8564ea commit 93a50337222fb332b9a71c47c399ed00a07e878e @pat pat committed Sep 12, 2009
View
8 lib/thinking_sphinx/active_record.rb
@@ -155,6 +155,14 @@ def sphinx_name
self.name.underscore.tr(':/\\', '_')
end
+ def sphinx_index_names
+ klass = source_of_sphinx_index
+ names = ["#{klass.sphinx_name}_core"]
+ names << "#{klass.sphinx_name}_delta" if sphinx_delta?
+
+ names
+ end
+
private
def sphinx_delta?
View
13 lib/thinking_sphinx/search.rb
@@ -177,6 +177,13 @@ def offset
(current_page - 1) * per_page
end
+ def indexes
+ return options[:index] if options[:index]
+ return '*' if classes.empty?
+
+ classes.collect { |klass| klass.sphinx_index_names }.flatten.join(',')
+ end
+
def each_with_groupby_and_count(&block)
populate
results[:matches].each_with_index do |match, index|
@@ -224,7 +231,7 @@ def populate
retry_on_stale_index do
begin
log "Querying Sphinx: #{query}"
- @results = client.query query, index, comment
+ @results = client.query query, indexes, comment
rescue Errno::ECONNREFUSED => err
raise ThinkingSphinx::ConnectionError,
'Connection to Sphinx Daemon (searchd) failed.'
@@ -363,10 +370,6 @@ def star_query(query)
end
end
- def index
- options[:index] || '*'
- end
-
def comment
options[:comment] || ''
end
View
24 spec/lib/thinking_sphinx/active_record_spec.rb
@@ -1,7 +1,7 @@
require 'spec/spec_helper'
-describe "ThinkingSphinx::ActiveRecord" do
- describe "define_index method" do
+describe ThinkingSphinx::ActiveRecord do
+ describe '.define_index' do
before :each do
module ::TestModule
class TestModel < ActiveRecord::Base; end
@@ -154,7 +154,7 @@ class TestModel < ActiveRecord::Base; end
end
end
- describe "source_of_sphinx_index method" do
+ describe '.source_of_sphinx_index' do
it "should return self if model defines an index" do
Person.source_of_sphinx_index.should == Person
end
@@ -164,13 +164,13 @@ class TestModel < ActiveRecord::Base; end
end
end
- describe "to_crc32 method" do
+ describe '.to_crc32' do
it "should return an integer" do
Person.to_crc32.should be_a_kind_of(Integer)
end
end
- describe "to_crc32s method" do
+ describe '.to_crc32s' do
it "should return an array" do
Person.to_crc32s.should be_a_kind_of(Array)
end
@@ -351,4 +351,18 @@ class TestModel < ActiveRecord::Base; end
@person.primary_key_for_sphinx.should == id
end
end
+
+ describe '.sphinx_index_names' do
+ it "should return the core index" do
+ Alpha.sphinx_index_names.should == ['alpha_core']
+ end
+
+ it "should return the delta index if enabled" do
+ Beta.sphinx_index_names.should == ['beta_core', 'beta_delta']
+ end
+
+ it "should return the superclass with an index definition" do
+ Parent.sphinx_index_names.should == ['person_core', 'person_delta']
+ end
+ end
end
View
29 spec/lib/thinking_sphinx/search_spec.rb
@@ -193,6 +193,14 @@
search[3].id.should == 1
end
+ it "should use the requested classes to generate the index argument" do
+ @client.should_receive(:query) do |query, index, comment|
+ index.should == 'alpha_core,beta_core,beta_delta'
+ end
+
+ ThinkingSphinx::Search.new(:classes => [Alpha, Beta]).first
+ end
+
describe 'query' do
it "should concatenate arguments with spaces" do
@client.should_receive(:query) do |query, index, comment|
@@ -838,6 +846,27 @@
end
end
+ describe '#indexes' do
+ it "should default to '*'" do
+ ThinkingSphinx::Search.new.indexes.should == '*'
+ end
+
+ it "should use given class to determine index name" do
+ ThinkingSphinx::Search.new(:classes => [Alpha]).indexes.
+ should == 'alpha_core'
+ end
+
+ it "should add both core and delta indexes for given classes" do
+ ThinkingSphinx::Search.new(:classes => [Alpha, Beta]).indexes.
+ should == 'alpha_core,beta_core,beta_delta'
+ end
+
+ it "should respect the :index option" do
+ ThinkingSphinx::Search.new(:classes => [Alpha], :index => '*').indexes.
+ should == '*'
+ end
+ end
+
describe '.each_with_groupby_and_count' do
before :each do
@alpha = Alpha.new

0 comments on commit 93a5033

Please sign in to comment.