Elasticsearch学习(九)————Java四种添加文档方式(Index API)

Index API————(添加文档)

官网地址https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high-document-index.html

1.pom文件添加依赖(注意:依赖版本和安装的es主版本相同)
    <!--es的rest风格的高级客户端 -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.4.0</version>
    </dependency>
2.获取客户端
    private static RestHighLevelClient getRestHighLevelClient() throws Exception {
        //1.指定es的集群  my-application:集群名称(在config目录下的elasticsearch.yml配置文件中查看)
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();
    
        //2.创建es的客户端  注意:9300为tcp端口;9200是http端口
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
    
        return client;
    }
3.添加文档的四种方式
  1. jsonString
  2. jsonMap
  3. XContentBuilder
  4. Object
3.1:jsonString
    	/**
         * @param : client
         * @description : 添加文档————jsonString
         */
    
        private static void addDocumentByJsonString(RestHighLevelClient client) throws Exception {
            //1.index请求
            IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "1");
            //2.构建jsonString参数
            Map<String, Object> paramsMap = new HashMap<>(16);
            List<String> list = new ArrayList<>();
            paramsMap.put("first_name", "cxk");
            paramsMap.put("last_name", "cxk");
            paramsMap.put("age", 25);
            paramsMap.put("about", "道可道,非常道");
            list.add("唱");
            list.add("跳");
            list.add("rap");
            list.add("篮球");
            paramsMap.put("interests", list);
            String jsonString = JSON.toJSONString(paramsMap);
            //3.设置请求数据(_source)
            indexRequest.source(jsonString, XContentType.JSON);
    
            //添加文档后的响应
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
            
            //index
            String index = indexResponse.getIndex();
            //type
            String type = indexResponse.getType();
            //id
            String id = indexResponse.getId();
            //version:版本
            long version = indexResponse.getVersion();
            //获取结果:创建 | 更新
            if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
    			//创建后的操作
            } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
    			//更新后的操作
            }
            //shardInfo:分片信息
            ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
            if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
    
            }
            //失败信息
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                    String reason = failure.reason(); 
                }
            }
        }

3.2 jsonMap
     	/**
         * @param : client
         * @description : 添加文档————jsonMap
         */
        private static void addDocumentByJsonMap(RestHighLevelClient client) throws Exception {
            
            IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "2");
            Map<String, Object> paramsMap = new HashMap<>(16);
            List<String> list = new ArrayList<>();
            paramsMap.put("first_name", "风月");
            paramsMap.put("last_name", "wind");
            paramsMap.put("age", 20);
            paramsMap.put("about", "道可道,非常道");
            list.add("书法");
            list.add("篮球");
            paramsMap.put("interests", list);
            
            indexRequest.source(paramsMap);
    
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
            
            Result result = indexResponse.getResult();
            String s = indexResponse.toString();
            System.out.println(result.toString());
            System.out.println(s);
        }
3.3 XContentBuilder
     /**
         * @param : client
         * @description : 添加文档————XContentBuilder
         */
        private static void addDocumentByXContentBuilder(RestHighLevelClient client) throws IOException {
    
            //1.创建文档对象
            List<String> list = new ArrayList<>();
            list.add("read");
            list.add("thinking");
            
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            builder.field("first_name", "欣儿");
            builder.field("last_name", "xin");
            builder.field("age", 18);
            builder.field("about", "beautiful");
            builder.field("interests", list);
            builder.endObject();
    
            IndexRequest indexRequest = new IndexRequest("my_index", "my_type", "3");
            indexRequest.source(builder);
            IndexResponse indexResponse=client.index(indexRequest,RequestOptions.DEFAULT);
            Result result = indexResponse.getResult();
            String s = indexResponse.toString();
            System.out.println(result.toString());
            System.out.println(s);
        }
3.4 Object
    /**
     * @param : client
     * @description : 添加文档————Object
     */
    private static void addDocumentByObject(RestHighLevelClient client) throws IOException {
    
        List<String> list = new ArrayList<>();
        list.add("哲学");
        list.add("计算机");
    
        IndexRequest request = new IndexRequest("my_index", "my_type", "4")
                .source("first_name","didi","last_name","dd","age",18,"about","yuan","interests", list);
        IndexResponse indexResponse=client.index(request,RequestOptions.DEFAULT);
        Result result = indexResponse.getResult();
        String s = indexResponse.toString();
        System.out.println(result.toString());
        System.out.println(s);
    }
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值