Success! My hardcoding fixes:
namespace :thinking_sphinx do
desc "Fix associations in configuration file"
task :configure do
Rake::Task['thinking_sphinx:fix_config'].execute
end
desc "Fix associations in configuration file"
task :index do
unless ENV["INDEX_ONLY"] == "true"
config = ThinkingSphinx::Configuration.instance
Rake::Task['thinking_sphinx:fix_config'].execute
FileUtils.mkdir_p config.searchd_file_path
cmd = "#{config.bin_path}indexer --config #{config.config_file}
--all"
cmd << " --rotate" if sphinx_running?
puts cmd
system cmd
end
end
desc "Fix Sphinx config"
task :fix_config do
config = ThinkingSphinx::Configuration.instance
puts "Fix #{config.config_file}"
fixed_text = nil
File.open(config.config_file, 'r+') do |file|
text = file.read
fixed_text = text.dup
unless text.blank?
[ 'post', 'album' ].each do |index|
core_regexp = Regexp.new("source #{index}_0_core\n\{(.+?)
\}", Regexp::MULTILINE)
core_index = core_regexp.match(text).to_s
unless core_index.include?("LEFT OUTER JOIN `sites` ON
`sites`.id = `#{index.pluralize}`.owner_id AND `#{index.pluralize}
`.`owner_type` = 'Site'")
fixed_text.gsub!(core_regexp, core_index.gsub('LEFT OUTER
JOIN `clubs`', "LEFT OUTER JOIN `sites` ON `sites`.id = `#
{index.pluralize}`.owner_id AND `#{index.pluralize}`.`owner_type` =
'Site' LEFT OUTER JOIN `clubs`"))
delta_regexp = Regexp.new("source #{index}_0_delta : #
{index}_0_core\n\{(.+?)\}", Regexp::MULTILINE)
delta_index = delta_regexp.match(text).to_s
fixed_text.gsub!(delta_regexp, delta_index.gsub('LEFT
OUTER JOIN `clubs`', "LEFT OUTER JOIN `sites` ON `sites`.id = `#
{index.pluralize}`.owner_id AND `#{index.pluralize}`.`owner_type` =
'Site' LEFT OUTER JOIN `clubs`"))
end
end
end
end
unless fixed_text.blank?
File.open(config.config_file, 'w') do |f|
f.write(fixed_text)
end
end
end
end
On 14 дек, 15:24, Shumkov <[email protected]> wrote:
> Thank you, i'm try this!
>
> On 14 дек, 09:15, Pat Allan <[email protected]> wrote:
>
> > The only way currently to force a join is to run
> > thinking_sphinx:configure, then edit the generated SQL, then run
> > rake thinking_sphinx:index INDEX_ONLY=true
>
> > I know it's not elegant, but it's the best solution at the moment.
>
> > Cheers
>
> > --
> > Pat
>
> > On 13/12/2008, at 9:19 PM, Shumkov wrote:
>
> > > Its does not work, becouse posts not owned projects in database.
> > > Posts aleady have attribute from owner (has owner.city_id, :as
> > > => :city_id).
>
> > > How i'm can force join or maybe add sql condition in where (but as i'm
> > > known it's imposible)?
>
> > > On 13 дек, 10:01, Pat Allan <[email protected]> wrote:
> > >> To force joins to all the polymorphic tables, you'll need to add a
> > >> field or - probably better - an attribute that uses a common field
> > >> across all the appropriate models. The best choice is, of course, id.
>
> > >> So:
>
> > >> has owner(:id), :as => :owner_id
>
> > >> And then both clubs and projects should be included in the SQL
> > >> statement *IF* there are posts owned by projects in the database.
> > >> Thinking Sphinx can't figure out by itself which models are
> > >> appropriate for polymorphic joins, so it looks at the existing data.
> > >> If there's no data joining posts to clubs or projects, then they
> > >> won't
> > >> have the needed joins. I know it's not an ideal situation, but I
> > >> haven't found a better way yet.
>
> > >> --
> > >> Pat
>
> > >> On 12/12/2008, at 10:01 PM, Shumkov wrote:
>
> > >>> Hello, i have some problem with index conditions.
> > >>> I'm have Post model with polymorphic relationship with Club model
> > >>> and
> > >>> Project model. And i can't index posts if club or project is not
> > >>> active.
>
> > >>> Look this:
>
> > >>> class Post < ActiveRecord::Base
>
> > >>> belongs_to :owner, :polymorphic => true
>
> > >>> define_index do
> > >>> indexes title, cached_tag_list
> > >>> indexes body, :html_strip => true
>
> > >>> has owner.city_id, :as => :city_id
>
> > >>> where "(`clubs`.activated_at IS NOT NULL OR
> > >>> `projects`.activated_at IS NOT NULL)"
>
> > >>> set_property :match_mode => :boolean
> > >>> set_property :delta => true
> > >>> end
>
> > >>> end
>
> > >>> class Club < ActiveRecord::Base
> > >>> has_many :posts, :as => :owner
> > >>> end
>
> > >>> class Project < ActiveRecord::Base
> > >>> has_many :posts, :as => :owner
> > >>> end
>
> > >>> If i'm have projects and clubs and posts index sql query is good!
> > >>> But
> > >>> if i'm don't have Project for example in index sql query join with
> > >>> table projects is not present. And i'm have error on indexing:
>
> > >>> ERROR: index 'post_core': sql_range_query: Unknown column
> > >>> 'projects.activated_at' in 'where clause' (DSN=mysql://
> > >>> root:*...@localhost:3306/clubovorot).
>
> > >>> What can i do?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---