Spring整合Elasticsearch

引入依赖

<!-- 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();
    }

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值