4

I am trying to retrieve data form ES query and I am confused about how can I transform each hit of retrieved data to Java Object. Until now, I can get each hit in JSON format using Gson but I think is useless to transform from Hit to JSON and then to Java Object.

My current code is:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    Settings settings = Settings.builder()
            .put("cluster.name", "elasticsearch")
            .put("path.home", "/Users/user/Apps/elasticsearch-5.4.1")
            .build();

    QueryBuilder qb = termQuery("price", 12);
    SearchResponse response = client.prepareSearch("kal").setTypes("products")
            .setSearchType(SearchType.DEFAULT)
            .setQuery(qb)
            .get();

    SearchHit[] results = response.getHits().getHits();
    for (SearchHit hit : results) {
        String sourceAsString = hit.getSourceAsString();
        Map<String, SearchHitField> responseFields = hit.getFields();
        if (sourceAsString != null) {
            Gson gson = new GsonBuilder().setDateFormat(sourceAsString)
                    .create();
        }
    }
5
  • I'm not sure what you are trying to achieve there. You practically already have the java objects (the SearchHit array named results ). Do you want to get a reference to a child object of a SearchHit? Commented Jun 8, 2017 at 6:41
  • Is there a direct way to map the SearchHit to another Object? Or you need to implement constructor? Commented Jun 8, 2017 at 6:47
  • If you see here stackoverflow.com/a/30376770/4343294 you retrieve JSON object from SearchHit Commented Jun 8, 2017 at 6:49
  • oh, i see.. you want to get the source of SearchHit and map it to another custom object? or do you need the whole SearchHit? Commented Jun 8, 2017 at 7:01
  • This code is working: SearchHit[] results = response.getHits().getHits(); for (SearchHit hit : results) { String sourceAsString = hit.getSourceAsString(); Map<String, SearchHitField> responseFields = hit.getFields(); SearchHitField field = responseFields.get("product_id"); Map map = hit.getSource(); System.out.println(map.toString()); } Commented Jun 8, 2017 at 12:06

3 Answers 3

4

One line

YourObject yourObject = new com.fasterxml.jackson.databind.ObjectMapper().convertValue(searchHit.getSourceAsMap(), YourObjectClass.class);
Sign up to request clarification or add additional context in comments.

Comments

1

I implemented this solution but I am not sure if this is the proper way or not.

SearchHit[] results = response.getHits().getHits();
    for (SearchHit hit : results) {
        String sourceAsString = hit.getSourceAsString();
        Map<String, SearchHitField> responseFields = hit.getFields();
        SearchHitField field = responseFields.get("product_id");
        Map map = hit.getSource();
        System.out.println(map.toString());
    }

Comments

1
    I am working with Elasticsearch version 7.4.2

    SearchHit[] searchHit = response.getHits().getHits();
            for (SearchHit hit : searchHit) {
                String jsonString=hit.getSourceAsString();
                TempClass tmpCl=(TempClass )JSONToObject.parser(jsonString, TempClass .class);
                System.out.println(tmpCl.toString());
            }


    // JSONToObject class
    private static Object dc;
    public static Object parser(String file, Class<?> cls) {
        try {
                Gson gson = new Gson();
                dc = gson.fromJson(file, cls);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dc;
    }

1 Comment

Please add explanation to your answer.

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.