0.安装 es
es版本:elasticsearch-7.17.5
下载:https://www.elastic.co/cn/downloads/elasticsearch
参考:http://www.javacui.com/tool/669.html
1. pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.application.properties
spring.elasticsearch.rest.uris=http://localhost:9200
3.ElasticsearchConfig
这一步是装配 ElasticsearchRestTemplate(提供更灵活的查询能力) ,不用可以略过
/**
* @Classname ElasticsearchConfig
* @Date 2022/6/30 13:22
* @Created by jiww
*/
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchRestTemplate elasticsearchRestTemplate(){
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
4.使用 ArticleRepository 和 ElasticsearchRestTemplate API
* 有2种方式使用es CRUD
* 1.ArticleRepository
* 2.ElasticsearchRestTemplate
*
* articleRepository 类似 JPA 较为方便 可以直接通过方法名来生成sql
* elasticsearchRestTemplate 较为灵活 可以实现复杂的查询
package com.example.my_learn_es;
import com.example.my_learn_es.bean.ArticleEntity;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@SpringBootTest
class MyLearnEsApplicationTests {
/**
* 有2种方式使用es CRUD
* 1.ArticleRepository
* 2.ElasticsearchRestTemplate
*
* articleRepository 类似 JPA 较为方便 可以直接通过方法名来生成sql
* elasticsearchRestTemplate 较为灵活 可以实现复杂的查询
*/
@Autowired
ArticleRepository articleRepository;
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
void testElasticsearchRestTemplate2() {
/**
* 使用 elasticsearchRestTemplate 查询
*/
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
NativeSearchQuery query = builder
// 查询 id = eaac341c-ef9f-4ca4-8a71-4c43513fb9e5 的记录
.withFilter(new QueryStringQueryBuilder("eaac341c-ef9f-4ca4-8a71-4c43513fb9e5").field("id"))
// 查询 content 有 jjj 的记录
// .withFilter(new QueryStringQueryBuilder("jjj").field("title"))
// .withHighlightFields(new HighlightBuilder.Field("title"))
.build();
SearchHits<ArticleEntity> search = elasticsearchRestTemplate.search(query, ArticleEntity.class);
if (search.hasSearchHits()) {
search.getSearchHits().stream().forEach(s -> {
System.out.println(s.getContent());
});
}
}
@Test
void testElasticsearchRestTemplate() {
ArticleEntity articleEntity = new ArticleEntity();
String id = UUID.randomUUID().toString();
articleEntity.setId(id);
articleEntity.setTitle("jjj-title");
articleEntity.setContent("jjj-contentcontentcontentcontentcontentcontentcontentcontentcontentcontent");
articleEntity.setUserId(65311135);
articleEntity.setCreateTime(new Date());
elasticsearchRestTemplate.save(articleEntity);
ArticleEntity articleEntity1 = elasticsearchRestTemplate.get(id, ArticleEntity.class);
System.out.println("=========");
System.out.println("es:" + articleEntity1.toString());
System.out.println("=========");
System.out.println("delete it");
elasticsearchRestTemplate.delete(id, ArticleEntity.class);
ArticleEntity articleEntity2 = elasticsearchRestTemplate.get(id, ArticleEntity.class);
System.out.println("=========");
System.out.println(articleEntity2);
System.out.println("=========");
}
@Test
void testArticleRepository() {
ArticleEntity articleEntity = new ArticleEntity();
String id = UUID.randomUUID().toString();
articleEntity.setId(id);
articleEntity.setTitle("jjj-title");
articleEntity.setContent("jjj-contentcontentcontentcontentcontentcontentcontentcontentcontentcontent");
articleEntity.setUserId(65311135);
articleEntity.setCreateTime(new Date());
System.out.println("save id");
articleRepository.save(articleEntity);
Optional<ArticleEntity> byId = articleRepository.findById(id);
System.out.println("=========");
System.out.println("es:" + byId.get().toString());
System.out.println("=========");
articleRepository.deleteById(id);
System.out.println("delete id");
System.out.println("=========");
Optional<ArticleEntity> byId2 = articleRepository.findById(id);
if (byId2.isPresent()) {
System.out.println("delete fail");
}
System.out.println("=========");
}
@Test
void testArticleRepository2() {
/**
* 类似 JPA 的通过方法名search
*/
List<ArticleEntity> articleEntities = articleRepository.findByContentContains("jjj");
System.out.println("=========");
articleEntities.stream().forEach(System.out::println);
System.out.println("=========");
/**
* 类型 JPA 的通过sql直接搜索
*/
List<ArticleEntity> articleEntities2 = articleRepository.queryContent("jjj");
System.out.println("=========");
articleEntities2.stream().forEach(System.out::println);
System.out.println("=========");
}
}