rap-2-h
rap-2-h

Reputation: 31948

Elasticsearch get documents where a property is not empty

If I want to return all the documents which have an empty property (IMG) I can do something like that:

GET something/_search/?
{
  "query": {
    "term": {"IMG": ""}
  }
}

It works because IMG is a keyword. If I want the exact inverse, which means get all the documents where IMG is not null, what should I type? Is there an "inverse" of term query?

In other words, is there a way with Elasticsearch to get documents where a property is not empty?

Upvotes: 4

Views: 13504

Answers (2)

Val
Val

Reputation: 217274

Your solution above would also return documents where the field is null, which you don't want I guess. So the correct solution would be this one:

GET memoire/_search/?
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "test"
        }
      },
      "must_not": {
        "term": {
          "test.keyword": ""
        }
      }
    }
  }
}

Upvotes: 8

rap-2-h
rap-2-h

Reputation: 31948

Here is a solution. Use must_not with term query. This should work:

GET memoire/_search/?
{
  "query": {
    "bool": {
      "must_not": {
        "term": {"IMG.keyword": ""}
      }
    }
  }
}

Upvotes: 2

Related Questions