# Query for the fields which is non empty

**URL:** <https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764>\
**Category:** Elasticsearch\
**Created:** [October 26, 2023, 1:27am UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764 "2023-10-26T01:27:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manasa4](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Manasa4](https://discuss.elastic.co/u/Manasa4)\
**Post date:** [October 26, 2023, 1:27am UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/1 "2023-10-26T01:27:31Z")

</div>

Hi Team,  
I'm reaching out query that I have, I want a query which returns the field with any random value inside it and filter out the empty records.

For eg: In my case, I have a FileContent.content field and it has values for that field and few doesn't have any value for it and it is empty. I figured out the query to filter out the empty values l, but I want a query which gives me the Filecontent.content field which has values in it. I've been trying alot on this. Can someone please help on this?

Here is the query I tried for filtering out the empty values :  
{  
"query": {  
"bool": {  
"should": [  
{  
"bool": {  
"must\_not": [  
{  
"exists": {  
"field": "FileContent.content"  
}  
}  
]  
}  
},  
{  
"term": {  
"FileContent.content": ""  
}  
}  
]  
}  
}  
}

The above query is working **perfectly fine for empty records** , but I need a **query which gives me the FileContent.content with a value inside it i.e; non empty**.

Can you please help me on this?  
I've been stuck since a long time.

Thanks in advance and appreciate you if you can really help me on this.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 26, 2023, 4:04am UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/2 "2023-10-26T04:04:49Z")

</div>

Hi @Manasa4

The query below works?

```auto
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "FileContent.content": ""
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Manasa4](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Manasa4](https://discuss.elastic.co/u/Manasa4)\
**Post date:** [October 26, 2023, 8:33am UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/3 "2023-10-26T08:33:01Z")

</div>

Yes! I did try this and this is giving me the whole index count. It would be really be helpful if you can give me the query for the non empty fields.

Thanks,  
Manasa

---

<div class="post-metadata">

**Author:** ![sholzhauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sholzhauer/32/110282_2.png) [@sholzhauer](https://discuss.elastic.co/u/sholzhauer)\
**Post date:** [October 26, 2023, 7:13pm UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/4 "2023-10-26T19:13:34Z")

</div>

If I follow you need somthing like this:

```json
{
    "query": {
        "bool": {
            "must": [
                {
                    "exists": {
                        "field": "FileContent.content"
                    }
                }
            ],
            "must_not": [
                {
                  "term": {
                    "FileContent.content": ""
                  }
                }
            ]
        }
    }
}

```

This query makes sure the field exists and that the field is not 'empty'.

---

<div class="post-metadata">

**Author:** ![Manasa4](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Manasa4](https://discuss.elastic.co/u/Manasa4)\
**Post date:** [November 3, 2023, 7:51pm UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/5 "2023-11-03T19:51:30Z")

</div>

Hi sholzhauer,

Thanks for the input. But the above query is giving the empty records. This query is working perfectly for non-empty records :  
{"query": {  
"bool": {  
"must": {  
"exists": {  
"field": "Description"  
}  
},  
"must\_not": {  
"term": {  
"Description.keyword": ""  
}  
}  
}  
}  
}

The above query eliminates all the empty records and process only non-empty records

---

<div class="post-metadata">

**Author:** ![sholzhauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sholzhauer/32/110282_2.png) [@sholzhauer](https://discuss.elastic.co/u/sholzhauer)\
**Post date:** [November 4, 2023, 6:27pm UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/6 "2023-11-04T18:27:32Z")

</div>

I am afraid I don't understand what you need then?

---

<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:** [December 2, 2023, 6:28pm UTC](https://discuss.elastic.co/t/query-for-the-fields-which-is-non-empty/345764/7 "2023-12-02T18:28:30Z")

</div>

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