# Java - How to get the ID of all results of a search?

**URL:** https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662
**Category:** Elasticsearch
**Created:** [June 8, 2017, 6:42am UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662 "2017-06-08T06:42:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![froufrou2224](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@froufrou2224](https://discuss.elastic.co/u/froufrou2224)
#### Post date: [June 8, 2017, 6:42am UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/1 "2017-06-08T06:42:28Z")

</div>

Hello,

I'm working on a project of search engine ! But when we try to search documents according to a condition. The program sends back a the Json response. So I would like to get back all the id of the results. How can I do that ?

Thank you.

---

<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: [June 8, 2017, 7:32am UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/2 "2017-06-08T07:32:37Z")

</div>

If the resultset is small you can increase the size parameter to get all records at once.

Otherwise you need to use the scroll API.

In the result you have \_id which is the document id

---

<div class="post-metadata">

### Author: ![froufrou2224](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@froufrou2224](https://discuss.elastic.co/u/froufrou2224)
#### Post date: [June 8, 2017, 3:55pm UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/3 "2017-06-08T15:55:20Z")

</div>

But I want to retrieve my "objects" with this id. How can I do that ?  
Is there a method ? Like getObject(the id) ?

---

<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: [June 8, 2017, 5:04pm UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/4 "2017-06-08T17:04:11Z")

</div>

No. You need to read the Json \_source field and map it back to your beans.

You can use Jackson or any other parser you wish for this.

---

<div class="post-metadata">

### Author: ![froufrou2224](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@froufrou2224](https://discuss.elastic.co/u/froufrou2224)
#### Post date: [June 8, 2017, 6:01pm UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/5 "2017-06-08T18:01:51Z")

</div>

Tika can do this ?

---

<div class="post-metadata">

### Author: ![froufrou2224](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@froufrou2224](https://discuss.elastic.co/u/froufrou2224)
#### Post date: [June 8, 2017, 6:08pm UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/6 "2017-06-08T18:08:13Z")

</div>

Or how Jackson works ?

---

<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: [June 8, 2017, 10:01pm UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/7 "2017-06-08T22:01:19Z")

</div>

No Tika can't do this.  
Jackson. Well you can do something like:

```
     public static <T> T deserialize(InputStream stream, Class<T> clazz) {
        try {
            return MetaParser.mapper.readValue(stream, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

```

Code from:

[https://github.com/dadoonet/fscrawler/blob/master/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/JsonUtil.java](https://github.com/dadoonet/fscrawler/blob/master/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/JsonUtil.java)

HTH

---

<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: [June 9, 2017, 6:17am UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/8 "2017-06-09T06:17:14Z")

</div>

Not sure what exactly do you need guidance on since you have said both "So  
I would like to get back all the id of the results." and "I want to  
retrieve my "objects" with this id.

The way I parse it, the former is asking to search for results and then  
return the id, while the latter is asking to search by the id.

For the former, if you already have the search results, the you can iterate  
through them and easily get the id. searchResponse.getHits().getHits() and  
then getId() on the SearchHit:  
[https://static.javadoc.io/org.elasticsearch/elasticsearch/5.4.1/org/elasticsearch/search/SearchHit.html#getId--](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.4.1/org/elasticsearch/search/SearchHit.html#getId--)

The latter is a simple GetRequest using the index, type and id:  
[https://static.javadoc.io/org.elasticsearch/elasticsearch/5.4.1/org/elasticsearch/action/get/GetRequest.html#GetRequest-java.lang.String-java.lang.String-java.lang.String-](https://static.javadoc.io/org.elasticsearch/elasticsearch/5.4.1/org/elasticsearch/action/get/GetRequest.html#GetRequest-java.lang.String-java.lang.String-java.lang.String-)

You mentioned Java in the subject, so these examples use the Java API.

---

<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 7, 2017, 6:17am UTC](https://discuss.elastic.co/t/java-how-to-get-the-id-of-all-results-of-a-search/88662/9 "2017-07-07T06:17:39Z")

</div>

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