Exists operation being ignored when searching

Hi everyone!

A new noob here with some doubts. I was testing a silly thing with the exists filter, but looks like is not working at all :confused: Look at the example below, when I run it, it completly ignores the exists filter and bring me the only result I have, only when I remove the { "query":{ "match_all":{} } } it works (giving me 0 results). How is that possible??

POST /test/tweet
{
"message": "some arrays in this tweet...",
"tags": [
"elasticsearch",
"wow"
],
"lists": [
{
"name": "prog_list",
"description": "programming list"
},
{
"name": "cool_list",
"description": "cool stuff list"
}
],
"numbers": [1,2,3,4]

}

GET /test/tweet/_search?explain #&format=yaml
{
_source:["lists.description","ttt"],
"query": {
"filtered": {
"filter": [
{
"exists": {
"field": "ttt"
}
},
{
"query": {
"match_all": {}
}
}
]
}
}
}

Thanks!
Mauricio Repetto

You need to wrap the filters in an and for them to be both applied, it's not an implicit or or and.
If you run;

POST test/tweet/_validate/query?explain
{
  "query": {
    "filtered": {
      "filter": [
        {
          "exists": {
            "field": "ttt"
          }
        },
        {
          "query": {
            "match_all": {}
          }
        }
      ]
    }
  }
}

You will get;

{
   "valid": true,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "explanations": [
      {
         "index": "test",
         "valid": true,
         "explanation": "filtered(ConstantScore(QueryWrapperFilter(ConstantScore(*:*))))->cache(_type:tweet)"
      }
   ]
}

And basically it's saying we ignore the exists and just default to the match_all.

This should really return an error as it's invalid for the filter query, we've refactoring things in current master which should help moving forward though.

1 Like

Thanks for your reply :slight_smile: I think I get it now Mark!