1、文档获取
代码语言:javascript
复制package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
public class GetDocDemo {
public static void main(String[] args){
TransportClient client=ESUtil.getClient();
GetResponse response =client.prepareGet("index1","blog","1").get();
System.out.println(response.isExists());
System.out.println(response.getIndex());
System.out.println(response.getType());
System.out.println(response.getId());
System.out.println(response.getVersion());
String source=response.getSourceAsString();
System.out.println(source);
}
}
2、文档删除
代码语言:javascript
复制package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
public class DeleteDocDemo {
public static void main(String[] args){
TransportClient client=ESUtil.getClient();
DeleteResponse response=client.prepareDelete("index1","blog","1").get();
//删除成功返回OK,否则返回NOT_FOUND
System.out.println(response.status());
//返回被删除文档的类型
System.out.println(response.getType());
//返回被删除文档的ID
System.out.println(response.getId());
//返回被删除文档的版本信息
System.out.println(response.getVersion());
}
}
3、文档更新
代码语言:javascript
复制package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
public class UpdateDocDemo {
public static void main(String[] args)throws Exception{
TransportClient client=ESUtil.getClient();
UpdateRequest request=new UpdateRequest();
request.index("index1")
.type("blog")
.id("2")
.doc(
jsonBuilder().startObject()
.field("title","单例模式解读")
.endObject()
);
UpdateResponse response=client.update(request).get();
//更新成功返回OK,否则返回NOT_FOUND
System.out.println(response.status());
//返回被更新文档的类型
System.out.println(response.getType());
//返回被更新文档的ID
System.out.println(response.getId());
//返回被更新文档的版本信息
System.out.println(response.getVersion());
}
}
4、文档upsert操作
代码语言:javascript
复制package cn.hadron;
import cn.hadron.es.ESUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
* 文档upsert操作:如果文档存在则执行更新操作,否则执行添加操作
*/
public class UpsertDocDemo {
public static void main(String[] args)throws Exception{
TransportClient client=ESUtil.getClient();
IndexRequest request1 =new IndexRequest("index1","blog","1")
.source(
jsonBuilder().startObject()
.field("id","1")
.field("title","装饰模式")
.field("content","动态地扩展一个对象的功能")
.field("postdate","2018-02-03 14:38:10")
.field("url","csdn.net/79239072")
.endObject()
);
UpdateRequest request2=new UpdateRequest("index1","blog","1")
.doc(
jsonBuilder().startObject()
.field("title","装饰模式解读")
.endObject()
).upsert(request1);
UpdateResponse response=client.update(request2).get();
//upsert操作成功返回OK,否则返回NOT_FOUND
System.out.println(response.status());
//返回被操作文档的类型
System.out.println(response.getType());
//返回被操作文档的ID
System.out.println(response.getId());
//返回被操作文档的版本信息
System.out.println(response.getVersion());
}
}