1
votes

I'm trying to use elasticsearch via tire gem for a multi-tenant app. The app has many accounts with each account having many users.

User Model:

  include Tire::Model::Search
  mapping do
    indexes :name, :boost => 10
    indexes :account_id
    indexes :company_name
  end

  def to_indexed_json
    to_json( :only => [:name, :account_id, :company_name], 
       )
  end

I would like to add the routing based on account_id. Please help how this can be achieved. I see that there are two ways to specify the routing.

  1. during Mapping using the routing field
  2. Using aliases

I'm interested in the first option. I see that _routing can be added to mapping section as a hash.

  mapping :_routing => { :required => true, :path => :account_id } do
    indexes :name, :boost => 10
    indexes :account_id
    indexes :company_name
  end

Search Query:

  User.tire.search do
    query do
      filtered do
          query { string('[email protected]') },
          filter :term, :account_id => 2
      end
    end
  end

Do we need to specify anything in the search query? The indexing is not happening when I specify the mapping as specified above (with routing). Without routing it works fine.

1

1 Answers

0
votes

Adding the account_id as path helps you to accumulate all the same account data on 1 shard.

It is recommended to use the routing in the search query also to avoid hiting the search query on every shard.

Use the Get Mapping API to verify the mapping is set properly or not.