java - Specify indexName and type of index in elasticsearch in a properties file -
i'm using elasticsearch , spring in application. each index type, have document mapping. using @document
annotation have specified indexname
, type
of index. eg: @document(indexname = "myproject", type = "user")
. writing unit tests, want create indexes different indexname. hence want indexname read properties file. how in spring?
just utilize elasticsearchtemplate unit tests create index different name , utilize method "index" or "bulkindex" index document new index created.
estemplate.createindex(newindexname, loadfromfromfile(settingsfilename)); estemplate.putmapping(newindexname, "user", loadfromfromfile(usermappingfilename)); list<indexquery> indexes = users.parallelstream().map(user -> { indexquery index = new indexquery(); index.setindexname(newindexname); index.settype("user"); index.setobject(user); index.setid(string.valueof(user.getid())); homecoming index; }).collect(collectors.tolist()); estemplate.bulkindex(indexes); //load file src/java/resources or /src/test/resources public string loadfromfromfile(string filename) throws illegalstateexception { stringbuilder buffer = new stringbuilder(2048); seek { inputstream = getclass().getresourceasstream(filename); linenumberreader reader = new linenumberreader(new inputstreamreader(is)); while (reader.ready()) { buffer.append(reader.readline()); buffer.append(' '); } } grab (exception e) { throw new illegalstateexception("couldn't load file " + filename, e); } homecoming buffer.tostring(); }
this should work working me. same scenario.
java spring elasticsearch spring-data-elasticsearch
No comments:
Post a Comment