【ES代码操作索引库】

ES代码操作索引库

  1. 创建索引库
    @Test
    public void createIndex() throws IOException {

    //使用client获取操作索引的对象
    IndicesClient indicesClient = restHighLevelClient.indices();
    
    //操作索引对象执行操作,获取返回值
    CreateIndexRequest createRequest = new CreateIndexRequest("myfirst");
    CreateIndexResponse response = indicesClient.create(createRequest, RequestOptions.DEFAULT);
    
    //返回结果判断
    System.out.println(response.isAcknowledged());
    

    }`

  2. 带mapping创建索引`

    @Test
    public void createIndexAndMapping() throws IOException {

    //使用高级客户端获取操作Index对象
    IndicesClient indicesClient = restHighLevelClient.indices();
    //创建索引(带索引的Index)
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("mytwo");
    String mapping = "{\n" +
            "  \"properties\":{\n" +
            "    \"id\":{\n" +
            "      \"type\":\"long\",\n" +
            "      \"store\":true,\n" +
            "      \"index\":true\n" +
            "    },\n" +
            "    \"name\":{\n" +
            "      \"type\":\"keyword\"\n" +
            "    },\n" +
            "    \"email\":{\n" +
            "      \"type\":\"text\",\n" +
            "      \"analyzer\":\"standard\"\n" +
            "    }\n" +
            "  }\n" +
            "}";
    createIndexRequest.mapping(mapping, XContentType.JSON);
    CreateIndexResponse createIndexResponse = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
    
    //打印结果
    System.out.println(createIndexResponse.isAcknowledged());
    

    }`

  3. 查询索引库`
    @Test
    public void queryIndex() throws IOException {

    //索引客户端创建
    IndicesClient indicesClient = restHighLevelClient.indices();
    
    //查询
    GetIndexRequest getIndexRequest = new GetIndexRequest("mytwo");
    GetIndexResponse getIndexResponse = indicesClient.get(getIndexRequest, RequestOptions.DEFAULT);
    
    
    //解析响应
    Map<String, MappingMetaData> mappings = getIndexResponse.getMappings();
    for (String key : mappings.keySet()) {
        System.out.println("key: " + key + ",  value:" + mappings.get(key).getSourceAsMap());
    }
    

    }
    `

  4. 删除索引库`
    @Test
    public void deleteIndex() throws IOException {

    //高级客户端获取索引客户端
    IndicesClient indicesClient = restHighLevelClient.indices();
    
    //删除操作
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("mytwo");
    AcknowledgedResponse deleteResponse = indicesClient.delete(deleteIndexRequest, RequestOptions.DEFAULT);
    System.out.println(deleteResponse.isAcknowledged());
    

    }`

  5. 判断索引是否存在 @Test public void existIndex() throws IOException { IndicesClient indicesClient = restHighLevelClient.indices(); GetIndexRequest getIndexRequest = new GetIndexRequest("mytwo"); boolean exists = indicesClient.exists(getIndexRequest, RequestOptions.DEFAULT); System.out.println(exists); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值