前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >?SpringBoot 整合 Elasticsearch (超详细).md

?SpringBoot 整合 Elasticsearch (超详细).md

作者头像
用户7630333
发布2023-12-07 19:12:47
6760
发布2023-12-07 19:12:47
举报
文章被收录于专栏:look Javalook Java

SpringBoot 整合 Elasticsearch (超详细)

注意:

1、环境搭建

安装es

?Elasticsearch 6.4.3 下载链接

为了方便,环境使用Windows

配置

?解压后配置

  • 找到config目录的elasticsearch.yml
image-20221109152237692
image-20221109152237692

分词器

下图所示,解压后的分词器放在plugins目录下,ik目录需要自己创建

image-20221109154446235
image-20221109154446235

启动

  • ? 由于我是在Windows环境下,找到bin目录的elasticsearch.bat双击即可。
命令测试
  • ? 查看健康状态
    • curl -X GET “localhost:9200/_cat/health?v“
  • ? 查看所有节点
    • curl -X GET “localhost:9200/_cat/nodes?v“
  • ? 新建索引
    • curl -X PUT "localhost:9200/test"
  • ?? 查看索引
    • curl -X GET "localhost:9200/_cat/indices?v"
  • ? 删除索引
    • curl -X DELETE "localhost:9200/test"

2、整合 Es

依赖 & 配置

  • ? 我这里使用的是SpringBoot 2.1.5.RELEASE,根据实际情况选择版本。
代码语言:javascript
复制
		<!--elasticsearch-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
  • ? yaml配置
image-20221109233533596
image-20221109233533596
  • ? properties配置
代码语言:javascript
复制
spring.data.elasticsearch.cluster-name=community
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

启动项目:

  • ? 不出意外肯定会出意外
image-20221109234157966
image-20221109234157966
  • ? 这个问题是由于Es底层的问题,这里就不展开解释,会提供思路,自行了解

解决办法:

image-20221109234326083
image-20221109234326083

3、使用

SpringBoot 整合 Elasticsearch视频教程

实体类

  • ? 相信之前学过Jpa的同学,对于以下配置很熟悉。
代码语言:javascript
复制
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
// 指定Es 索引 类型 分片 备份
@Document(indexName = "discusspost", type = "_doc", shards = 6, replicas = 3)
@Data
public class DiscussPost implements Serializable {
    private static final long serialVersionUID = 114809849189593294L;

    // 标识主键
    @Id
    private Integer id;
	
    // 对应文档类型
    @Field(type = FieldType.Integer)
    private Integer userId;

    /**
     * type 类型
     * analyzer 存储解析器
     * searchAnalyzer 查询解析器
     */
    @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;
}

持久层

  • ? ElasticsearchRepository 里面有许多常用方法
代码语言:javascript
复制
@Repository
public interface DiscussPostRepository extends ElasticsearchRepository<DiscussPost, Integer> {
}

测试代码

  • ? 查询数据层的代码就不展示了,我会在代码中说明
  • ? 时间足够,建议把视频看完
代码语言:javascript
复制
/**
 * @author : look-word
 * 2022-11-03 18:56
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticSearchTest {

    @Resource
    private DiscussPostRepository discussPostRepository;

    @Resource
    private DiscussPostMapper discussPostMapper;

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    public void testInsert() {
        // 将查询的结果,同步到Es中
        discussPostRepository.save(discussPostMapper.selectDiscussPostById(241));
    }

    @Test
    public void testInsertAll() {
        // 批量导入 discussPostRepository.saveAll()
        discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(101, 0, 100));
    }

    /**
     * 测试更新
     */
    @Test
    public void testUpdate() {
        DiscussPost discussPost = discussPostMapper.selectDiscussPostById(241);
        discussPost.setContent("我爱中华人民共和国,我是中国人");
        discussPostRepository.save(discussPost);
    }

    /**
     * 测试修改
     */
    @Test
    public void testDelete() {
        discussPostRepository.deleteById(241);
    }

    /**
     * 测试查询
     */
    @Test
    public void testSelect() {
        // 构造查询条件
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
            	// (查询的值,查询字段1,查询字段1) 匹配title或者title里面是否含有互联网寒冬
                .withQuery(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "title"))
                .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) // 排序
                .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
                .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .withPageable(PageRequest.of(0, 10)) // 分页
                .withHighlightFields(
                        new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),
                        new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")
                ).build(); //高亮
        Page<DiscussPost> page = discussPostRepository.search(searchQuery);
        page.get().forEach(System.out::println);
    }

    /**
     * 测试查询高亮显示
     */
    @Test
    public void testSelectHighlight() {
        // 构造查询条件
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))
                .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) // 排序
                .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
                .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .withPageable(PageRequest.of(0, 10)) // 分页
                .withHighlightFields(
                        new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),
                        new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")
                ).build(); //高亮

        Page<DiscussPost> page = elasticsearchTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() {
            @Override
            public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {
                SearchHits hits = response.getHits();
                if (hits.getTotalHits() <= 0) {
                    return null;
                }
                List<DiscussPost> list = new ArrayList<>();
                for (SearchHit hit : hits) {
                    DiscussPost post = new DiscussPost();

                    String id = hit.getSourceAsMap().get("id").toString();
                    post.setId(Integer.parseInt(id));

                    String userId = hit.getSourceAsMap().get("userId").toString();
                    post.setUserId(Integer.parseInt(userId));

                    String title = hit.getSourceAsMap().get("title").toString();
                    post.setTitle(title);

                    String content = hit.getSourceAsMap().get("content").toString();
                    post.setContent(content);

                    String type = hit.getSourceAsMap().get("type").toString();
                    post.setType(Integer.parseInt(type));

                    String status = hit.getSourceAsMap().get("status").toString();
                    post.setStatus(Integer.parseInt(status));

                    String createTime = hit.getSourceAsMap().get("createTime").toString();
                    post.setCreateTime(new Date(Long.parseLong(createTime)));

                    String commentCount = hit.getSourceAsMap().get("commentCount").toString();
                    post.setCommentCount(Integer.parseInt(commentCount));

                    String score = hit.getSourceAsMap().get("score").toString();
                    post.setScore(Double.parseDouble(score));

                    // 处理高亮
                    HighlightField titleField = hit.getHighlightFields().get("title");
                    // 页面有多个高亮字 只显示第一个
                    if (titleField != null) {
                        post.setTitle(titleField.getFragments()[0].toString());
                    }
                    HighlightField contentField = hit.getHighlightFields().get("content");
                    if (contentField != null) {
                        post.setTitle(contentField.getFragments()[0].toString());
                    }
                    list.add(post);
                }
                return new AggregatedPageImpl(list, pageable, hits.getTotalHits(), response.getAggregations(), response.getScrollId(), hits.getMaxScore());
            }
        });
        page.get().forEach(System.out::println);
    }
}
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-11-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot 整合 Elasticsearch (超详细)
    • 注意:
      • 1、环境搭建
        • 安装es
        • 配置
        • 分词器
        • 启动
      • 2、整合 Es
        • 依赖 & 配置
        • 启动项目:
      • 3、使用
        • 实体类
        • 持久层
        • 测试代码
    相关产品与服务
    腾讯云服务器利旧
    云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    http://www.vxiaotou.com