Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define child/parent mappings? #56

Closed
jondavidford opened this issue Aug 15, 2014 · 15 comments
Closed

How to define child/parent mappings? #56

jondavidford opened this issue Aug 15, 2014 · 15 comments

Comments

@jondavidford
Copy link
Contributor

I can't figure out how to define a child/parent mapping in chewy. The following gives a routing error, presumably because column isn't being given a parent _id.

define_type Table do
  field :_id, type: 'integer', value: { id }
  field :user_ids, value: -> { server.users_for_table(self) }
end

define_type Column.includes(:table) do
  root _parent: { type: 'table' } do
    field :_parent, value: -> { table.id }
  end
end

I can't find any examples of parent ids being given anywhere other than the url (like the examples here http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/indexing-parent-child.html). Is it possible to specify a parent id in chewy currently or does there need to be some code added?

@pyromaniac
Copy link
Contributor

Yeah, currently there is no way to do it. Indexing objects with parent is not supported for now. I need to dig deeper to figure out how to do this.

Currently I think it should be like this:

define_type Column.includes(:table) do
  root _parent: { type: 'table' }, parent: -> { table.id } do
    field ...
  end
end

Or maybe use parent_id key instead of just parent to define parent id evaluator proc

@jondavidford
Copy link
Contributor Author

Okay, I may work on this but for now it'd be nice to have a work-around for my use case. Basically there is a field in the parent that is expensive to calculate and is also needed for each of its children. If I could store results for this expensive function in a hash at index time then I could just look up in the hash instead of having to recalculate for each child.

I tried doing something like this but just getting Nil for the @table_permissions_store.

@table_permissions_store = TablePermissionsStore.new()

define_type Table do
  field :_id, type: 'integer', value: { id }
  field :user_ids, value: -> { @table_permissions_store.get_permission(self) }
end

define_type Column.includes(:table) do
  field :user_ids, value: -> { @table_permissions_store.get_permission(table) }
end

Is there any way to do something like this?

@pyromaniac
Copy link
Contributor

Yeah, sure, because value procs are called in the AR object context. Try value: -> (table) { @table_permissions_store.get_permission(table) } At this case the context of value proc should not be changed. But I'm still not sure you need this. It seems like TablePermissionsStore object will be initialized only once on the application start. If this is what you need - it is better to use constant, it will be available from every context. If this is not - then explain what are you trying to do. Maybe it is a new object for every stored record?

@jondavidford
Copy link
Contributor Author

Sorry, it is just a hash that stores permissions at index time. If the key for a specific table is already in the hash then it just does a lookup and the value at that key is the permission. If the key isn't there then it runs the expensive function to get the user permissions and stores it in the hash.

So anytime a new index action is occurring (specifically reset and import), the hash should be reset to be empty (since permissions can change). Maybe I can use the chewy atomic block?

@pyromaniac
Copy link
Contributor

I'm still thinking about how to implement such a case. Previous code will not work, I'm sure. But Don't even know. Maybe you have to redefine import method for index? To initialize TablePermissionsStore, but how to access it inside importer? Maybe use a kind of https://github.com/pyromaniac/contextuality to create variable inside the import context. Not sure.

@jondavidford
Copy link
Contributor Author

Okay, it seems like a work around will be pretty difficult so I'm just going to work on adding in parent/child mappings.

@baothi
Copy link

baothi commented Feb 5, 2015

https://gist.github.com/baothi/72c63fbb9b8b4675188e
please help me error

@pyromaniac
Copy link
Contributor

just place tcos_index.rb inside app/chewy dir and pay attention to it's name

@baothi
Copy link

baothi commented Feb 5, 2015

hi pyromaniac
yes I do same gem chewy but It until error

@pyromaniac
Copy link
Contributor

are you sure your file named tacos_index.rb?

@baothi
Copy link

baothi commented Feb 5, 2015

https://www.youtube.com/watch?v=v_MCcue2uEA
yes i see here

@baothi
Copy link

baothi commented Feb 5, 2015

hi pyromaniac
have you skype, you can add my skype help me
my skype: bao.thi2

@pyromaniac
Copy link
Contributor

just try to execute spring stop in bash and do rails c once again

@baothi
Copy link

baothi commented Feb 6, 2015

hi pyromaniac
yes i try ``spring stop` but it still error

@baothi
Copy link

baothi commented Feb 6, 2015

hi pyromaniac
when i do again bin/spring stop it run TacosIndex but when i run TacosIndex::Taco.import(Taco.all)
it error Faraday::ConnectionFailed: Connection refused - connect(2) for "localhost" port 9200

@toptal toptal locked and limited conversation to collaborators Feb 6, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants