ElasticSearch API for JAVA 学习笔记
/*** es-api的方法学习:
* 1.prepareIndex方法:索引数据到ElasticSearch
* 2.prepareGet方法:获取信息
* 3.prepareDelete方法:删除信息
* 4.update方法:更新信息
* 4.1 upsert:在使用update方法时:
* a:针对文档不存在的情况时,做出index数据的操作,update无效;
* b:如果文档存在,那么index数据操作无效,update有效;
*/
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
//通过Settings类设置属性参数
Settings settings = Settings.settingsBuilder().put("cluster.name","myApp").build();
//启动Client
Client client = null;
try {
client = TransportClient.builder().settings(settings).build().
addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("101.200.124.27"),9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
//执行操作
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("user","yuchen")
.field("interest","reading book")
.field("insert_time",df.format(new Date()))
.endObject();
//1.prepareIndex方法:索引数据到ElasticSearch
IndexResponse response = client.prepareIndex("index-test","weibo","4")
.setSource(jsonBuilder)
.get();
String _index = response.getIndex();
String _type = response.getType();
String _id = response.getId();
long _version = response.getVersion();
boolean created = response.isCreated();
System.out.println(_index+" "+_type+" "+_id+" "+_version+" "+created);
//2.prepareGet方法:获取信息
GetResponse getResponse = client.prepareGet("index-test","weibo","1").get();
System.out.println(getResponse.getSourceAsString());
//3.prepareDelete方法:删除信息
DeleteResponse deleteResponse = client.prepareDelete("index-test","weibo","4").get();
System.out.println(deleteResponse.isFound());
//4.update方法:更新信息
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index-test");
updateRequest.type("weibo");
updateRequest.id("1");
updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("interest","music").endObject());
UpdateResponse updateResponse = client.update(updateRequest).get();
System.out.println(updateResponse.isCreated());
//update方法: 可以为已有的文档添加新的字段
UpdateResponse updateResponse2 = client.prepareUpdate("index-test", "weibo", "1")
.setDoc(XContentFactory.jsonBuilder()
.startObject()
.field("interest2","reading")
.endObject()).get();
System.out.println(updateResponse2.isCreated());
//4.1 upsert:在使用update方法时,a:针对文档不存在的情况时,做出index数据的操作,update无效;
// b:如果文档存在,那么index数据操作无效,update有效;
//先构建一个IndexRequest
IndexRequest indexRequest = new IndexRequest("index-test","weibo","14");
indexRequest.source(XContentFactory.jsonBuilder()
.startObject()
.field("user","yuchen2")
.field("interest","eating")
.field("insert_time",df.format(new Date()))
.endObject());
//再构建一个UpdateRequest,并用IndexRequest关联
UpdateRequest updateRequest3 = new UpdateRequest("index-test","weibo","14");
updateRequest3.doc(XContentFactory.jsonBuilder()
.startObject()
.field("interest2","love")
.endObject()
).upsert(indexRequest);
client.update(updateRequest3).get();
if(client != null){
client.close();
}
}
页:
[1]