ES7.8 Spring Boot 集成

1. 简介

Elasticsearch 是一个开源的分布式全文搜索和分析引擎,支持实时搜索、分布式搜索和数据分析等功能。Spring Boot 是一个用于快速构建企业级应用的框架,提供了很多便利的功能,使得开发者可以快速开发出高效、可扩展的应用。本文将介绍如何在 Spring Boot 中集成 Elasticsearch 7.8,并通过代码示例演示其基本用法。

2. 准备工作

首先,我们需要在项目的 pom.xml 文件中添加 Elasticsearch 的依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.0</version>
</dependency>

3. 创建 Elasticsearch 客户端

在 Spring Boot 中,我们可以通过自动配置来创建 Elasticsearch 的客户端。在 application.properties 文件中添加以下配置:

spring.elasticsearch.rest.uris=http://localhost:9200

然后,在代码中注入 RestHighLevelClient 对象:

import org.elasticsearch.client.RestHighLevelClient;

@Component
public class MyElasticsearchClient {

    private final RestHighLevelClient restHighLevelClient;

    public MyElasticsearchClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    // ...
}

4. 创建索引

在 Elasticsearch 中,数据存储在索引中。我们可以通过 Elasticsearch 的客户端来创建索引。下面是一个创建索引的示例代码:

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyIndexCreator {

    private final RestHighLevelClient client;

    public MyIndexCreator(RestHighLevelClient client) {
        this.client = client;
    }

    public void createIndex(String indexName) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.settings(Settings.builder()
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 0)
        );
        XContentBuilder mapping = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("title")
                .field("type", "text")
                .endObject()
                .startObject("content")
                .field("type", "text")
                .endObject()
                .endObject()
                .endObject();
        request.mapping(mapping);
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        if (response.isAcknowledged()) {
            System.out.println("Index created successfully.");
        } else {
            System.out.println("Failed to create index.");
        }
    }
}

5. 添加文档

在 Elasticsearch 中,文档是存储在索引中的基本单位。我们可以通过 Elasticsearch 的客户端来添加文档。下面是一个添加文档的示例代码:

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyDocumentIndexer {

    private final RestHighLevelClient client;

    public MyDocumentIndexer(RestHighLevelClient client) {
        this.client = client;
    }

    public void addDocument(String indexName, String documentId, String title, String content) throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .field("title", title)
                .field("content", content)
                .endObject();

        IndexRequest request = new IndexRequest(indexName)
                .id(documentId)
                .source(builder);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println("Document added successfully. Document ID: " + response.getId());
    }
}

6. 搜索文档

在 Elasticsearch 中,我们可以根据条件搜索文档。下面是一个搜索文档的示例代码:

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query