elasticsearch7.1 Java API-索引、创建文档

             在咱们写创建索引,删除索引之前需要在咱们测试类中提前做好准备,第一是引入咱们的客户端(client),每次执行完都要将client释放资源,所以咱们可以用junit的After注解,在执行完方法后释放资源.over 废话不多说,直接上代码。

准备

        引入客户端(client)

   /**
     * 日志记录
     */
    private static final Logger logger= LoggerFactory.getLogger(ElasticsearchTest.class);
    @Autowired
    private RestHighLevelClient restHighLevelClient;

        @After

    /*
     * 释放资源
     */
    @After
    public void close(){
        try {
            restHighLevelClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

over,做好了准备工作,就可以上手啦~

索引

1、是创建索引

    /**
     * 创建索引测试
     */
    @Test
    public void createIndex() throws IOException {
        //创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("test_index");
        //相关配置
        request.settings(Settings.builder()
                //分片数
                .put("index.number_of_shards",3)
                //备份数
                .put("index.number_of_replicas",2)
        );
        //同步执行
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        logger.info("-------------------test_index created:"+createIndexResponse.isAcknowledged()+"--------------------------------");
    }

2、查看索引是否存在

 /**
     * 判断索引是否存在
     */
    @Test
    public void exist() throws IOException {
        GetIndexRequest request = new GetIndexRequest("test_index");
        boolean flag = restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
        logger.info("-------------test_index exists:"+flag+"----------------------");
    }

3、删除索引

    /*
     * 删除索引
     */
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest deleteRequest = new DeleteIndexRequest("test_index");
        AcknowledgedResponse response = restHighLevelClient.indices().delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println("Delete document successfully! \n" + response.isAcknowledged());
    }

弄完了,就可以在kibana中去查看咱们的所有索引啦,进入kibana 查看所有索引

GET _cat/indices

创建文档

1、使用字符串的形式构建文档信息

    /**
     * 创建文档(1)
     */
    @Test
    public void createDoc() throws IOException {
        //索引
        IndexRequest request = new IndexRequest("test_index");
        request.id("1");
        //使用字符串的形式来构建内容
        String jsonStr="{"+"\"user\""+":"+"\"旺仔呀\""+"}";
        request.source(jsonStr, XContentType.JSON);
        //同步执行
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
       
    }

2、map形式构建文档内容

    /**
     * 创建文档(2)
     */
    @Test
    public void createDoc2() throws IOException {
        //索引
        IndexRequest request = new IndexRequest("test_index");
        request.id("2");
        //使用map来构建内容
        Map<String,Object> map=new HashMap<>();
        map.put("user","马冬梅呀");
        map.put("sex","女");
        map.put("age",33);
        request.source(map,XContentType.JSON);
        //同步执行
       restHighLevelClient.index(request,RequestOptions.DEFAULT);

    }

3、使用xContentBuilder来构建内容

 /**
     * 创建文档(3)
     */
    @Test
    public void createDoc3() throws IOException {
        //使用xContentBuilder来构建内容
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
        xContentBuilder.startObject();
        {
            xContentBuilder.field("user","夏洛呀");
            xContentBuilder.timeField("birthday",new Date());
            xContentBuilder.field("sex","男");
        }
        xContentBuilder.endObject();
        IndexRequest request = new IndexRequest("test_index").id("3").source(xContentBuilder);
        //同步执行
       restHighLevelClient.index(request,RequestOptions.DEFAULT);
       
    }

4、直接用键值对对象架构数据

    /**
     * 创建文档(4)
     */
    @Test
    public void createDoc4() throws IOException {
        //直接用键值对对象架构数据
        IndexRequest request = new IndexRequest("test_index").id("4").source("user", "袁华", "sex", "男", "age", "18");
        //同步执行
      restHighLevelClient.index(request,RequestOptions.DEFAULT);
      
    }

创建完索引咱们就可以在咱们的kibana里面去查看咱们的文档啦

查看所有test_index索引下所有的文档

GET test_index/_doc/_search

根据id查找文档 

GET test_index/_doc/1

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值