上一篇文章介绍了ELK的使用,这里继续学习Elasticsearch的相关内容。
一,ElasticSearch的使用场景:Elasticsearch的使用场景深入详解_狂奔的蜗牛Evan的博客-CSDN博客_elasticsearch使用场景
二,ElasticSearch与关系数据库的对比
Elasticsearch | 关系数据库 |
---|---|
索引(index) | 数据库 |
类型(type) | 表,新版本默认是_doc |
映射(mapping) | 表结构 |
属性(field) | 字段 |
文档(document) | 一条记录 |
三,如何向 Elasticsearch 添加一些索引、映射和数据
- 启动 elasticsearch.bat
- 默认地址:http://localhost:9200
1)Restful API 使用方式 - 使用PostMan发送请求
- 索引操作
- 映射操作
- 文档操作
2)Kibana 使用方式 - 使用Dev-Tools
- 启动 kibana .bat
- 默认地址:http://localhost:5601
直接在左边控制台输入语句就可以执行成功了。
四,Java API 的使用方式
elasticsearch官方在8.0版本以后,已经舍弃了High level rest clint Api,推荐使用java clint api。
官方文档:Installation | Elasticsearch Java API Client [8.4] | Elastic
1)在项目中的使用
- pom.xml引入依赖包
co.elastic.clients
elasticsearch-java
8.4.2
com.fasterxml.jackson.core
jackson-databind
2.12.3
jakarta.json
jakarta.json-api
2.0.1
- application.yml 添加配置
# =========================================================================
elasticSearch:
url: 127.0.0.1
port: 9200
# ==========================================================================
- 自定义Client配置类
package com.qi.study.springboot.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
public class ElasticSearchConfig {
@Value("${elasticSearch.url}")
private String url;
@Value("${elasticSearch.port}")
private Integer port;
@Bean
public ElasticsearchClient elasticsearchClient() {
// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost(url, port)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
return client;
}
}
- 添加测试代码,结构如下:
Controller
studentService
2)启动测试
- 启动elasticSearch.bat
- 启动logstash 【logstash -f ./config/logstash-es.conf】
- 启动springboot
- 通过postman访问测试:
本篇文章主要是了解如何去使用ElasticSearch,有个初步的认知,更多高级搜索方法可以根据需要进一步去学习。
五,源代码下载: https://download.csdn.net/download/MyNoteBlog/86727519