My application allows users to save searches. These saved searches are
planned to be executed each night with a rake task, but I'm getting
inconsistent results. It's very strange.
There are three classes of people:
class Person < ActiveRecord::Base
define_index do
# My index definition is here
end
end
class User < Person; end
class Contact < Person; end
My saved search class is Request.
class Request < ActiveRecord::Base
def self.build_all_matches
Request.all.each do |request|
request.build_matches!
end
end
def build_matches!
# All the searching takes place here
Person.search(people_search_options)
end
end
Finally, there is a rake task:
task :build_all_matches => :environment do
Request.build_all_matches
end
Debugging this, I found several confusing issues.
1. From the console or from my application, executing
Request.build_all_matches or Request.first.build_matches! produces the
expected results. However, I noticed something strange in the logs.
Even though the build_matches! method calls Person.search, the logs
have:
SELECT people.id FROM `people` WHERE (`people`.`id` IN
(963,926,1008,984,931,929,36,1009,1006,993,947,33,930,62,187,98,407,1010,49,202,1005,156,954,248,177,55,256,179,266,183,271,274,288,292,196,31,126,301,133,318,320,331,16,338,341,345,240,165,352,355,356,364,371,374,375,378,401,258,408,409,414,548,549,560,575,580,584,591,602,611,616,293,631,297,657,199,662,664,694,695,697,698,723,730,741,751,209,773,790,796,798,801,826,834,846,858,348,879,882,905,912,918,269,871,347,254,251,769,336,306,224,658,637,619,299,201,192))
AND ( (`people`.`type` = 'Contact' ) )
SELECT people.id FROM `people` WHERE (`people`.`id` IN (1336)) AND
(`people`.`activated` = 1) AND ( (`people`.`type` = 'User') )
Why is TS loading User and Contact people separately? I called
Person.search. Shouldn't it only do a Person.find(:all, :conditions =>
"people.id in (...)") and not bother trying to separate records by
type?
I bring this up because I think this is where the problem is. See
number 2.
2. Now, when I run the build_all_matches rake task and look through
the logs for the same instance of Request executing, I only see this
in the logs:
SELECT people.id FROM `people` WHERE (`people`.`id` IN (1336)) AND
(`people`.`activated` = 1) AND ( (`people`.`type` = 'User') )
The select with (`people`.`type` = 'Contact' ) is not executed, and
the results of build_matches! is incorrect. There is only 1 person
returned from Person.search instead of the 118 I expected. I verified
with several log statements that the same inputs are happening. It's
always the load of people.type = 'User' that is skipped on all Request
objects, but ONLY when I run it from the rake task. Same thing
happens if I make rake task that only calls build_matches! on a single
Request object.
Rake, console, searchd, and web app are all running as the same user.
I am using version 1.2.7 of the gem on Rails 2.3.3, Ruby 1.8.6, and
rake 0.8.7.
Help! I've been staring at this for hours, and it doesn't make any
sense. I'm guessing it's something about the way rake loads Rails that
is causing TS to get confused, but I haven't found where or how yet.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---