码迷,mamicode.com
首页 > 编程语言 > 详细

Elasticsearch JavaApi

时间:2017-12-04 20:41:58      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:客户   trying   pre   std   import   res   upd   cti   ttl   

 

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();
12         
13         return response.isCreated();
14         
15     }

 

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         
 9         return response.isFound();
10         
11     }

 

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();
10             
11             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();
7             
8             System.out.println(updateResponse.isCreated());

 

 

4.查询

 

未完待续

Elasticsearch JavaApi

标签:客户   trying   pre   std   import   res   upd   cti   ttl   

原文地址:http://www.cnblogs.com/IT-study/p/7978580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!