# How to filter the following query?

**URL:** https://discuss.elastic.co/t/how-to-filter-the-following-query/218430
**Category:** Elasticsearch
**Created:** [February 8, 2020, 9:51am UTC](https://discuss.elastic.co/t/how-to-filter-the-following-query/218430 "2020-02-08T09:51:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![abhijith\_chandran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhijith_chandran/32/60470_2.png) [@abhijith\_chandran](https://discuss.elastic.co/u/abhijith_chandran)
#### Post date: [February 8, 2020, 9:51am UTC](https://discuss.elastic.co/t/how-to-filter-the-following-query/218430/1 "2020-02-08T09:51:10Z")

</div>

Hi,  
I am using the following search:

```auto
{
    "_source": [
        "title",
        "bench",
        "court",
        "id_"
    ],
    "size": 10,
    "query": {
    	
        "bool": {
            "must": {
                
                    "multi_match": {
                    	"query": "murder" 
                    ,
                    "fields": [
                        "title",
                        "content"
                    ]
                }
            },
            
            "should": {
                "multi_match": {
                    "query": "murder",
                    "fields": [
                        "title.standard",
                        "content.standard"
                    ]
                }
            }
            
        }
          
    
    },
    "highlight": {
        "fields": {
            "title": {},
            "content": {}
        }
    }
}

```

I now want to filter the results using the id (`_id`) elastic search gave it during indexing. For example, `{"_id" : 5903}`. I guess you have to use the `term` query. The results should be such that only if the `_id` is matched, the document returns. How can I do that?

---

<div class="post-metadata">

### Author: ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)
#### Post date: [February 8, 2020, 10:57am UTC](https://discuss.elastic.co/t/how-to-filter-the-following-query/218430/2 "2020-02-08T10:57:42Z")

</div>

Term query will work just same as it works for other fields. You can even use [ids query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html). Just add to the must clause as below:

```
{
  "_source": [
    "title",
    "bench",
    "court",
    "id_"
  ],
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "murder",
            "fields": [
              "title",
              "content"
            ]
          }
        },
        {
          "ids":{
            "values": [5903]
          }
        }
      ],
      "should": {
        "multi_match": {
          "query": "murder",
          "fields": [
            "title.standard",
            "content.standard"
          ]
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {}
    }
  }
}
```

---

<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: [March 7, 2020, 10:57am UTC](https://discuss.elastic.co/t/how-to-filter-the-following-query/218430/3 "2020-03-07T10:57:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
