ysimonson
ysimonson

Reputation: 1102

Exclude empty array fields - but include documents missing the field - in elasticsearch

I'm trying to run a query against elasticsearch that will find documents where one of the following conditions applies:

The problem is that my current query will return documents that have a tags field where the value is an empty array. Presumably this is because elasticsearch is treating an empty array as the same thing as not having the field at all. Here's the full query I'm running that's returning the bad results:

{
    "from": 0, 
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "exists": {
                                "field": "_rankings.public"
                            }
                        }, 
                        {
                            "or": [
                                {
                                    "missing": {
                                        "existence": true, 
                                        "field": "tags", 
                                        "null_value": false
                                    }
                                }, 
                                {
                                    "terms": {
                                        "execution": "or", 
                                        "tags": [
                                            "foo"
                                        ]
                                    }
                                }
                            ]
                        }
                    ]
                }
            }, 
            "query": {
                "match_all": {}
            }
        }
    }, 
    "size": 10000, 
    "sort": [
        {
            "_rankings.public": {
                "ignore_unmapped": true, 
                "order": "asc"
            }
        }
    ]
}

Upvotes: 1

Views: 2031

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

I don't think you can achieve this so easily "out-of-the-box" for the reason you already mentioned: there's no difference between an empty array and a field (corresponding to that array) with no values in it.

Your only option might be to use a "null_value" for that "tags" field and, if you have any control over the data that goes into your documents, to treat a "[]" array as a '["_your_null_value_of_choice_"]'. And in your query to change "null_value": false to true.

Upvotes: 1

Related Questions