ElasticSearch学习(五)—— Java写入数据

  • 7.2.0版本
  • 获取客户端连接
private static int port = 9200;
private static String host = "192.168.1.105";

private static RestHighLevelClient getClient() {
    RestHighLevelClient client = new RestHighLevelClient(
    RestClient.builder(new HttpHost(host, port, "http")));
    return client;
}
  • 批量添加数据
/**
     * bulk批量添加
      */
    public static void batchAddDocuments(){
        try {
            RestHighLevelClient client = getClient();

            Map<String, Object> jsonMap = new HashMap<String, Object>();
            jsonMap.put("id", "01209");
            jsonMap.put("title", "紅樓夢");
            jsonMap.put("author", "曹雪芹");
            jsonMap.put("classification", "小說");
            IndexRequest request = new IndexRequest("book")
                    .id("2").source(jsonMap);

            jsonMap.put("id", "01210");
            jsonMap.put("title", "水滸傳");
            jsonMap.put("author", "施耐庵");
            jsonMap.put("classification", "小說");
            IndexRequest request1 = new IndexRequest("book")
                    .id("3").source(jsonMap);

            BulkRequest bulkRequest = new BulkRequest();
            bulkRequest.add(request);
            bulkRequest.add(request1);

            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            for (BulkItemResponse bulkItemResponse : bulkResponse) {
                DocWriteResponse itemResponse = bulkItemResponse.getResponse();
                switch (bulkItemResponse.getOpType()) {
                    case INDEX:
                        break;
                    case CREATE:
                        IndexResponse indexResponse = (IndexResponse) itemResponse;
                        System.out.println("新增文档成功!");
                        break;
                    case UPDATE:
                        UpdateResponse updateResponse = (UpdateResponse) itemResponse;
                        System.out.println("更新文档成功!");
                        break;
                    case DELETE:
                        DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
                        System.out.println("删除文档成功!");
                        break;
                    default:
                }
            }
            client.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
  • 根据索引id删除
/**
     * 根据索引id删除
     */
    public static void deleteDocument(){
        try {
            RestHighLevelClient client = getClient();
            DeleteRequest request = new DeleteRequest(
                    "ancientbook",
                    "2607");
            DeleteResponse deleteResponse = client.delete(
                    request, RequestOptions.DEFAULT);
            System.out.println(deleteResponse.getResult());
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 根据索引id获取
/**
     * 根据索引id获取
     */
    public static void getIndex() {
        try {
            RestHighLevelClient client = getClient();
            GetRequest getRequest = new GetRequest(
                    "ancientbook",
                    "2607");
            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            String index = getResponse.getIndex();
            String id = getResponse.getId();
            if (getResponse.isExists()) {
                long version = getResponse.getVersion();
                String sourceAsString = getResponse.getSourceAsString();
                Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
                byte[] sourceAsBytes = getResponse.getSourceAsBytes();
                System.out.println("index:" + index);
                System.out.println("id:" + id);
                System.out.println("version:" + version);
                System.out.println("sourceAsString:" + sourceAsString);
                System.out.println("sourceAsMap:" + sourceAsMap);
                System.out.println("sourceAsBytes:" + sourceAsBytes);
            } else {

            }
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值