# Search Query

**URL:** <https://discuss.elastic.co/t/search-query/59156>\
**Category:** Elasticsearch\
**Created:** [August 29, 2016, 7:38am UTC](https://discuss.elastic.co/t/search-query/59156 "2016-08-29T07:38:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://discuss.elastic.co/u/Lee)\
**Post date:** [August 29, 2016, 7:38am UTC](https://discuss.elastic.co/t/search-query/59156/1 "2016-08-29T07:38:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:24pm UTC](https://discuss.elastic.co/t/search-query/59156/2 "2017-07-05T22:24:30Z")

</div>

Closed on July 5, 2017, 10:24pm UTC


