Skip to content

Instantly share code, notes, and snippets.

@KnowledgeGarden
Created September 15, 2014 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KnowledgeGarden/b67b9f38a2ea9a29c21a to your computer and use it in GitHub Desktop.
Save KnowledgeGarden/b67b9f38a2ea9a29c21a to your computer and use it in GitHub Desktop.
Section of code which initializes mappings
protected void checkIndexes(IResult result) {
List<List<String>>indexes = (List<List<String>>)environment.getProperties().get("IndexNames");
List<String>indices = new ArrayList<String>();
environment.setIndices(indices);
int len = indexes.size();
ImmutableSettings.Builder settings = null;
IndicesExistsRequestBuilder ib;
IndicesExistsResponse ir;
CreateIndexRequest cir;
CreateIndexResponse afr;
String idx = null;
String mappx = null;
JSONObject mappy = null;
IResult rx;
for (int i=0;i<len;i++) {
idx = indexes.get(i).get(1);
idx = indexes.get(i).get(0);
mappx = indexes.get(i).get(1);
rx = getMappings(mappx);
mappy = (JSONObject)rx.getResultObject();
indices.add(idx);
ib = getClient().admin().indices().prepareExists(idx);
ir = ib.get("1000");
System.out.println("BAR "+idx+" "+ir.isExists());
String mapx;
if (!ir.isExists()) {
try {
settings = ImmutableSettings.settingsBuilder();
settings.put("path.data", "data/");
settings.build();
mapx = createMapping(idx,mappy);
cir = new CreateIndexRequest(idx)
.mapping(IJSONDocStoreOntology.CORE_TYPE, mapx)
.settings(settings);
afr = getClient().admin().indices().create(cir).get();
environment.logDebug("ElasticSearchEmbeddedModel-1 "+afr.isAcknowledged()+" "+mapx);
} catch (Exception x) {
environment.logError(x.getMessage(), x);
throw new RuntimeException(x);
}
} else {
//check the mappings
ClusterState cs = client.admin().cluster().prepareState().setIndices(idx).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(idx);
MappingMetaData mdd = imd.mapping("core");
try {
Map<String,Object>mx = mdd.sourceAsMap();
System.out.println(mx);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment