Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Multiple model single index approach #514

Closed
vamsi-krishna-E0029 opened this issue Nov 13, 2012 · 8 comments
Closed

Multiple model single index approach #514

vamsi-krishna-E0029 opened this issue Nov 13, 2012 · 8 comments

Comments

@vamsi-krishna-E0029
Copy link

In my multi-tenant app (account based with number of users per account), how would I update index for a particular account when a user document is changed.

Rails 2.3 app applied changes as per loe@53d8510

Account Model

      include Tire::Model::Search

      Tire.index('account_1') do
        create(
          :mappings => {
            :user => {
              :properties => {
                :name => { :type => :string, :boost => 10 },
                :company_name => { :type => :string, :boost => 5 }
              }
            },
            :comments => {
              :properties => {
                :description => { :type => :string, :boost => 5 }
              }
            }
          }
        )
      end

As you can see above, there are two models here user and comments. Is it the correct way to address single index with multiple models.

In that case how do I update index when a user document or comment document alone is changed.

@karmi
Copy link
Owner

karmi commented Nov 19, 2012

In my multi-tenant app (account based with number of users per account), how would I update index for a particular account when a user document is changed.

I can't say I understand 100% what are you trying to do, but basically, for models, just call update_index, it does honor the index/type structure, or just use Tire.store with a proper index name and document type.

@vamsi-krishna-E0029
Copy link
Author

How do I update an index based on it's name? I mean if I want to update an index like users, how can I do something like tire.update_index 'users'. The index name may vary in my case. Is that possible? The index name isn't dynamically generated one in my case.

@karmi
Copy link
Owner

karmi commented Dec 4, 2012

Since you have a separate index for each model, just call @record.tire.update_index. It will pick up whatever index name you've set in the class.

Otherwise, use Index#store to roll your own indexing strategy. If this is still about the "multi-tenancy" aspect, I really don't understand what you're doing, if you have an index for each account, if you use aliases, etc.

The index name may vary in my case. (...) The index name isn't dynamically generated one in my case.

I don't know what you mean. You're refering to dynamic index names by passing a block to index_name, eg. ActiveRecordClassWithDynamicIndexName in test/models?

@vamsi-krishna-E0029
Copy link
Author

Thanks for the reply.

I don't have a separate index for each model. I have a separate index for each account, in which the mappings for each model (user and comments) are specified. In this case if any change has been done for user model or comment model, the index that has been created for the related account has to be updated. Is this possible? Please let me know if yes.

I guess this is the way I specify the mappings in my case. Correct me if I'm wrong.

Account Model:

  include Tire::Model::Search

  Tire.index('account_1') do
    create(
      :mappings => {
        :user => {
          :properties => {
            :name => { :type => :string, :boost => 10 },
            :company_name => { :type => :string, :boost => 5 }
          }
        },
        :comments => {
          :properties => {
            :description => { :type => :string, :boost => 5 }
          }
        }
      }
    )
  end

The index is getting created correctly with both the mappings for account index. But, I don't see a way where I can update the index when any model specified in the mappings are changed.

Whenever a new user is added or if an user is updated the index created for the corresponding account has to be updated.

@karmi
Copy link
Owner

karmi commented Dec 11, 2012

Hello, sorry for the delay.

I have a separate index for each account, in which the mappings for each model (user and comments) are specified. In this case if any change has been done for user model or comment model, the index that has been created for the related account has to be updated. Is this possible? Please let me know if yes.

This is a tricky thing to do, the only real solution I have found in several projects is to just "switch" the index/database name based on some criteria in your application (logged-in user, etc).

Please see the #92 issue for a lot of Tire historical trivia, there is a solution outline at the end as well.

Whenever a new user is added or if an user is updated the index created for the corresponding account has to be updated.

Yes, this is something you need to handle in your application.

@karmi
Copy link
Owner

karmi commented Dec 12, 2012

Hello, had a look at the solution?

@vamsi-krishna-E0029
Copy link
Author

Oops sorry...Will look into it. Thanks for the reply.

@karmi
Copy link
Owner

karmi commented Dec 17, 2012

@ovamsikrishna Hello, any news? Closing?

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

2 participants