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.
- during Mapping using the routing field
- 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.