0

Here is my code to send the list:

for (int i = 0; i < list.size(); i++) {
        request.add(new IndexRequest("myindex", "doc").source(list,XContentType.JSON));
        count++;
        }

I tried converting the list in form of a map as well but it is creating index and hitting multiple times but not pushing any data :

for (int i = 0; i < list.size(); i++) {
        JSONObject dataAsJson = new JSONObject(list);
        HashMap<String, Object> dataAsMap = new HashMap<String, Object>(dataAsJson.toMap()); 
        request.add(new IndexRequest("myindex", "doc").source(dataAsMap,XContentType.JSON));
        }

Here is the sample data which I am trying to load :

           [{"Name" : "ABC",
          "Class" : "six",                                                    
          "Roll" : "330344953  ",
          "Team" : "XYX"
          },
           {"Name" : "AEBC",
          "Class" : "six",                                                    
          "Roll" : "3344953  ",
          "Team" : "XYZ"
          }]
5
  • can you sample of what data you are trying to index or what is content of list. Commented May 15, 2020 at 14:34
  • I have updated the data in question Commented May 15, 2020 at 14:48
  • Is this ObjectMapper a class of org.elasticsearch.index.mapper ? Commented May 15, 2020 at 15:15
  • Sorry for not mentioning it. It is a class of Jackson import com.fasterxml.jackson.databind.ObjectMapper; Will update in answer as well. Commented May 15, 2020 at 15:17
  • Thanks a lot! It solved my issue. Commented May 15, 2020 at 15:23

1 Answer 1

1

Assuming your index creation is successful, this is how you can post data present in list.

So, I have created list as :

List<Test> list = new ArrayList<Test>();

Test first = new Test();
first.setName("ABC");
first.setClassname("six");
first.setRoll(1);
first.setTeam("XYZ");

Test sec = new Test();
sec.setName("ABCDE");
sec.setClassname("tenth");
sec.setRoll(2);
sec.setTeam("XYZ");

list.add(first);
list.add(sec);

Test is having same object structure that you have mentioned :

class Test {

    private String name;
    private String classname;
    private int roll;
    private String team;
}

This is how you can use this list to index your documents :

Code :

    import com.fasterxml.jackson.databind.ObjectMapper;


    class TestService {

       private ObjectMapper objectMapper;
       @Autowired
       public TestService(ObjectMapper objectMapper) {
         this.objectMapper = objectMapper;
       }

       public String createBulkDocument() {

          BulkRequest request = new BulkRequest();
          IndexRequest indexRequest;
          for(int index=0;index<list.size();index++) {
                Map<String, Object> documentMapper = objectMapper.convertValue(list.get(index), Map.class);
                indexRequest = new IndexRequest("myindex", "doc")
                        .source(documentMapper);
                request.add(indexRequest);
            }
         BulkResponse bulkresp=client.bulk(request, RequestOptions.DEFAULT); --> `client` is your elasticsearch rest level client
        return bulkresp.status().toString();     ---> This will return the status
 whether your documents got indexed or not
      }
  }
Sign up to request clarification or add additional context in comments.

Comments

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.