Elasticsearch入门Demo(二)

1.向索引库添加json字符串

/**
     * 添加索引:传入json字符串
     */
    @Test
    public void addIndex2()
    {
        String jsonStr = "{" + "\"userName\":\"张三\"," + "\"sendDate\":\"2017-11-30\"," + "\"msg\":\"你好李四\"" + "}";
        IndexResponse response = client.prepareIndex("weixin", "tweet").setSource(jsonStr, XContentType.JSON).get();
        logger.info("json索引名称:" + response.getIndex() + "\njson类型:" + response.getType() + "\njson文档ID:"
                + response.getId() + "\n当前实例json状态:" + response.status());


    }


2.向索引库添加一个Map集合

 /**
     * 创建索引-传入Map对象
     */
    @Test
    public void addIndex3()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userName", "张三");
        map.put("sendDate", new Date());
        map.put("msg", "你好李四");


        IndexResponse response = client.prepareIndex("momo", "tweet").setSource(map).get();


        logger.info("map索引名称:" + response.getIndex() + "\n map类型:" + response.getType() + "\n map文档ID:"
                + response.getId() + "\n当前实例map状态:" + response.status());

    }


3.向索引库添加JsonObject

/**
     * 传递json对象
     */
    @Test
    public void addIndex4()
    {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userName", "张三");
        jsonObject.addProperty("sendDate", "2017-11-23");
        jsonObject.addProperty("msg", "你好李四");


        IndexResponse response = client.prepareIndex("qq", "tweet").setSource(jsonObject, XContentType.JSON).get();


        logger.info("jsonObject索引名称:" + response.getIndex() + "\n jsonObject类型:" + response.getType()
                + "\n jsonObject文档ID:" + response.getId() + "\n当前实例jsonObject状态:" + response.status());

    }


4.从索引库获取数据

/**
     * 从索引库获取数据
     */
    @Test
    public void getData1()
    {
        GetResponse getResponse = client.prepareGet("msg", "tweet", "1").get();
        logger.info("索引库的数据:" + getResponse.getSourceAsString());

    }


5.更新索引库数据

/**
     * 更新索引库数据
     */
    @Test
    public void updateData()
    {
        JsonObject jsonObject = new JsonObject();


        jsonObject.addProperty("userName", "王五");
        jsonObject.addProperty("sendDate", "2008-08-08");
        jsonObject.addProperty("msg", "你好,张三,好久不见");


        UpdateResponse updateResponse = client.prepareUpdate("msg", "tweet", "1")
                .setDoc(jsonObject.toString(), XContentType.JSON).get();


        logger.info("updateResponse索引名称:" + updateResponse.getIndex() + "\n updateResponse类型:"
                + updateResponse.getType() + "\n updateResponse文档ID:" + updateResponse.getId()
                + "\n当前实例updateResponse状态:" + updateResponse.status());

    }


6.删除索引库数据

/**
     * 根据索引名称,类别,文档ID 删除索引库的数据
     */
    @Test
    public void deleteData()
    {
        DeleteResponse deleteResponse = client.prepareDelete("msg", "tweet", "1").get();


        logger.info("deleteResponse索引名称:" + deleteResponse.getIndex() + "\n deleteResponse类型:"
                + deleteResponse.getType() + "\n deleteResponse文档ID:" + deleteResponse.getId()
                + "\n当前实例deleteResponse状态:" + deleteResponse.status());

    }


posted @ 2018-04-04 17:20  小草1234  阅读(118)  评论(0编辑  收藏  举报