# Filter documents that contain array with empty string

**URL:** https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562
**Category:** Elasticsearch
**Created:** [February 8, 2022, 8:44am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562 "2022-02-08T08:44:05Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Muhammad\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_ahmad/32/101520_2.png) [@Muhammad\_Ahmad](https://discuss.elastic.co/u/Muhammad_Ahmad)
#### Post date: [February 8, 2022, 8:44am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/1 "2022-02-08T08:44:05Z")

</div>

I've documents in Elasticsearch and I want to filter out the documents that contain an array of only empty strings or have nothing / empty array.

```auto
#doc 1
{
  "_index": "my-index-000001",
  "_type": "_doc",
  "_id": "0",
  "_source": {
    "doc":{
    	"field": ["",""]
    }
  }
}

#doc 2
{
  "_index": "my-index-000001",
  "_type": "_doc",
  "_id": "0",
  "_source": {
    "doc":{
    	"field": []
    }
  }
}

#doc 3
{
  "_index": "my-index-000001",
  "_type": "_doc",
  "_id": "0",
  "_source": {
    "doc":{
    	"field": ["hello",""]
    }
  }
}

```

From the above documents is it possible to filter out only doc 1 and doc 2 as for these, the "field" either contains nothing in the array or only empty string(s).

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [February 8, 2022, 9:07am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/2 "2022-02-08T09:07:54Z")

</div>

Welcome to our community! 😃

What about something like;

```auto
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "your_field"
              }
            }
          ]
        }
      }
    }
  },
  "from": 0,
  "size": 500
}

```

---

<div class="post-metadata">

### Author: ![Muhammad\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_ahmad/32/101520_2.png) [@Muhammad\_Ahmad](https://discuss.elastic.co/u/Muhammad_Ahmad)
#### Post date: [February 8, 2022, 9:14am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/3 "2022-02-08T09:14:40Z")

</div>

Thanks for the reply... Happy to be on board!  
I tried this but it throws the exception "no [query] registered for [filtered]"

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [February 8, 2022, 9:17am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/4 "2022-02-08T09:17:53Z")

</div>

Yeah I think there's an extra `filtered` in there that shouldn't be.

What about;

```auto
GET /_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "your_field"
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![Muhammad\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_ahmad/32/101520_2.png) [@Muhammad\_Ahmad](https://discuss.elastic.co/u/Muhammad_Ahmad)
#### Post date: [February 8, 2022, 9:42am UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/5 "2022-02-08T09:42:15Z")

</div>

Really appreciate your help but this query is returning all the documents, that contain array with empty strings or nonempty strings, can you please have a look at the sample docs in the questions? if there is anything I can add up there for reference?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [February 8, 2022, 3:54pm UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/6 "2022-02-08T15:54:14Z")

</div>

If you are using keyword family field type, how about this?

```auto
GET /test_filter/_search
{
  "query":{
    "regexp": {
      "myField": ".+"
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 8, 2022, 3:54pm UTC](https://discuss.elastic.co/t/filter-documents-that-contain-array-with-empty-string/296562/7 "2022-03-08T15:54:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
