Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created June 15, 2015 06:50
Show Gist options
  • Save stephen-maina/be1f4e47d58b30a0ae15 to your computer and use it in GitHub Desktop.
Save stephen-maina/be1f4e47d58b30a0ae15 to your computer and use it in GitHub Desktop.
elasticsearch java code snippets
private boolean deleteIndex(String indexName, String typeName, String id) {
if (isIndexExist(indexName)) {
System.out.println("INDEX EXISTS>>>DELETE INDEX");
return NodeOperation.INSTANCE.getClient().prepareDelete(indexName, typeName, id).execute().actionGet()
.isFound();
} else {
return false;
}
}
private boolean inputIndex(String indexName, String typeName, String id, String source) {
if (isTypeExist(indexName, typeName)) {
System.out.println("INDEX EXISTS>>>INPUTTING");
return NodeOperation.INSTANCE.getClient().prepareIndex(indexName, typeName, id).setSource(source).execute()
.actionGet().isCreated();
} else {
return false;
}
}
private boolean updateIndex(String indexName, String typeName, String id, String source) {
if (isIndexExist(indexName)) {
System.out.println("INDEX EXISTS>>>UPDATING");
return NodeOperation.INSTANCE.getClient().prepareUpdate(indexName, typeName, id).setDoc(source).execute()
.actionGet().isCreated();
} else {
return false;
}
}
private boolean updatePartialIndex(String indexName, String typeName, String id, HashMap<String, String> partials) {
if (isIndexExist(indexName)) {
System.out.println("INDEX EXISTS>>>UPDATING PARTIAL");
String script = "";
for (String name : partials.keySet()) {
script += "ctx._source." + name + "=\"" + partials.get(name) + "\";";
}
return NodeOperation.INSTANCE.getClient().prepareUpdate(indexName, typeName, id)
.setScript(script, ScriptService.ScriptType.INLINE).get().isCreated();
} else {
return false;
}
}
private boolean updateAddPartialIndex(String indexName, String typeName, String id, HashMap<String, String> partials) {
if (isIndexExist(indexName)) {
System.out.println("INDEX EXISTS>>>UPDATEW ADD PARTIAL");
String script = "";
UpdateRequest request = new UpdateRequest(indexName, typeName, id);
int index = 0;
for (String name : partials.keySet()) {
script += "ctx._source." + name + " += new_services_" + index + ";";
request.addScriptParam("new_services_" + index, partials.get(name));
index++;
}
try {
return NodeOperation.INSTANCE.getClient()
.update(request.script(script, ScriptService.ScriptType.INLINE)).get().isCreated();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} else {
return false;
}
}
private boolean deletePartialIndex(String indexName, String typeName, String id, HashMap<String, String> partials) {
if (isIndexExist(indexName)) {
System.out.println("INDEX EXISTS>>>DELETE PARTIAL");
String script = "";
UpdateRequest request = new UpdateRequest(indexName, typeName, id);
int index = 0;
for (String name : partials.keySet()) {
script += "ctx._source.services.remove(old_services_" + index + ");";
request.addScriptParam("old_services_" + index, partials.get(name));
index++;
}
try {
return NodeOperation.INSTANCE.getClient()
.update(request.script(script, ScriptService.ScriptType.INLINE)).get().isCreated();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} else {
return false;
}
}
private boolean isTypeExist(String indexName, String typeName) {
System.out.println("IS TYPE EXIST?");
return NodeOperation.INSTANCE.getClient().admin().indices().prepareTypesExists(indexName).setTypes(typeName)
.execute().actionGet().isExists();
}
private boolean isIndexExist(String indexName) {
System.out.println("IS INDEX EXIST?");
return NodeOperation.INSTANCE.getClient().admin().indices().prepareExists(indexName).execute().actionGet()
.isExists();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment