1、简介
elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容。
ELK技术栈
elasticsearch结合kibana、Logstash、Beats,也就是elastic stack(ELK)。被广泛应用在日志数据分析、实时监控等领域。elasticsearch是elastic stack的核心,负责存储、搜索、分析数据。
正向索引
正向索引是最传统的,根据id索引的方式。但根据词条查询时,必须先逐条获取每个文档,然后判断文档中是否包含所需要的词条,是根据文档找词条的过程
倒排索引
而倒排索引则相反,是先找到用户要搜索的词条,根据词条得到保护词条的文档的id,然后根据id获取文档。是根据词条找文档的过程
倒排索引中有两个非常重要的概念:
文档(
Document
):用来搜索的数据,其中的每一条数据就是一个文档。例如一个网页、一个商品信息。【每一条数据就是一个文档】词条(
Term
):对文档数据或用户搜索数据,利用某种算法分词,得到的具备含义的词语就是词条。例如:我是中国人,就可以分为:我、是、中国人、中国、国人这样的几个词条。 【对文档中的内容分词,得到的词语就是词条】
创建倒排索引是对正向索引的一种特殊处理,流程如下:
将每一个文档的数据利用算法分词,得到一个个词条
创建表,每行数据包括词条、词条所在文档id、位置等信息
因为词条唯一性,可以给词条创建索引,例如hash表结构索引
倒排索引的搜索流程如下(以搜索"华为手机"为例):
1)用户输入条件
"华为手机"
进行搜索。2)对用户输入内容分词,得到词条:
华为
、手机
。3)拿着词条在倒排索引中查找,可以得到包含词条的文档id:1、2、3。
4)拿着文档id到正向索引中查找具体文档。
2、es
2.1 文档和字段
2.2 索引和映射
2.3 概念对比
Mysql:擅长事务类型操作,可以确保数据的安全和一致性
Elasticsearch:擅长海量数据的搜索、分析、计算
3、索引库操作
4、文档操作
5、RestAPI
这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:Elasticsearch Clients | Elastic
初始化RestClient
1)引入es的RestHighLevelClient依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本【需要跟服务端的版本一致】
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
3)初始化RestHighLevelClient
@SpringBootTest
public class HotelIndexTest {
private RestHighLevelClient client;
@BeforeEach // 用于表示应在当前类中的 每个@Test方法之前 执行注解方法
void setUp(){
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.234.128:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
6、利用JavaRestClient实现创建、删除索引库,判断索引库是否存在
// --------------------------操作索引库--------------------------------
@Test
void createHotelIndex() throws IOException {
// 1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.准备请求的参数:DSL语句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
@Test
void deleteHotelIndex() throws IOException {
// 1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 2.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@Test
void ExistsHotelIndex() throws IOException {
// 1.创建Request对象
GetIndexRequest request = new GetIndexRequest("hotel");
// 2.发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "索引库存在!" : "索引库不存在!");
}
7、文档操作
@SpringBootTest
public class HotelDocTest {
private RestHighLevelClient client;
@Test
void testInit(){
System.out.println(client);
}
@BeforeEach
void setUp(){
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.234.128:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
新增
// --------------------------操作文档--------------------------------
@Test
void createDoc() throws IOException {
// 根据id查询酒店数据
Hotel hotel = hotelService.getById(61083L);
// 转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
// 1.准备Request对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
// 2. 准备json文档
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON); // JSON.toJSONString(hotelDoc) 序列化
// 3.发送请求
client.index(request,RequestOptions.DEFAULT);
}
查询
@Test
void getDocById() throws IOException {
// 1.准备Request
GetRequest request = new GetRequest("hotel","61083");
// 2.发送请求,得到响应
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 3. 解析响应结果
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); // 反序列化
System.out.println(hotelDoc);
}
删除
@Test
void deleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest("hotel","61083");
client.delete(request,RequestOptions.DEFAULT);
}
修改
@Test
void updateDoc() throws IOException {
UpdateRequest request = new UpdateRequest("hotel","61083");
request.doc(
"price","952",
"starName","四钻"
);
client.update(request,RequestOptions.DEFAULT);
}
批量导入
@Test
void bulkDoc() throws IOException { // 批量导入
// 批量查询酒店数据
List<Hotel> hotels = hotelService.list();
// 1.创建Request
BulkRequest request = new BulkRequest();
// 2.准备参数,添加多个新增的Request
for (Hotel hotel : hotels){
// 转换为文档类型HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
// 创建新增文档的Request对象
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
// 3.发送请求
client.bulk(request,RequestOptions.DEFAULT);
}