elasticsearch8.0 java客户端 <一>

前言

本人在学习elasticsearch8.0时,因elasticsearch8.0缺少java文档,自己在摸索中学习es8.0 java。

elasticsearch8.0 java客户端

首先在es官网下载8.0,安装后,使用java调用es报错连接超时,es中提示
received plaintext http traffic on an https channel, closing connection Netty4HttpChannel
解决方法 因es8.0在安装时默认开启了SSL认证,在es安装根目录config/elasticsearch.yml文件修改

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12

enabled改为false

java连接es8.0

在改好yml启动后,在使用官网
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.0/connecting.html
提供的连接demo连接时异常,后来测试好像改完yml异常就消失了,不知道什么原因

RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);

在没测试之前使用

    private static final String clusterNodes = "127.0.0.1:9200";//es集群节点 
	//用户名
    private static final String account = "elastic";
	//密码
    private static final String passWord = "cs8I7+dQjh1BGwUaO_KM";

    public static ElasticsearchClient client;

    public static RestClientBuilder builder;

    public static RestClient restClient;

    public static ElasticsearchTransport transport;

    //有密码登录
    public static void main(String[] args) {
        try {
            HttpHost[] httpHosts = Arrays.stream(clusterNodes.split(",")).map(x -> {
                String[] hostInfo = x.split(":");
                return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1]));
            }).toArray(HttpHost[]::new);

            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            //设置账号密码
            credentialsProvider.setCredentials(
                    AuthScope.ANY, new UsernamePasswordCredentials(account, passWord));

            builder = RestClient.builder(httpHosts)
                    .setHttpClientConfigCallback(httpClientBuilder ->
                            httpClientBuilder
                                    .setDefaultCredentialsProvider(credentialsProvider));
			// Create the low-level client
            restClient = builder.build();
            // Create the transport with a Jackson mapper
            transport = new RestClientTransport(
                    restClient, new JacksonJsonpMapper());
            // And create the API client
            client = new ElasticsearchClient(transport);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.shutdown();
                transport.close();
                restClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

连接测试无问题

es8.0创建索引

 //创建索引并返回状态
 Boolean acknowledged = client.indices().create(c -> c.index("user")).acknowledged();

es8.0获取索引

GetIndexResponse getIndexResponse = client.indices().get(c -> c.index("user"));

es8.0删除索引

DeleteIndexResponse deleteIndexResponse = client.indices().delete(c -> c.index("user"));

es8.0创建doc

User user = new User("zhangsan", 18, "男");
String userJson = new Gson().toJson(user);

IndexResponse indexResponse = client.index(IndexRequest.of(x -> {
    x.id("10001");
    x.index("user");
    x.document(user);
    return x;
}));

以上方法不确定是否正确,本人自己测试无问题,文档中没有找到相应写法,如有问题,请大佬指正
下一篇链接: https://blog.csdn.net/u013979493/article/details/123172320?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
我理解你的问题是关于 Elasticsearch 8.0 Java API 中的 SearchResponse 类的使用。 SearchResponse 类是 Elasticsearch Java API 中的一个重要类,用于存储搜索请求的响应结果。可以通过该类获取搜索结果的各项信息,例如搜索结果的总数、搜索响应的状态信息以及搜索结果中的每个文档的具体信息。 在 Elasticsearch 8.0 中,可以通过以下方式创建 SearchResponse 对象: ``` SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); ``` 其中,searchRequest 是一个 SearchRequest 对象,用于定义搜索请求的具体参数,例如搜索的索引、搜索的查询语句等等。client 是 Elasticsearch客户对象,通过该客户对象可以执行各种 Elasticsearch 操作,包括搜索操作。 一旦执行搜索请求,就可以得到一个 SearchResponse 对象,可以通过该对象获取搜索结果的各项信息,例如搜索结果的总数: ``` long totalHits = response.getHits().getTotalHits().value; ``` 以上代码中,getHits() 方法返回一个 SearchHits 对象,该对象包含了所有搜索结果的详细信息。getTotalHits() 方法可以获取搜索结果的总数。value 属性可以获取具体的搜索结果总数值。 除了获取搜索结果的总数,还可以通过 SearchResponse 对象获取各个搜索结果的详细信息,例如搜索结果中第一个文档的具体信息: ``` SearchHit hit = response.getHits().getAt(0); String id = hit.getId(); String sourceAsString = hit.getSourceAsString(); ``` 以上代码中,getAt() 方法通过索引获取搜索结果中的具体文档信息。getId() 方法可以获取该文档的 ID 值,getSourceAsString() 方法可以获取该文档的具体内容。 以上是 Elasticsearch 8.0 Java API 中 SearchResponse 类的基本使用方法,希望可以帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值