Elasticsearch fetching only schema using Java REST API


(Roopesh Kumar) #1

Hi,
When trying to fetch data from elasticsearch using Java rest API, I am getting only the schema as JSON, and not the data.
Can you please help me.
Here is what I am doing:

    RestClient restClient =
              RestClient.builder(new HttpHost("localhost", 9200, "http")).build();

    String query = "{\n" +
            "\"query\": {\n" +
            "\"bool\": {\n" +
            "\"must\": [\n" +
            "   {\n" +
            "      \"match_all\": {}\n" +
            "   }\n" +
            "]\n" +
            "}\n" +
            "}\n" +
            "\"from\": 0,\n" +
            "\"size\": 10" +
            "}";

    HttpEntity entity = new NStringEntity(
            query, ContentType.APPLICATION_JSON);

    Response contentResponse =
            restClient.performRequest("GET", "/customers",
                    Collections.singletonMap("pretty", "true"), entity);
    String contentResult = EntityUtils.toString(contentResponse.getEntity()); 
    System.out.println("Content JSON : " + contentResult);

Please suggest.


(Yannick Welsch) #2

restClient.performRequest("GET", "/customers", ...

you have to use the correct endpoint, "/customers/_search", see
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html


(Roopesh Kumar) #3

Thanks @ywelsch,
You are right. I tried with that also and that didn't work so I raised this question.
Actually the query, I am sending was incorrect. I missed a comma ( , ).

The query should be :

String query = "{\n" +
        "\"query\": {\n" +
        "\"bool\": {\n" +
        "\"must\": [\n" +
        "   {\n" +
        "      \"match_all\": {}\n" +
        "   }\n" +
        "]\n" +
        "}\n" +
        "},\n" +
        "\"from\": 0,\n" +
        "\"size\": 10" +
        "}"; 

Thanks again


(system) #4

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