Elasticsearch JavaApi
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Elasticsearch JavaApi,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2287字,纯文字阅读大概需要4分钟。
内容图文
1.创建索引与数据
把json字符写入索引,索引库名为twitter、类型为tweet,id为1
语法
import static org.elasticsearch.common.xcontent.XContentFactory.*; IndexResponse response = client.prepareIndex("twitter", "tweet", "1") .setSource(jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject() ) .get();
相关用例
1 public static boolean create(String index, String type, @Nullable String id,String json){ 2 3 // index:索引库名 4 // type:类型 5 // id:文档的id 6 // json:json字符串 7 // response.isCreated():创建是否成功 8 IndexResponse response = client.prepareIndex(index, type, id) 9// .setSource("{ \"title\": \"Mastering ElasticSearch\"}")10 .setSource(json) 11 .execute().actionGet(); 1213return response.isCreated(); 1415 }
2.删除索引与数据
索引库名为twitter、类型为tweet,id为1
语法
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
相关用例
1 public static boolean remove(String index, String type, String id){ 2 3 // index:索引库名 4 // type:类型 5 // id:文档的id 6 // response.isFound():是否删除成功 7 DeleteResponse response = client.prepareDelete(index, type, id).get(); 8 9return response.isFound(); 1011 }
3.修改数据
你可以创建一个UpdateRequest并将其发送到客户端:
UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("index"); updateRequest.type("type"); updateRequest.id("1"); updateRequest.doc(jsonBuilder() .startObject() .field("gender", "male") .endObject()); client.update(updateRequest).get();
相关用例
1 UpdateRequest updateRequest = new UpdateRequest(); 2 updateRequest.index("library"); 3 updateRequest.type("book"); 4 updateRequest.id("1"); 5 updateRequest.doc(XContentFactory.jsonBuilder() 6 .startObject() 7 .field("name", "jackRose222") 8 .endObject()); 9 UpdateResponse updateResponse = client.update(updateRequest).get(); 1011 System.out.println(updateResponse.isCreated());
也可以用prepareUpdate()
方法
client.prepareUpdate("ttl", "doc", "1") .setDoc(jsonBuilder() .startObject() .field("gender", "male") .endObject()) .get();
相关用例
1 UpdateResponse updateResponse = client.prepareUpdate("library", "book", "1") 2 .setDoc(XContentFactory.jsonBuilder() 3 .startObject() 4 .field("name", "male2") 5 .endObject()) 6 .get(); 78 System.out.println(updateResponse.isCreated());
4.查询
未完待续
原文:http://www.cnblogs.com/IT-study/p/7978580.html
内容总结
以上是互联网集市为您收集整理的Elasticsearch JavaApi全部内容,希望文章能够帮你解决Elasticsearch JavaApi所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。