# Excluding multiple documents from query

**URL:** https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556
**Category:** Elasticsearch
**Created:** [February 3, 2014, 1:38pm UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556 "2014-02-03T13:38:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bruskie24](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bruskie24](https://discuss.elastic.co/u/bruskie24)
#### Post date: [February 3, 2014, 1:38pm UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556/1 "2014-02-03T13:38:45Z")

</div>

I'm trying to use a Boolean query to exclude multiple documents from my  
result set using the must\_not operator, when I specify a single \_id using  
the term facet, it works just fine, but when I try more than one \_id with  
either the term or terms facet, it doesn't display back correctly. Is there  
another way of doing this?

My original (working) query is in the form of:  
{  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"content" : "data mining"  
}  
}  
],  
"must\_not": [  
{  
"term": {  
"\_id": "1234"  
}  
}  
]  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9e983990-3c5d-426b-b23d-7459837dfc12%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9e983990-3c5d-426b-b23d-7459837dfc12%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [February 3, 2014, 3:25pm UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556/2 "2014-02-03T15:25:12Z")

</div>

Try using a ids filter combined with a not filter instead;

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Filters are faster, cacheable (if desired) and will not influence scoring.  
If you do not want the excluded ids to be added to the facet counts, use  
the filter as part of a filtered query.

--  
Ivan

On Mon, Feb 3, 2014 at 5:38 AM, James Massey [jmassey@clearedgeit.com](mailto:jmassey@clearedgeit.com)wrote:

> I'm trying to use a Boolean query to exclude multiple documents from my  
> result set using the must\_not operator, when I specify a single \_id using  
> the term facet, it works just fine, but when I try more than one \_id with  
> either the term or terms facet, it doesn't display back correctly. Is there  
> another way of doing this?
> 
> My original (working) query is in the form of:  
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "content" : "data mining"  
> }  
> }  
> ],  
> "must\_not": [  
> {  
> "term": {  
> "\_id": "1234"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/9e983990-3c5d-426b-b23d-7459837dfc12%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9e983990-3c5d-426b-b23d-7459837dfc12%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD%2BMSqGOSDzSBVjenmgHpdqMiPkj6dKnK9vLoXkHBM9%2Bg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQD%2BMSqGOSDzSBVjenmgHpdqMiPkj6dKnK9vLoXkHBM9%2Bg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Sloan\_Ahrens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sloan_ahrens/32/1828_2.png) [@Sloan\_Ahrens](https://discuss.elastic.co/u/Sloan_Ahrens)
#### Post date: [February 3, 2014, 3:31pm UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556/3 "2014-02-03T15:31:03Z")

</div>

Have you tried the [ids filter](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-ids-filter.html)?

I created a simple index with some data as follows:

```
curl -XPUT "http://localhost:9200/test_index/"

curl -XPUT "http://localhost:9200/test_index/_bulk" -d'
{"index":{"_index":"test_index","_type":"docs","_id":1}}
{"content": "abc"}
{"index":{"_index":"test_index","_type":"docs","_id":2}}
{"content": "abc"}
{"index":{"_index":"test_index","_type":"docs","_id":3}}
{"content": "abc"}
{"index":{"_index":"test_index","_type":"docs","_id":4}}
{"content": "xyz"}
'
```

and then queried like this:

```
curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "content": "abc"
               }
            }
         ],
         "must_not": [
            {
                "ids": {
                    "values": ["1", "2"]
                }
            }
         ]
      }
   }
}'
```

and got back the result I expected:

```
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.5945348,
      "hits": [
         {
            "_index": "test_index",
            "_type": "docs",
            "_id": "3",
            "_score": 0.5945348,
            "_source": {
               "content": "abc"
            }
         }
      ]
   }
}
```

Here is a runnable example you can play with if you want:

[http://sense.qbox.io/gist/d0ef08e3c5b94706f85ede9ea95229e315dbfa6b](http://sense.qbox.io/gist/d0ef08e3c5b94706f85ede9ea95229e315dbfa6b)

---

<div class="post-metadata">

### Author: ![bruskie24](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bruskie24](https://discuss.elastic.co/u/bruskie24)
#### Post date: [February 3, 2014, 3:57pm UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556/4 "2014-02-03T15:57:14Z")

</div>

That worked, thanks everyone!

---

<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 6, 2017, 1:53am UTC](https://discuss.elastic.co/t/excluding-multiple-documents-from-query/15556/5 "2017-07-06T01:53:08Z")

</div>


