Must not query not working

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

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.

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.

Could you provide a full recreation script as described in

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

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("Addingcountry also in search::"+Country);
filter.mustNot(FilterBuilders.termFilter("restrictedCountry",restrictedCountry));

	}

I am filtering content in this way

Please format your code using </> icon as explained in this guide. 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.

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

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

Whatever. Here is a working example in 6.0:

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:

# 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:

POST _analyze
{
  "analyzer": "standard", 
  "text": [ "IN" ]
}
# POST _analyze
{
  "tokens": [
    {
      "token": "in",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

Thanks dadoonet.
Now it's working fine.

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