Easysearch 的很多用户都有这样的需要,之前是用的 ES,现在要迁移到 Easysearch,但是业务方使用的是 Spring Boot 集成的客户端,问是否能平滑迁移。
Easysearch 是完全兼容 Spring Boot 的,完全不用修改,本指南将探讨如何将 Spring Boot 和 ES 的 high-level 客户端 与 Easysearch 进行集成,涵盖从基础设置到实现 CRUD 操作和测试的所有内容。
首先,需要修改 Easysearch 节点的 easysearch.yml 文件,打开并配置这 2 个配置项:
elasticsearch.api_compatibility: true #根据客户端版本配置版本号,我这里配置成 7.17.18 elasticsearch.api_compatibility_version: "7.17.18"
然后,让我们设置 Maven 依赖。以下是 pom.xml
中的基本配置:
<properties> <java.version>11</java.version> <spring-data-elasticsearch.version>4.4.18</spring-data-elasticsearch.version> <elasticsearch.version>7.17.18</elasticsearch.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>${spring-data-elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
完全和连接 Elasticsearch 的方式一样,不用修改:
配置 src/main/resources/application.yml 文件
spring: elasticsearch: rest: uris: https://localhost:9202 username: admin password: xxxxxxxxxxx ssl: verification-mode: none
连接配置类
@Configuration public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { @Value("${spring.elasticsearch.rest.uris}") private String elasticsearchUrl; @Value("${spring.elasticsearch.rest.username}") private String username; @Value("${spring.elasticsearch.rest.password}") private String password; @Override @Bean public RestHighLevelClient elasticsearchClient() { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(null, (x509Certificates, s) -> true) .build(); RestClientBuilder builder = RestClient.builder(HttpHost.create(elasticsearchUrl)) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider) .setSSLContext(sslContext) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)); return new RestHighLevelClient(builder); } }
使用 Spring 的 Elasticsearch 注解定义领域模型:
@Data @Document(indexName = "products") public class Product { @Id private String id; @Field(type = FieldType.Text, name = "name") private String name; @Field(type = FieldType.Double, name = "price") private Double price; }
创建继承 ElasticsearchRepository 的仓库接口:
@Repository @EnableElasticsearchRepositories public interface ProductRepository extends ElasticsearchRepository<Product, String> { }
实现服务层来处理业务逻辑:
@Service public class ProductService { private final ProductRepository productRepository; @Autowired public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } public Product saveProduct(Product product) { return productRepository.save(product); } public Product findProductById(String id) { return productRepository.findById(id).orElse(null); } }
编写集成测试类:
@SpringBootTest public class ProductServiceIntegrationTest { @Autowired private ElasticsearchOperations elasticsearchOperations; @Autowired private ProductService productService; private static final String INDEX_NAME = "products"; @BeforeEach public void setUp() { IndexOperations indexOperations = elasticsearchOperations.indexOps(IndexCoordinates.of(INDEX_NAME)); if (indexOperations.exists()) { indexOperations.delete(); } // 定义 mapping Document mapping = Document.create() .append("properties", Document.create() .append("name", Document.create() .append("type", "text") .append("analyzer", "standard")) .append("price", Document.create() .append("type", "double"))); // 创建索引并应用 mapping indexOperations.create(Collections.EMPTY_MAP, mapping); } @Test public void testSaveAndFindProduct() { List<Product> products = Arrays.asList( new Product("Test Product 1", 99.99), new Product("Test Product 2", 199.99), new Product("Test Product 3", 299.99) ); List<IndexQuery> queries = products.stream() .map(product -> new IndexQueryBuilder() .withObject(product) .withIndex(INDEX_NAME) .build()) .collect(Collectors.toList()); List<IndexedObjectInformation> indexedInfos = elasticsearchOperations.bulkIndex( queries, IndexCoordinates.of(INDEX_NAME) ); // 验证结果 List<String> ids = indexedInfos.stream() .map(IndexedObjectInformation::getId) .collect(Collectors.toList()); assertFalse(ids.isEmpty()); assertEquals(products.size(), ids.size()); } }
本指南展示了 Easysearch 与 Elasticsearch 的高度兼容性:
配置方式相同,仅需启用 Easysearch 的 API 兼容模式。
可直接使用现有 Elasticsearch 客户端。
Maven 依赖无需更改。
API、注解和仓库接口完全兼容。
现有测试代码可直接应用。
这种兼容性使得从 Elasticsearch 迁移到 Easysearch 成为一个简单、低风险的过程。Spring Boot 项目可以几乎无缝地切换到 Easysearch,同时获得其性能和资源利用方面的优势。
INFINI Easysearch 是一个分布式的搜索型数据库,实现非结构化数据检索、全文检索、向量检索、地理位置信息查询、组合索引查询、多语种支持、聚合分析等。Easysearch 可以完美替代 Elasticsearch,同时添加和完善多项企业级功能。Easysearch 助您拥有简洁、高效、易用的搜索体验。
官网文档:https://infinilabs.cn/docs/latest/easysearch
作者:张磊,极限科技(INFINI Labs)搜索引擎研发负责人,对 Elasticsearch 和 Lucene 源码比较熟悉,目前主要负责公司的 Easysearch 产品的研发以及客户服务工作。
原文:https://infinilabs.cn/blog/2024/use-spring-boot-for-easysearch-connection/