ElasticSearch 6.x 学习笔记:27.Java API之文档管理

1、文档获取

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;

public class GetDocDemo {
    public static void main(String[] args){
        TransportClient client=ESUtil.getClient();
        GetResponse response =client.prepareGet("index1","blog","1").get();
        System.out.println(response.isExists());
        System.out.println(response.getIndex());
        System.out.println(response.getType());
        System.out.println(response.getId());
        System.out.println(response.getVersion());
        String source=response.getSourceAsString();
        System.out.println(source);
    }
}

这里写图片描述

2、文档删除

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
public class DeleteDocDemo {
    public static void main(String[] args){
        TransportClient client=ESUtil.getClient();
        DeleteResponse response=client.prepareDelete("index1","blog","1").get();
        //删除成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被删除文档的类型
        System.out.println(response.getType());
        //返回被删除文档的ID
        System.out.println(response.getId());
        //返回被删除文档的版本信息
        System.out.println(response.getVersion());
    }
}

这里写图片描述

3、文档更新

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
public class UpdateDocDemo {
    public static void main(String[] args)throws Exception{
        TransportClient client=ESUtil.getClient();
        UpdateRequest request=new UpdateRequest();
        request.index("index1")
                .type("blog")
                .id("2")
                .doc(
                        jsonBuilder().startObject()
                        .field("title","单例模式解读")
                        .endObject()
                );
        UpdateResponse response=client.update(request).get();
        //更新成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被更新文档的类型
        System.out.println(response.getType());
        //返回被更新文档的ID
        System.out.println(response.getId());
        //返回被更新文档的版本信息
        System.out.println(response.getVersion());
    }
}

这里写图片描述

4、文档upsert操作

package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 *  文档upsert操作:如果文档存在则执行更新操作,否则执行添加操作
 */
public class UpsertDocDemo {
    public static void main(String[] args)throws Exception{
        TransportClient client=ESUtil.getClient();
        IndexRequest request1 =new IndexRequest("index1","blog","1")
                .source(
                        jsonBuilder().startObject()
                                .field("id","1")
                                .field("title","装饰模式")
                                .field("content","动态地扩展一个对象的功能")
                                .field("postdate","2018-02-03 14:38:10")
                                .field("url","csdn.net/79239072")
                                .endObject()
                );
        UpdateRequest request2=new UpdateRequest("index1","blog","1")
                .doc(
                        jsonBuilder().startObject()
                        .field("title","装饰模式解读")
                        .endObject()
                ).upsert(request1);
        UpdateResponse response=client.update(request2).get();
        //upsert操作成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被操作文档的类型
        System.out.println(response.getType());
        //返回被操作文档的ID
        System.out.println(response.getId());
        //返回被操作文档的版本信息
        System.out.println(response.getVersion());
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值