# Get only ids with no source Java API

**URL:** <https://discuss.elastic.co/t/get-only-ids-with-no-source-java-api/20281>\
**Category:** Elasticsearch\
**Created:** [October 16, 2014, 9:45am UTC](https://discuss.elastic.co/t/get-only-ids-with-no-source-java-api/20281 "2014-10-16T09:45:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilija\_Subasic](https://avatars.discourse-cdn.com/v4/letter/i/ee7513/32.png) [@Ilija\_Subasic](https://discuss.elastic.co/u/Ilija_Subasic)\
**Post date:** [October 16, 2014, 9:45am UTC](https://discuss.elastic.co/t/get-only-ids-with-no-source-java-api/20281/1 "2014-10-16T09:45:08Z")

</div>

Is there a way in elasticsearch using JAVA API to get only the ids of the  
documents returned for a give query.

```
SearchResponse sr = 

```

esClient.prepareSearch(index).setSize(resultSize).setQuery(q).setScroll(new  
TimeValue(10000)).setQuery(fqb).setFetchSource(false).get();

but I get empty hits (`sr.getHits().hits[].length == 0`) althouh the total  
count of returned hits is 0 (`sr.getHits().getTotalHits == 2`). I  
understand that nothing is returned by elasticsearch because I set fetch  
source to false, but the ids should somehow be available. My current  
solution is:

```
SearchResponse sr = 

```

esClient.prepareSearch(index).setSize(resultSize).setQuery(q).setScroll(new  
TimeValue(10000)).setQuery(fqb).srb.setFetchSource("\_id", null).get();

However I think that gets the \_id field from source, and for speed I would  
like to avoid this if possible.

Thanks,  
Ilija

--  
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/089b670f-763c-4795-859a-720767d24a81%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/089b670f-763c-4795-859a-720767d24a81%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 17, 2014, 8:18pm UTC](https://discuss.elastic.co/t/get-only-ids-with-no-source-java-api/20281/2 "2014-10-17T20:18:35Z")

</div>

Have you tried setting no fields to be returned or the explicit  
setNoFields() method?

[http://jenkins.elasticsearch.org/job/Elasticsearch%20Master%20Branch%20Javadoc/Elasticsearch\_API\_Documentation/org/elasticsearch/action/search/SearchRequestBuilder.html#setNoFields()](http://jenkins.elasticsearch.org/job/Elasticsearch%20Master%20Branch%20Javadoc/Elasticsearch_API_Documentation/org/elasticsearch/action/search/SearchRequestBuilder.html#setNoFields())

--  
Ivan

On Thu, Oct 16, 2014 at 2:45 AM, Ilija Subasic [subasic.ilija@gmail.com](mailto:subasic.ilija@gmail.com)  
wrote:

> Is there a way in elasticsearch using JAVA API to get only the ids of the  
> documents returned for a give query.
> 
> ```
> SearchResponse sr =
> 
> ```
> 
> esClient.prepareSearch(index).setSize(resultSize).setQuery(q).setScroll(new  
> TimeValue(10000)).setQuery(fqb).setFetchSource(false).get();
> 
> but I get empty hits (`sr.getHits().hits[].length == 0`) althouh the total  
> count of returned hits is 0 (`sr.getHits().getTotalHits == 2`). I  
> understand that nothing is returned by elasticsearch because I set fetch  
> source to false, but the ids should somehow be available. My current  
> solution is:
> 
> ```
> SearchResponse sr =
> 
> ```
> 
> esClient.prepareSearch(index).setSize(resultSize).setQuery(q).setScroll(new  
> TimeValue(10000)).setQuery(fqb).srb.setFetchSource("\_id", null).get();
> 
> However I think that gets the \_id field from source, and for speed I would  
> like to avoid this if possible.
> 
> Thanks,  
> Ilija
> 
> --  
> 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/089b670f-763c-4795-859a-720767d24a81%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/089b670f-763c-4795-859a-720767d24a81%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/089b670f-763c-4795-859a-720767d24a81%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/089b670f-763c-4795-859a-720767d24a81%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/CALY%3DcQCYskSm7WyTDX5LVCrcL%2BR5y%2B2e9fUBTH0Z8iamu06OBg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCYskSm7WyTDX5LVCrcL%2BR5y%2B2e9fUBTH0Z8iamu06OBg%40mail.gmail.com).  
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:55am UTC](https://discuss.elastic.co/t/get-only-ids-with-no-source-java-api/20281/3 "2017-07-06T00:55:24Z")

</div>


