A Coder Gamer
A Coder Gamer

Reputation: 840

How to return all the results in Elasticsearch using Java?

I've a data set indexed in elasticsearch. (index-name: demoindex1, type-name: SearchTech). I want to get all the results back in JSON format using Java. My Java code is as follows:

import java.net.InetAddress;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;

public class App {

  public static void main(String[] args) throws Exception {
    Client client = TransportClient.builder().build().addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName("localhost"),
            9300));

    SearchResponse scrollResp = client.prepareSearch("demoindex1")
        .setScroll(new TimeValue(60000)).setTypes("SearchTech").setSize(1000)
        .execute().actionGet();
    // Scroll until no hits are returned
    while (true) {

      for (SearchHit hit : scrollResp.getHits()) {
        System.out.println(hit.getSourceAsString()); //hit.get
      }
      scrollResp = client.prepareSearchScroll(scrollResp.getScrollId())
          .setScroll(new TimeValue(60000)).execute().actionGet();
      // Break condition: No hits are returned
      if (scrollResp.getHits().getHits().length == 0) {
        break;
      }
    }
  }
}

My java program returns the source part of each hit. But I want to get all results back as we get in Elasticsearch using the query GET index/type/_search. I need the result dataset back in JSON format. What am I doing wrong here?

-I've read the JAVA documentation for elasticsearch but I couldn't find anything about getting all the result dataset back.

Thank you.

Upvotes: 1

Views: 1718

Answers (1)

Arad
Arad

Reputation: 168

I cannot comment so I can only write my comment here: I might have understood your question, but please correct me if I am wrong.

Why don't you try scrollResp.toString() if you want to get the whole JSON for GET index/type/_search? Then you can just read the JSON tree from the string using one of the abundant libraries available, example com.fasterxml.jackson.

Or if you are looking for a JSON for a _source of a hit, you may convert the result of hit.getSourceAsString() to JSON using those libraries.

Upvotes: 1

Related Questions