简体   繁体   中英

Exclude indexed data in search results - elasticsearch (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.

Now I would like to index the User based on account id.

User Model:

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

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

Search Query:

  User.tire.search do
    query do
      filtered do
        query { string 'vamsikrishna' }
        filter :term, :account_id => 1
      end
    end
  end

The filter works fine and the results are displayed only for the filtered account id (1). But, when I search for a query string 1:

  User.tire.search do
    query do
      filtered do
        query { string '1' }
        filter :term, :account_id => 1
      end
    end
  end

Lets say there is an user named 1. In that case, the results are getting displayed for all the users with account id 1 and the user too. This is because I added account_id in the to_indexed_json. Now, how can I display only the user with name 1? (All users should not be available in the hits. Only the user with name 1 should be displayed)

When there are no users with 1 as name or company name or description, I just don't want any results to be displayed. But, in my case as I explained I would get all the users in the account id 1.

You are searching on the _all special field, which contains a copy of the content of all the fields that you indexed. You should search on a specific field to have the right results like this: field_name:1 .

If you want you can search on multiple fields using the query string :

{
    "query_string" : {
        "fields" : ["field1", "field2", "field3"],
        "query" : "1"
    }
}

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