引入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>3.1.0</version>
</dependency>
配置Elasticsearch
# Elasticsearch
spring.data.elasticsearch.cluster-name=mycommunity
# http
#spring.data.elasticsearch.cluster-nodes=127.0.0.1:9200
# tcp
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
Spring Data Elasticsearch
netty 冲突
es与redis在底层都基于netty运行,如果不经过设置直接启动es会报错。解决方法如下:
@SpringBootApplication
@MapperScan({"com.nowcoder.mycommunity.dao"})
public class MyCommunityApplication {
// PostConstruct注解保证该函数在初始化之后马上执行
@PostConstruct
public void init(){
// fix the problem of netty startup conflicts
// see Netty4Utils.setAvailableProcessors
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
public static void main(String[] args) {
SpringApplication.run(MyCommunityApplication.class, args);
}
}
将待搜索的实体类与ES之间建立联系
// 在ES 4.0之前为这些参数
// 4.0之后的版本已经弃用
@Document(indexName = "discusspost"/*, type = "_doc", shards = 6, replicas = 3*/)
public class DiscussPost {
@Id
private int id;
@Field(type = FieldType.Integer)
private int userId;
// 标题和内容承担索引作用,需要添加分词的方法
// ik_max_word:
// ik_smart:
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String title;
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String content;
@Field(type = FieldType.Integer)
private int type;
@Field(type = FieldType.Integer)
private int status;
@Field(type = FieldType.Date)
private Date createTime;
@Field(type = FieldType.Integer)
private int commentCount;
@Field(type = FieldType.Double)
private double score;
// getter & setter
....
}
在dao创建
@Repository
public interface DiscussPostRepository extends ElasticsearchRepository<DiscussPost, Integer> {
}
测试将帖子加入ES并自动创建索引
一次插入一条数据
@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyCommunityApplication.class)
public class ElasticSearchTest {
@Autowired
private DiscussPostRepository discussPostRepository;
@Autowired
private DiscussPostMapper discussPostMapper;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
// 将ID为241,242,243的帖子加入到ES中
@Test
public void testInsert(){
discussPostRepository.save(discussPostMapper.selectDiscussPostById(241));
discussPostRepository.save(discussPostMapper.selectDiscussPostById(242));
discussPostRepository.save(discussPostMapper.selectDiscussPostById(243));
}
}
执行该测试程序,会自动创建discusspost索引:
241,242,243三篇文章也被加入到ES中了
一次插入多条数据
// 将用户101,102,103的前100篇文章全部插入
@Test
public void testInsertList(){
discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(101, 0, 100));
discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(102, 0, 100));
discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(103, 0, 100));
}
更改ES中的记录
// 更改ES中已存在的post
@Test
public void testUpdate(){
DiscussPost post = discussPostMapper.selectDiscussPostById(231);
post.setContent("Hello, world!");
discussPostRepository.save(post);
}
删除记录
@Test
public void testDelete(){
discussPostRepository.deleteById(231);
// 删除所有帖子
discussPostRepository.deleteAll();
}