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"
}]
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:
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.