1.搭建一个Springboot项目,导入如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
2.配置yml
elasticsearch:
uris: http://localhost:9200
#连接超时时间
connection-timeout: 20000ms
data:
elasticsearch:
repositories:
#是否持久化
enabled: true
3.准备一个domain实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
/** indexName索引名称 ,createIndex是否创建索引,默认为true,shards分片机制,replicas代表副本默认为1*/
@Document(indexName = "t_user",createIndex = true,shards = 5,replicas = 1)
public class User {
@Id()
private Long Id;
/** FieldType.Keyword代表不分词*/
@Field(type =FieldType.Keyword)
private String nickName;
/** FieldType.Text代表分词,这里可以指定分词器 analyzer*/
@Field(type = FieldType.Text)
private String passWord;
/** FieldType.Date, pattern="dd.MM.uuuu 时间格式uuuu代替yyyy*/
@Field(type=FieldType.Date, format= DateFormat.basic_date)
private String registerDate;
}
关于文档映射就不多做简绍了,有兴趣的朋友可以去看下官网Spring Data Elasticsearch - Reference Documentation
4.准备一个测试类
@SpringBootTest
class DemoApplicationTests {
@Test
public void contextLoads() {
}
5.开始操作ES
5.1添加和更新
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void contextLoads() {
LinkedList<User> list = new LinkedList<>();
list.add(new User(1L,"张三","12345","2022-03-26"));
list.add(new User(2L,"李四","321","2022-03-26"));
elasticsearchRestTemplate.save(list);
}
然后运行访问我们的可视化界面;http://localhost:9100/ ,没有的小伙伴需要去安装哦,添加成功
更新的话和添加的话区别不大,我们只需要去更新我们的参数就好了
5.2搜索单个用matchQuery
@Test
public void contextLoads() throws ParseException {
NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("nickName", "四")).build();
SearchHits<User> search = elasticsearchRestTemplate.search(query, User.class);
search.getSearchHits().forEach(System.out::println);
}
查询成功
查询多个就用matchAllQuery()就好了
5.3删除
@Test
public void contextLoads() throws ParseException {
elasticsearchRestTemplate.delete("1", IndexCoordinates.of("t_user"));
}
IndexCoordinates.of()里面填写我们的索引库,删除成功
6.分页,高亮,排序
@Test
public void contextLoads() throws ParseException {
NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("nickName", "李"))
// 分页加排序
.withPageable(PageRequest.of(0,5, Sort.Direction.DESC,"_id"))
// 高亮
.withHighlightFields(new HighlightBuilder.Field("nickName")).build();
SearchHits<User> search = elasticsearchRestTemplate.search(query, User.class);
search.getSearchHits().forEach(System.out::println);
}
效果;