0

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.

Vamsi Krishna
  • 3,742
  • 4
  • 20
  • 45

1 Answers1

0

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"
    }
}
javanna
  • 59,145
  • 14
  • 144
  • 125
  • Thanks for the reply @javanna... Its not just the name field alone I have here. I do have other fields like user description, company name which are indexed as well. In that case how am I supposed to run search on the User model? Please help. – Vamsi Krishna Oct 30 '12 at 12:49
  • I don't get it: where do you want to search for the `1`? I thought only on the name field. If you want to search on multiple fields you can with the query string specifying the `fields` you want to search on. – javanna Oct 30 '12 at 12:55
  • I'm sorry for posting the question like that. The string can be in multiple fields. 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. I will edit the question now. – Vamsi Krishna Oct 30 '12 at 13:02
  • The above code worked fine. Small clarification please...If I add the include_in_all as false for the account_id, why don't I get the expected result. My understanding is that instead of specifying the fields where to search, I thought of excluding the fields from _all field. So, I tried adding the :include_in_all => false for the account_id. But, that doesn't seem to work. I'm getting all the users list when I search for 1. – Vamsi Krishna Oct 31 '12 at 07:02
  • Did you reindex after applying the settings change? – javanna Oct 31 '12 at 09:52
  • I feel so dumb. That did the trick. Thank you very much for the help. – Vamsi Krishna Oct 31 '12 at 12:10