ddnexus / flex

The ultimate ruby client for elasticsearch.
http://ddnexus.github.io/flex/doc/
72 stars 15 forks source link

Indexing with Parent/Child relationships #7

Closed spodlecki closed 11 years ago

spodlecki commented 11 years ago

Gemfile:

gem 'redis' gem 'rest-client' gem "flex-rails", "~> 1.0.4", :git => 'git@github.com:ddnexus/flex.git', :branch => '1.0.4' gem 'flex-admin', "~> 1.0.4", :git => 'git@github.com:ddnexus/flex.git', :branch => '1.0.4' gem 'flex-models', "~> 1.0.4", :git => 'git@github.com:ddnexus/flex.git', :branch => '1.0.4'

Top of model:

include Flex::ModelIndexer flex.sync self

belongs_to :title flex.parent :title, 'model' => 'title'

When indexing the model:

rake flex:import MODELS=****


Model *: Processing 2239 documents in batches of 1000: rake aborted!: 0% | | ETA: --:--:-- undefined method `routing' for #Flex::ClassProxy::Base:0x007fa92c249820 /Users/**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/instance_proxy/model_indexer.rb:78:in `routing' /Users/**_/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/instance_proxy/modelindexer.rb:78:in `routing' /Users/**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex/lib/flex/utility_methods.rb:112:in `bulk_string_from_flex' /Users/**_/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex/lib/flex/utility_methods.rb:82:in build_bulk_string' /Users/**_**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex/lib/flex/utility_methods.rb:72:inblock in post_bulkcollection' /Users/****/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex/lib/flex/utilitymethods.rb:71:in `each' /Users/**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex/lib/flex/utility_methods.rb:71:in `post_bulk_collection' /Users/**_/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/model_tasks.rb:94:in block (2 levels) in import_models' /Users/**_**/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/batches.rb:72:infind_inbatches' /Users/****/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/querying.rb:8:in find_in_batches' /Users/**_**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/model_tasks.rb:93:inblock in importmodels' /Users/****/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/modeltasks.rb:57:in `each' /Users/**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/flex/model_tasks.rb:57:in `import_models' /Users/**/.rvm/gems/ruby-1.9.3-p429/bundler/gems/flex-41fed1554947/flex-models/lib/tasks.rake:8:in `block (2 levels) in <top (required)>' /Users/****/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `eval' /Users/***/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `

'

Trying to follow your guides to the T incase I'm missing something..

ddnexus commented 11 years ago

please could you paste the relvant code of parent and child models?

ddnexus commented 11 years ago

not sure is the (only) problem, but it looks like the parent line has the inverted logic:

- flex.parent :title, 'model' => 'title'
+ flex.parent :title, 'title' => 'model'

If it is not that, please, paste both models declarations. Thanks.

spodlecki commented 11 years ago

Ok, I figured it out. The problem was (not only the inverse declaration) but also.. I'm using STI for the parent model. Is there a better way to handle this? Is it possible to have a "parent" with 2 different types? Ex: If I ended up with 2 different mappings for parent a & b (which there probably will be eventually)

Solution: class Child < ActiveRecord::Base include Flex::ModelIndexer flex.sync self flex.type = 'child' flex.parent :parent, 'parent' => 'child' end

class Parent < ActiveRecord::Base include Flex::ModelIndexer end

class ParentTypeA < Parent include Flex::ModelIndexer flex.sync self flex.type = 'parent' end

class ParentTypeB < Parent include Flex::ModelIndexer flex.sync self flex.type = 'parent' end

ddnexus commented 11 years ago

Not sure to fully understand your need, but maybe this is a situation could be handled like a polymorphic association, where child_record.parent.flex.type maybe 'parent_a' or 'parent_b'.

If that is the case, then you can probably do this in the child class:

  # child
  flex.parent :parent, 'parent_a' => 'child_a', 'parent_b' => 'child_b'

  # parent A
  flex.type = 'parent_a'

  # parent B
  flex.type = 'parent_b'

That will cause a child of a 'parent_a' to be indexed as a type 'child_a' while a child of a 'parent_b' will be indexed as type 'child_b'. That way your parents will be indexed as 2 different types (as I think you want).

Does it help?