# Using Java, How to retrieve one field from all documents inside an Index

**URL:** https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564
**Category:** Elasticsearch
**Created:** [November 4, 2014, 5:57pm UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564 "2014-11-04T17:57:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ap1](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@Ap1](https://discuss.elastic.co/u/Ap1)
#### Post date: [November 4, 2014, 5:57pm UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/1 "2014-11-04T17:57:44Z")

</div>

Eg. Index has 100 documents with field A, B, C ....Z

I want to retrieve a list of all A's from the 100 documents in the Index.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [November 4, 2014, 6:01pm UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/2 "2014-11-04T18:01:32Z")

</div>

If it’s « only » 100 docs, you can use .setSize(100) to your search query and .addFields("a"):

client.prepareSearch().setSize(100).addFields("a").get();

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr) | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)

> Le 4 nov. 2014 à 18:57, Ap [usc90909@gmail.com](mailto:usc90909@gmail.com) a écrit :
> 
> Eg. Index has 100 documents with field A, B, C ....Z
> 
> I want to retrieve a list of all A's from the 100 documents in the Index.
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com) [mailto:elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com) [https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b36f895c-8497-4167-be7b-3227763e6fea%40googlegroups.com?utm_medium=email&utm_source=footer).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout) [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/66A869B8-E43D-49E8-B890-38BC2ED66FBF%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/66A869B8-E43D-49E8-B890-38BC2ED66FBF%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ted\_Smith](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Ted\_Smith](https://discuss.elastic.co/u/Ted_Smith)
#### Post date: [November 5, 2014, 1:48pm UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/3 "2014-11-05T13:48:31Z")

</div>

Hello, David:

I have issue with trying to retrieve all document Ids (or a single field  
value of all documents) in an index.  
I have about several million documents, but all I need is a list of  
document id (sorted if possible), nothing else.  
It is taking 5 minutes now for me to get the results. All I need is the  
array/list of the Ids.  
What would be the fastest and most efficient (no extra data returned) ways  
to do this in Java.

Thanks very much

On Tuesday, November 4, 2014 12:57:44 PM UTC-5, Ap wrote:

> Eg. Index has 100 documents with field A, B, C ....Z
> 
> I want to retrieve a list of all A's from the 100 documents in the Index.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [November 5, 2014, 2:10pm UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/4 "2014-11-05T14:10:14Z")

</div>

So that's not the same story.

You want to do scan and scroll.

See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 5 nov. 2014 à 13:48, Ted Smith [tedsmithgroup@gmail.com](mailto:tedsmithgroup@gmail.com) a écrit :
> 
> Hello, David:
> 
> I have issue with trying to retrieve all document Ids (or a single field value of all documents) in an index.  
> I have about several million documents, but all I need is a list of document id (sorted if possible), nothing else.  
> It is taking 5 minutes now for me to get the results. All I need is the array/list of the Ids.  
> What would be the fastest and most efficient (no extra data returned) ways to do this in Java.
> 
> Thanks very much
> 
> > On Tuesday, November 4, 2014 12:57:44 PM UTC-5, Ap wrote:
> > 
> > Eg. Index has 100 documents with field A, B, C ....Z
> > 
> > I want to retrieve a list of all A's from the 100 documents in the Index.
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2A15ADB3-340F-4F3A-959B-F72049EA22C9%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/2A15ADB3-340F-4F3A-959B-F72049EA22C9%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ted\_Smith](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Ted\_Smith](https://discuss.elastic.co/u/Ted_Smith)
#### Post date: [November 6, 2014, 5:23am UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/5 "2014-11-06T05:23:04Z")

</div>

Thanks. Wish there is simple Java API method to that gives a handle to get  
the list in a single call.  
Would it be possible to add this feature as it is often needed ( and  
supposed to be a simple process)

On the same topic, even with scan and scroll, how can I limit the result  
returned only include the id itself,  
without all those meta data?

On Wednesday, November 5, 2014 9:10:35 AM UTC-5, David Pilato wrote:

> So that's not the same story.
> 
> You want to do scan and scroll.
> 
> See  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 5 nov. 2014 à 13:48, Ted Smith \<[tedsmi...@gmail.com](mailto:tedsmi...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Hello, David:
> 
> I have issue with trying to retrieve all document Ids (or a single field  
> value of all documents) in an index.  
> I have about several million documents, but all I need is a list of  
> document id (sorted if possible), nothing else.  
> It is taking 5 minutes now for me to get the results. All I need is the  
> array/list of the Ids.  
> What would be the fastest and most efficient (no extra data returned) ways  
> to do this in Java.
> 
> Thanks very much
> 
> On Tuesday, November 4, 2014 12:57:44 PM UTC-5, Ap wrote:
> 
> > Eg. Index has 100 documents with field A, B, C ....Z
> > 
> > I want to retrieve a list of all A's from the 100 documents in the Index.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d9d9c5af-11cf-435f-9496-9c783b3f67f9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d9d9c5af-11cf-435f-9496-9c783b3f67f9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [November 6, 2014, 7:17am UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/6 "2014-11-06T07:17:15Z")

</div>

addFields("\_id") should work I think though all metadata will be sent but \_source.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 6 nov. 2014 à 05:23, Ted Smith [tedsmithgroup@gmail.com](mailto:tedsmithgroup@gmail.com) a écrit :
> 
> Thanks. Wish there is simple Java API method to that gives a handle to get the list in a single call.  
> Would it be possible to add this feature as it is often needed ( and supposed to be a simple process)
> 
> On the same topic, even with scan and scroll, how can I limit the result returned only include the id itself,  
> without all those meta data?
> 
> > On Wednesday, November 5, 2014 9:10:35 AM UTC-5, David Pilato wrote:  
> > So that's not the same story.
> > 
> > You want to do scan and scroll.
> > 
> > See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html)
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > > Le 5 nov. 2014 à 13:48, Ted Smith [tedsmi...@gmail.com](mailto:tedsmi...@gmail.com) a écrit :
> > > 
> > > Hello, David:
> > > 
> > > I have issue with trying to retrieve all document Ids (or a single field value of all documents) in an index.  
> > > I have about several million documents, but all I need is a list of document id (sorted if possible), nothing else.  
> > > It is taking 5 minutes now for me to get the results. All I need is the array/list of the Ids.  
> > > What would be the fastest and most efficient (no extra data returned) ways to do this in Java.
> > > 
> > > Thanks very much
> > > 
> > > > On Tuesday, November 4, 2014 12:57:44 PM UTC-5, Ap wrote:
> > > > 
> > > > Eg. Index has 100 documents with field A, B, C ....Z
> > > > 
> > > > I want to retrieve a list of all A's from the 100 documents in the Index.
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eb1b4706-2c66-4d8b-8264-f4364757f53e%40googlegroups.com).  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d9d9c5af-11cf-435f-9496-9c783b3f67f9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d9d9c5af-11cf-435f-9496-9c783b3f67f9%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9D2875A8-2445-4A8B-8C44-7F82836D39D6%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/9D2875A8-2445-4A8B-8C44-7F82836D39D6%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:51am UTC](https://discuss.elastic.co/t/using-java-how-to-retrieve-one-field-from-all-documents-inside-an-index/20564/7 "2017-07-06T00:51:55Z")

</div>


