# Must not query not working

**URL:** <https://discuss.elastic.co/t/must-not-query-not-working/107759>\
**Category:** Elasticsearch\
**Created:** [November 15, 2017, 1:50pm UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759 "2017-11-15T13:50:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravind\_apalya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aravind\_apalya](https://discuss.elastic.co/u/aravind_apalya)\
**Post date:** [November 15, 2017, 1:50pm UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/1 "2017-11-15T13:50:52Z")

</div>

Hi,

I have a bunch of documents in my database.  
I need to filter the contents which are not matching with my condition.  
For this, I have used filter **must\_not** , but not working.

Below is the code snippet.

filter.mustNot(FilterBuilders.termFilter("Country",Country));

My target is to filter document by this param.  
So if content has country **IN** above code shouldn't fetch it.  
But with the above query I'm still seeing the results with **IN** country.

Can someone help me out!! This is bugging me since a few days!!!

- I'm using elastic search version 0.9

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 15, 2017, 3:23pm UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/2 "2017-11-15T15:23:06Z")

</div>

First: I think you mean 0.90 as the version?  
IT S A WAY TOO OLD!!!

Second: I believe you are using the default analyzer and that `in` is actually a stop word so it's not indexed.

---

<div class="post-metadata">

**Author:** ![aravind\_apalya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aravind\_apalya](https://discuss.elastic.co/u/aravind_apalya)\
**Post date:** [November 16, 2017, 6:07am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/3 "2017-11-16T06:07:49Z")

</div>

Thanks for the response David Pilato.  
Yes I am using 0.90 v  
So in stop words, I have term **in** , so I tried with **SL** , Still, it is giving the same result.

Country: **SL**  
It is still giving record which has country SL.  
Please help me out where I did wrong.

Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 16, 2017, 7:44am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/4 "2017-11-16T07:44:38Z")

</div>

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![aravind\_apalya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aravind\_apalya](https://discuss.elastic.co/u/aravind_apalya)\
**Post date:** [November 23, 2017, 11:55am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/5 "2017-11-23T11:55:11Z")

</div>

Hi dadoonet,

Basically, my method is GET,  
I am receiving parameter through **@QueryParam("Country") String Country**  
so that in my script I am filtering content as:

BoolFilterBuilder filter = FilterBuilders.boolFilter().filterName("compositeFilter");  
if (Country != null && !Country.isEmpty()) {  
[log.info](http://log.info)("Addingcountry also in search::"+Country);  
filter.mustNot(FilterBuilders.termFilter("restrictedCountry",restrictedCountry));

```
	}

```

I am filtering content in this way

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 23, 2017, 2:11pm UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/6 "2017-11-23T14:11:17Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

Please share a pure elasticsearch CURL example as described in the link I posted previously.

---

<div class="post-metadata">

**Author:** ![aravind\_apalya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aravind\_apalya](https://discuss.elastic.co/u/aravind_apalya)\
**Post date:** [November 24, 2017, 4:51am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/7 "2017-11-24T04:51:48Z")

</div>

Sorry, David now I understood how to post a script.

My GET request is:

```
**POST: http://IP:9200/content/_search** 
  "must_not" : {
                "term" : {
                  "restrictedCountry" : "IN"
                }
              },
  "match" : {
                  "title" : {
                    "query" : "ish",
                    "type" : "phrase_prefix",
                    "analyzer" : "simple"
                  }
                }

```

Do you need any more info on this.

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 24, 2017, 7:33am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/8 "2017-11-24T07:33:07Z")

</div>

Your example is wrong and can not be used as is.

Whatever. Here is a working example in 6.0:

```auto
DELETE test
PUT test/doc/1
{
  "restrictedCountry": "IN",
  "title": "ishfoo"
}
PUT test/doc/2
{
  "restrictedCountry": "FR",
  "title": "ishfoo"
}
GET test/_search
{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "restrictedCountry": "in"
        }
      },
      "must": {
        "match_phrase_prefix": {
          "title": {
            "query": "ish",
            "analyzer": "simple"
          }
        }
      }
    }
  }
}

```

It gives:

```auto
# DELETE test
{
  "acknowledged": true
}

# PUT test/doc/1
{
  "_index": "test",
  "_type": "doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

# PUT test/doc/2
{
  "_index": "test",
  "_type": "doc",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

# GET test/_search
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "restrictedCountry": "FR",
          "title": "ishfoo"
        }
      }
    ]
  }
}

```

The thing in you last example is that you are using a `term` query with `IN` but as you can see `IN` has been indexed as `in`:

```auto
POST _analyze
{
  "analyzer": "standard", 
  "text": ["IN"]
}

```

```auto
# POST _analyze
{
  "tokens": [
    {
      "token": "in",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![aravind\_apalya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aravind\_apalya](https://discuss.elastic.co/u/aravind_apalya)\
**Post date:** [December 1, 2017, 11:46am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/9 "2017-12-01T11:46:37Z")

</div>

Thanks dadoonet.  
Now it's working fine.

---

<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:** [December 29, 2017, 11:46am UTC](https://discuss.elastic.co/t/must-not-query-not-working/107759/10 "2017-12-29T11:46:53Z")

</div>

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