Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 888
rap-2-h Avatar asked Oct 24 '25 16:10

rap-2-h


1 Answers

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": ""
        }
      }
    }
  }
}
like image 156
Val Avatar answered Oct 26 '25 19:10

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!