4

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?

5
  • this answer should help: stackoverflow.com/questions/32949321/… Commented Sep 14, 2018 at 14:06
  • Thanks you for your comment. It does not really help, because these answers check for existence, not emptiness. Commented Sep 14, 2018 at 14:09
  • Sorry, so you mean "the property is not the empty string"? Commented Sep 14, 2018 at 14:11
  • @Val Yes. BTW I just found a solution and posted an answer right below. Feel free to comment, edit, or post your own answer! Commented Sep 14, 2018 at 14:13
  • 1
    I've posted one, too ;-) Commented Sep 14, 2018 at 14:15

2 Answers 2

8

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": ""
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

3 Comments

you have must instead of must_not
@Val Woops! +1 thank you! (I was just checking with kibana the must_not then the must to be sure, and I posted the wrong one!). Edited, fixed!
See my solution in order to make sure that you don't return docs with IMG: null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.