RestHighLevelClient操作ES7.4.0

 记录一下,后续研究RestHighLevelClient:

    /**
     * 插入数据
     * */
        @Test
    public void index() {
        try {
            // 1. 初始化
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));

            // 2. 创建索引
            IndexRequest indexRequest = new IndexRequest("posts");
            indexRequest.id("1");
            String jsonString = "{" +
                    "\"user\":\"chargedot\"," +
                    "\"Date\":\"2019-10-25\"," +
                    "\"message\":\"trying out Elasticsearch\"" +
                    "}";
            indexRequest.source(jsonString, XContentType.JSON);

             // 3. 条件设置
            indexRequest.routing("routing");
            indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
            indexRequest.setRefreshPolicy("wait_for");

            // 4. 监听
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
            ActionListener listener = new ActionListener() {
                @Override
                public void onResponse(IndexResponse indexResponse) {
                    log.info("indexResponse succeed!");
                }
                @Override
                public void onFailure(Exception e) {
                    log.info("indexResponse failed!");
                }
            };
            client.close();
        } catch (IOException e) {
            log.info("IOException", e);
        }
    }

 

你可能感兴趣的:(大数据初探)