Search Query

Hello,

We're developing an instant messaging app and using elasticsearch with rails.

We have chat rooms, each room has name and users that are members in it.
We've implemented search on these chat rooms.
When a user perform search - the results should include all chat rooms that the user is member of and the given query appears in the chat room name or the query appears at least in one of the users names.

We’ve noticed that if a user search his name, the results will include all the chat rooms that he’s member of (that’s because his name appears in the chat rooms' users names).

We would like the search results to include only chat rooms that meet the our requirements as mentioned before but without the chat rooms that were found only because the search query includes the searching user name (or substring of it).

For example, John is member in these two chat rooms:
Chat room 1: name: "Dev Team", users names: "John", "Paul"
Chat room 2: name: "Design Team", users names: "John", "Jessica"

When John perform search with the query: "Paul", search results include chat room 1.
When John perform search with the query "John", search results include both chat rooms (while we want the search results to be none).

This is the index method of the chat rooms:

def as_indexed_json(options={})
  { :name => self.name, 
    :users_ids => self.users.ids,             #array of users ids
    :users_names => self.users.pluck(:name) } #array of users names
end

And this is our search method:

def self.search(query, user_id)
  __elasticsearch__.search(
      query: {
          bool:
              { must: [
                  { query_string: {
                          query: query,
                          fields: [:name, :users_names],
                          analyzer: 'standard'
                      }
                  },
                  { match: { users_ids: user_id }}
              ] }
      },
      size: 10000
  )
end

I would like to know what modifications should be made to the search method.

I’ll appreciate any help,
Lee