Elastic Stack 是一个可以帮助我们构建搜索体验、解决问题并取得成功的搜索平台。核心产品包括 Elasticsearch、Kibana、Beats 和 Logstash(也称为 ELK Stack)等等。能够安全可靠地获取任何来源、任何格式的数据,然后实时地对数据进行搜索、分析和可视化。
Elasticsearch 和 Kibana 都是在免费开源的基础上构建而成,适用于各种各样的用例,从日志开始,到能够想到的任何项目,无一不能胜任。
Elasticsearch 是一个基于 JSON 的分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。 作为 Elastic Stack 的核心,它集中存储数据,帮助发现意料之中以及意料之外的情况。
Kibana 是一个免费且开放的用户界面,能够对 Elasticsearch 数据进行可视化,并在 Elastic Stack 中进行导航。我们可以进行各种操作,从跟踪查询负载,到理解请求如何流经整个应用,都能轻松完成。
Elasticsearch Java API Client 是自 7.16 版本开始稳定发布的官方 Java API 客户端。该客户端为所有 Elasticsearch API 提供强类型请求和响应。主要特性如下:
Elasticsearch Java API Client 是一个全新的客户端库,与旧的 High Level Rest Client (HLRC) 没有任何关系。它提供了一个独立于 Elasticsearch 服务器代码的库,并为所有 Elasticsearch 功能提供了一个非常一致且更易于使用的 API。
Elasticsearch Java API Client 围绕三个主要组件构建:
以下代码片段创建并将这三个组件连接在一起:
// 1. Create the low-level clientRestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();// 2. Create the transport with a Jackson mapperElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// 3. And create the API clientElasticsearchClient client = new ElasticsearchClient(transport);Elasticsearch 和 Kibana 安装,如果不想在本地安装 Elasticsearch 和 Kibana,可以使用官方提供的免费试用版 Elastic Cloud
Kibana 中创建小说索引:
PUT /book{ "mappings" : { "properties" : { "id" : { "type" : "long" }, "authorId" : { "type" : "long" }, "authorName" : { "type" : "text", "analyzer": "ik_smart" }, "bookName" : { "type" : "text", "analyzer": "ik_smart" }, "bookDesc" : { "type" : "text", "analyzer": "ik_smart" }, "bookStatus" : { "type" : "short" }, "categoryId" : { "type" : "integer" }, "categoryName" : { "type" : "text", "analyzer": "ik_smart" }, "lastChapterId" : { "type" : "long" }, "lastChapterName" : { "type" : "text", "analyzer": "ik_smart" }, "lastChapterUpdateTime" : { "type": "long" }, "picUrl" : { "type" : "keyword", "index" : false, "doc_values" : false }, "score" : { "type" : "integer" }, "wordCount" : { "type" : "integer" }, "workDirection" : { "type" : "short" }, "visitCount" : { "type": "long" } } }}<dependencies> <dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.2.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency></dependencies>spring: elasticsearch: uris: - https://my-deployment-ce7ca3.es.us-central1.gcp.cloud.es.io:9243 username: elastic password: qTjgYVKSuExX/** * elasticsearch 相关配置 * * @author xiongxiaoyang * @date 2022/5/23 */@Configuration@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "true")public class EsConfig { @Bean public ElasticsearchClient elasticsearchClient(RestClient restClient) { // Create the transport with a Jackson mapper ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); // And create the API client return new ElasticsearchClient(transport); }}说明:因为我们使用的是 Spring Boot 项目,当我们引入了 Java API Client 的 maven 相关依赖时,Spring Boot 的自动配置类 ElasticsearchRestClientAutoConfiguration 生效,会自动为我们配置一个 RestClient。所以 Elasticsearch Java API Client 连接三步骤的第一步Create the low-level client可以省略。
@AutoConfiguration@ConditionalOnClass({RestClientBuilder.class})@EnableConfigurationProperties({ElasticsearchProperties.class, ElasticsearchRestClientProperties.class})@Import({RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class, RestClientFromRestHighLevelClientConfiguration.class, RestClientConfiguration.class, RestClientSnifferConfiguration.class})public class ElasticsearchRestClientAutoConfiguration { public ElasticsearchRestClientAutoConfiguration() { }}