zoukankan      html  css  js  c++  java
  • es之java操作插入文档

    4方式:

    1、  使用json字符串直接创建
    2、  使用Map集合
    3、  使用第三方库来序列化  createDocumentBySerialize
    4、  使用内置的帮助器XContentFactory.jsonBuilder()

    1: 使用JSON字符串创建

    @Test
    public void createDocumentByManually(){
        String json = "{" +
                ""user":"kimchy"," +
                ""postDate":"2013-01-30"," +
                ""message":"trying out Elasticsearch"" +
                "}";
        //IndexRequestBuilder prepareIndex(String index, String type)
        final IndexResponse response  = this.transportClient.prepareIndex("twitter", "tweet")
                .setSource(json, XContentType.JSON).get();
        //获取索引
        final String _index = response.getIndex();
        //获取类型
        final String _type = response.getType();
        // 文档ID
        String _id = response.getId();
        // 版本
        long _version = response.getVersion();
        // 返回的操作状态
        RestStatus status = response.status();
        System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status);
    
    }

    2:使用Map集合

    @Test
    public void createDocumentByMap(){
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("user","kimchy");
        json.put("postDate",new Date());
        json.put("message","trying out Elasticsearch");
        //this.transportClient.prepareIndex 可以传入id
        final IndexResponse response = this.transportClient.prepareIndex("twitter", "tweet")
                .setSource(json, XContentType.JSON).get();
        //获取索引
        final String _index = response.getIndex();
        //获取类型
        final String _type = response.getType();
        // 文档ID
        String _id = response.getId();
        // 版本
        long _version = response.getVersion();
        // 返回的操作状态
        RestStatus status = response.status();
        System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status);
    }

    3:使用第三方库来序列化

    /**
     *这种方式是使用jsckson来序列化一个bean的方式进行操作的
     * import com.fasterxml.jackson.databind.*;
     * */
    
    @Test
    public void createDocumentBySerialize(){
    
        try {
            // insstance a json mapper
            ObjectMapper mapper = new ObjectMapper(); // create once, reuse
            //构造一个类
            Person p = new Person();
            p.setUser("kimchy");
            p.setPostDate(new Date());
            p.setMessage("trying out Elasticsearch");
            // generate json
            byte[] json = mapper.writeValueAsBytes(p);
            IndexResponse response = this.client.prepareIndex("twitter3", "tweet")
                    .setSource(json, XContentType.JSON)
                    .get();
            // 索引名称
            String _index = response.getIndex();
            // 类型
            String _type = response.getType();
            // 文档ID
            String _id = response.getId();
            // 版本
            long _version = response.getVersion();
            // 返回的操作状态
            RestStatus status = response.status();
            System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status);
    
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    4:使用内置的帮助器jsonBuilder()

    @Test
    public void createDocumentByJsonBuilder(){
        XContentBuilder builder = null;
        try {
            builder = jsonBuilder()
                    .startObject()
                       .field("user", "kimchy")
                       .field("postDate", new Date())
                       .field("message", "trying out Elasticsearch")
                    .endObject();
            String json = builder.string();
            IndexResponse response = this.client.prepareIndex("twitter4", "tweet")
                    .setSource(json, XContentType.JSON)
                    .get();
            // 索引名称
            String _index = response.getIndex();
            // 类型
            String _type = response.getType();
            // 文档ID
            String _id = response.getId();
            // 版本
            long _version = response.getVersion();
            // 返回的操作状态
            RestStatus status = response.status();
            System.out.println("索引名称:"+_index+" "+"类型 :" +  _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status);
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }

    去elasticsearch的head页面查看:

     

  • 相关阅读:
    Bootstrap学习笔记系列2-------Bootstrap简单表格处理
    Bootstrap学习笔记系列1-------Bootstrap网格系统
    前端代码规范
    Dev TreeList设置焦点失败解决方法
    las数据集加载las数据
    c# 文件另存为代码
    Dev 饼图
    ASP.NET MVC Json的序列化和反序列化
    服务器重启后导致访问ArcServer地图服务须登录
    jQuery回调函数
  • 原文地址:https://www.cnblogs.com/niutao/p/10909204.html
Copyright © 2011-2022 走看看