1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. Yes, I want the whole Json tree in the output. I'm gonna try what you said. Thank you for answering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.