这里记录一下elasticsearch2.1.0文档的相关的操作,代码都是自己经过测试可以直接使用的代码。
/**
* es文档相关操作测试类
* @author yujie.wang
*/
public class DocOperator {
//es索引
private static String INDEX = "megacorp";
//索引megacorp下的类型
private static String TYPE = "employee";
public static void main(String [] args) throws Exception{
GenEsClient client = new GenEsClient();
DocOperator es = new DocOperator();
if(args.length >= 1 && "node".equals(args[0])){
System.out.println("genNodeClient");
es.getDocument(client.getNodeClient(), INDEX, TYPE, "4");
}else {
System.out.println("indexgenTransportClient");
//得到一条文档
//es.getDocument(client.genTransportClient(), INDEX, TYPE, "4");
//创建一个文档
//es.IndexDocument(client.genTransportClient(), INDEX, TYPE, "5");
//删除一个文档
//es.delDocument(client.genTransportClient(), INDEX, TYPE, "5");
//更新一个文档
//es.updateDocument(client.genTransportClient(), INDEX, TYPE, "4");
//批量操作
//es.bulk(client.genTransportClient());
es.checkIndexStatus(INDEX,client.genTransportClient());
}
}
public void checkIndexStatus(String index,Client client){
ClusterStateResponse response = client.admin().cluster().prepareState().setIndices(index).execute().actionGet();
ImmutableOpenMap<String, MappingMetaData> map = response.getState().getMetaData().index(index).getMappings();
Iterator<ObjectObjectCursor<String, MappingMetaData>> it = map.iterator();
while(it.hasNext()){
ObjectObjectCursor<String, MappingMetaData>