备忘: java 查询es7索引补全

参考该文中https://blog.csdn.net/MinggeQingchun/article/details/126762570
查询索引一节,补充,先判断索引是否存在,再查询具体索引细节,并加上异常处理,代码如下:

import org.apache.http.HttpHost;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class testES {

    public static  void main(String[] args) throws Exception {

        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        try {

            String indexName = "test-2023-12-06";
            GetIndexRequest request = new GetIndexRequest(indexName);

            //boolean exists = esClient.indices().exists(request, RequestOptions.DEFAULT);

            if (esClient.indices().exists(request, RequestOptions.DEFAULT)) {
                //查询索引

                GetIndexResponse getIndexResponse =
                        esClient.indices().get(request, RequestOptions.DEFAULT);
                System.out.println(getIndexResponse.getAliases());
                System.out.println(getIndexResponse.getMappings());
                System.out.println(getIndexResponse.getSettings());

            } else {
                System.out.println("index:" + request + " is ont existed");
            }
        } catch (ElasticsearchException e) {
            e.printStackTrace();

        } finally {
            try {
                esClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

索引存在,结果为
{test_sw_traces-2023-12-06=[]}
{test_sw_traces-2023-12-06=org.elasticsearch.cluster.metadata.MappingMetadata@f9d7aff9}
{test_sw_traces-2023-12-06={“index.creation_date”:“1701826112459”,“index.number_of_replicas”:“1”,“index.number_of_shards”:“1”,“index.provided_name”:“test_sw_traces-2023-12-06”,“index.uuid”:“EEgJnZhlSWqyWHoALzNaZg”,“index.version.created”:“7090299”}}

换成String indexName = “test-2023-12-05”;
索引不存在,结果为
index:org.elasticsearch.client.indices.GetIndexRequest@623a8092 is ont existed

复杂查询可在该框架进行扩展

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhyuli

您的鼓励是对我付出努力一种赞赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值