简体   繁体   中英

Routing field in elasticsearch via tire

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('ovamsikrishna@gmail.com') },
          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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM