ElasticsearchClient示例

本文档提供了一个详细的Java Elasticsearch客户端使用示例,涵盖了连接、索引创建、文档插入、搜索等基本操作,帮助开发者快速理解和集成Elasticsearch到Java应用中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.whybackend;

public class Question {

    public Integer Id;

    public String Title;

    public String Answer;
}
package com.example.whybackend.elasticsearchclient;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

public class ElasticSearchConfig {

    public ElasticsearchClient getClient(){
        // 创建低级客户端
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();

        // 使用Jackson映射器创建传输层
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }

}

 

@Test
    void esCreateDoc() throws IOException{
        ElasticSearchConfig cfg = new ElasticSearchConfig();
        ElasticsearchClient client = cfg.getClient();

        Question q = new Question();
        q.Id = 3;
        q.Answer = "23333";
        q.Title = "佩罗西";

// 构建一个创建Doc的请求
        CreateResponse createResponse = client.create(e->e.index("question").id("3").document(q));
// 打印请求结果
        System.out.println(createResponse.result());
// 打印结果
    }
@Test
    void esSearchDoc() throws IOException{
        ElasticSearchConfig cfg = new ElasticSearchConfig();
        ElasticsearchClient client = cfg.getClient();

        SearchResponse<Question> search = client.search(s -> s
                        .index("question")
                        .query(q -> q
                                .match(t -> t
                                        .field("Title")
                                        .query("佩罗西")
                                )
                        ),
                Question.class);
        System.out.println("0");
    }

Elasticsearch 8.0 版本中,可以使用 Java High Level REST Client 来进行批量更新文档操作。下面是一个示例代码: ```java import org.apache.http.HttpHost; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; public class ElasticsearchClientExample { public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); BulkRequest bulkRequest = new BulkRequest(); // 添加需要批量更新的文档 bulkRequest.add(new IndexRequest("your_index", "your_type", "document_id").source(createDocBuilder())); BulkResponse bulkResponse = client.bulk(bulkRequest); // 处理结果 if (bulkResponse.hasFailures()) { // 批量更新失败 } else { // 批量更新成功 } client.close(); } private static XContentBuilder createDocBuilder() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); builder.field("field1", "value1"); builder.field("field2", "value2"); // 添加需要更新的字段和值 builder.field("field3", "value3"); builder.endObject(); return builder; } } ``` 在上述示例中,使用 RestHighLevelClient 来与 Elasticsearch 进行通信。首先创建一个 BulkRequest 对象,然后使用 `bulkRequest.add()` 方法添加需要批量更新的文档,其中需要指定索引、类型和文档 ID,以及新的字段和值。最后执行 `client.bulk()` 方法进行批量更新,并根据返回结果判断更新是否成功。 请注意替换示例代码中的 "your_index"、"your_type" 和 "document_id" 为实际的索引、类型和文档 ID。另外,还需要根据需求添加更多的文档更新操作到 BulkRequest 对象中。 希望这个示例能够帮助到你!如果还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值