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

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.

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

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

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.

Tika can do this ?

Or how Jackson works ?

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

HTH

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--

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-

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

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