使用ElasticSearch已一年有余,这过程中的使用心得在此记录下来与大家分享,共同进步。
什么是ElasticSearch
某百科对ElasticSearch有如下描述:
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
我们可以简单抽象出ElasticSearch的能力。
那什么是Lucene呢?
Lucene是一个开放源代码的全文检索引擎工具包。其实就是一个(多个)jar包,我们可以将其引入到自己的java项目中来实现全文检索,而ElasticSearch就是基于Lucene开发而来的搜索引擎。
ElasticSearch中的基本概念
我们可以按照下图来对ElasticSearch做一个简单的认识。
现在ElasticSearch的最新版本以及到7.15,而这张图对应的关系应该是ElastciSearch 5.X以及6.X版本。这其中涉及到ElasticSearch对type设计的变更,在5.X版本中,一个index下可以有多个type存在,这和MySQL的库和表的对应关系非常相似;而在6.X版本中,一个index下只能有一个type存在,到了7.X版本则移除了type,那么与MySQL的对应关系则变成如下所示。
可以理解为ElasticSearch中没有库的概念,而是直接创建索引,索引下存储数据。
shards&replicas
shards(分片)的目的是将索引的数据可以均匀的分布在不同的数据节点上,而在查询的时候可以利用多台机器的计算能力,从而提高查询效率。
replicas(副本)则是对shards进行备份,防止数据丢失,通常副本可以设置1-2份,分片和副本会分开存储在不同的节点上,防止机器故障导致数据丢失。
ElasticSearch安装
-
ElasticSearch下载 下载地址 下载之后解压即可,进入解压目录,再进入bin目录,点击elasticsearch.bat运行即可
-
可视化界面安装 可视化界面的可选择比较多,官方提供的kibana,第三方的cerebro、head。
kibana功能比较多,也较为复杂。对新手不是很友好,head功能简单,但界面太low。而cerebro目前是比较好的选择。但博主已经习惯使用kibana,在此就使用kibana进行安装。选择与ElasticSearch对应的版本下载,同样下载好之后解压,运行bin/kibana.bat即可
注:安装仅是针对windows单机安装,并非完整集群模式安装。
操作命令
创建索引
创建索引使用PUT操作,如下命令创建了名称为first_index的索引,分片数量为3,副本数量为2。mapping可以理解为字段,我们创建了两个字段分别为name、age
PUT first_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"name": {
"type": "text",
"index": "true"
},
"age": {
"type": "integer",
"index": "true"
}
}
}
}
插入数据
POST first_index/_doc
{
"name":"zhangsan",
"age":15
}
查询数据
GET first_index/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "zhangsan"
}
}
}
}
}
删除数据
### 按照主键删除
DELETE 索引名称/_doc/数据id
DELETE first_index/_doc/WP0KEnwBpx7DePUf8yN6
### 按照条件删除
POST first_index/_doc/_delete_by_query
{
"query":{
"term":{
"name":"zhangsan"
}
}
}
更多的操作命令这里不再一一介绍,有兴趣可以留言或者官网研究一番。
项目中ElasticSearch的使用
目前Java项目常用的接入方式有以下三种:
- REST Client
- Jest
- Spring Data 本文以Spring Data形式作为演示,创建项目并添加依赖
<groupId>org.example</groupId>
<artifactId>elastic_search_test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.5.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
编写entity,字段与索引字段一一对应,id字段用于接收数据数据id
@Document(indexName = "first_index")
public class User {
@Id
private String id;
private String name;
private Integer age;
//getter and setter
}
编写repository类
@Repository
public interface UserRepository extends ElasticsearchRepository<User,String> {
}
配置文件
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
repositories:
enabled: true
测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class UserServiceTest {
@Autowired
private UserRepository repository;
@Test
public void selectUser(){
User user = repository.findById("Wv2ZFnwBpx7DePUf6SNU").get();
Assert.assertNotNull("查询数据为空",user);
Assert.assertTrue("年龄与预期不匹配",user.getAge() == 15);
Assert.assertTrue("姓名与预期不匹配","zhangsan".equals(user.getName()));
}
}
测试成功。到此,本篇文章分享结束。后续再深入探讨ElasticSearch!