# How to retrieve all the documents in which specified field has value?

**URL:** https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254
**Category:** Elasticsearch
**Created:** [May 6, 2015, 5:24am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254 "2015-05-06T05:24:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ananth](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@ananth](https://discuss.elastic.co/u/ananth)
#### Post date: [May 6, 2015, 5:24am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/1 "2015-05-06T05:24:49Z")

</div>

Solr provides this feature .

field : [\* TO \*] matches all documents with the field

(from solr wiki [http://wiki.apache.org/solr/SolrQuerySyntax](http://wiki.apache.org/solr/SolrQuerySyntax))

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [May 6, 2015, 5:36am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/2 "2015-05-06T05:36:36Z")

</div>

Sorry, what's the question? If Elasticsearch supports the [x TO y] range query operation? If yes, see [http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html#\_ranges\_2](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html#_ranges_2). If no, please clarify your question.

---

<div class="post-metadata">

### Author: ![ananth](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@ananth](https://discuss.elastic.co/u/ananth)
#### Post date: [May 6, 2015, 6:56am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/3 "2015-05-06T06:56:28Z")

</div>

Lets say my documents have the following fields 1.id 2.name 3.country

Example documents  
doc1 : {"id" : 1 , "name": "name1" , "country": "india"}  
doc2 : {"id" : 2 , "name": "name2" }  
doc3 : {"id" : 3 , "name": "name3" , "country": "UK"}  
doc4 : {"id" : 4 , "name": "name4" }

In solr , if i give country : [\* TO \*] , i will get document doc1 & doc3 .

I mean , field : [\* TO \*] will give documents in which it has values for the requested field.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 6, 2015, 7:40am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/4 "2015-05-06T07:40:04Z")

</div>

You could use exists filter?

[http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html](http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html)

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [May 6, 2015, 11:09pm UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/5 "2015-05-06T23:09:33Z")

</div>

+1 to David's suggestion. Note that using `exists` would also be potentially MUCH faster than a range query by using the special `_field_names` field: [http://www.elastic.co/guide/en/elasticsearch/reference/1.x/mapping-field-names-field.html](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/mapping-field-names-field.html)

---

<div class="post-metadata">

### Author: ![ananth](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@ananth](https://discuss.elastic.co/u/ananth)
#### Post date: [May 7, 2015, 6:16am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/6 "2015-05-07T06:16:15Z")

</div>

Thanks for the suggestion David . 'exists' filter worked nicely .  
Btw, field : [\* To \*] also worked . Sorry for the wrong reporting .  
In our indexing code protobuffer to json conversion part has been modified , where we are started adding empty string for fields which doesn't have value.

@jpountz We will use exists filter , thanks for the suggestion !

---

<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: [July 6, 2017, 12:15am UTC](https://discuss.elastic.co/t/how-to-retrieve-all-the-documents-in-which-specified-field-has-value/254/7 "2017-07-06T00:15:13Z")

</div>


