备忘: java 查询es7索引补全

2023-12-20 05:09:50

参考该文中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

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

文章来源:https://blog.csdn.net/zhyuli/article/details/135091475
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。