1

How one can fill existing index in ES with multiple documents from JSON file using spring / spring-data-elasticsearch ?

EDIT

I am aware of ES BulkAPI and Spring's elasticsearchTemplate.bulkIndex(indexQueries); method however that way I have to handle JSON parsing on my own (I want to avoid this way). But in case when we already know the index and mapping I wonder if there is something more straightforward that would require only passing the foo.json file?

vsingh
  • 6,365
  • 3
  • 53
  • 57
mCs
  • 2,591
  • 6
  • 39
  • 66
  • Fill means you want to index data in a JSON file. Right ? – Sachin Aug 09 '16 at 05:53
  • I have a large JSON file that I want to put into ES that has corespondent index for the JSON data mapping. – mCs Aug 09 '16 at 08:12
  • You may want to look at http://stackoverflow.com/questions/20646836/is-there-any-way-to-import-a-json-filecontains-100-documents-in-elasticsearch – Sachin Aug 09 '16 at 08:44
  • I am aware of the BulkAPI if its what you had in mind. However, I am after the programmatic solution using spring. – mCs Aug 09 '16 at 09:17
  • You can use elasticsearchTemplate class(configured as bean) and then bulk index docs. – Sachin Aug 09 '16 at 09:19
  • I have just mentioned it in my EDIT AND LINK. As I already have the index and mapping I would like to avoid manual creation of each end every document programmatically and then add it to `indexQueries` – mCs Aug 09 '16 at 09:21
  • I've added an answer. You may check – Sachin Aug 09 '16 at 10:51

1 Answers1

0

I don't think there's a functionality like this in spring data elasticsearch. But you can do something like :-

1.Set proper mapping for index(with json structure)

2.Create index and put mapping.

3.Use JSON parser and parse input json file like this:-

     //parse json file
     JSONParser parser = new JSONParser();
     Object object = parser.parse(new FileReader("mydoc\\test.js"));
     JSONObject jsonData = (JSONObject) object;
     // index parsed contents
     IndexQuery indexQuery = new IndexQueryBuilder().withIndexName(indexName).
                        withSource(jsonData.toString()).build();
     elasticsearchTemplate.index(indexQuery);

Am not sure if this is what you are looking at,but still this may help.

Sachin
  • 1,675
  • 2
  • 19
  • 42