美文网首页开源框架-SpringBoot系列
SpringBoot中如何使用Elasticsearch

SpringBoot中如何使用Elasticsearch

作者: singleZhang2010 | 来源:发表于2021-01-05 17:05 被阅读0次

概述

Elasticsearch是一个全文搜索中间件,,可以提供搜索服务,名词解释可以参考百度。

在SpringBoot中使用Elasticsearch

在springboot中主要借助spring-data-elasticsearch来操作Elasticsearch

参考:https://spring.io/projects/spring-data-elasticsearch,版本需要和ES版本对上
es下载:https://www.elastic.co/cn/downloads/past-releases
中文分词器ik:https://github.com/medcl/elasticsearch-analysis-ik

这里使用的springboot为2.1.x、spring-data-elasticsearch2.1.1、Elasticsearch为6.4
Elasticsearch的安装在本篇不做讲解,本文讲的是post文章搜索,所以前期准备需要先在Elasticsearch里创建post索引,并从post表中导入数据。如果是用容器启动ES的话,在文末给出docker下运行ES步骤。

1.在pom.xml中加入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId><!--netty-->
            <version>2.1.1.RELEASE</version>
        </dependency>
  1. 检索用的实体类PostDocument.java
@Document(indexName="post", type="post", createIndex=true)
public class PostDocument implements Serializable {

    @Id
    private Long id;

    // ik分词器
    @Field(type = FieldType.Text, searchAnalyzer="ik_smart", analyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.Long)
    private Long authorId;

    @Field(type = FieldType.Keyword)
    private String authorName;
    private String authorAvatar;

    private Long categoryId;
    @Field(type = FieldType.Keyword)
    private String categoryName;

    private Integer level;
    private Boolean isReply;

    private Integer commentCount;
    private Integer viewCount;

    @Field(type = FieldType.Date)
    private Date created;

  //...省略getter setter
}
  1. 创建一个Dao类PostRepository 继承ElasticsearchRepository
@Repository
public interface PostRepository extends ElasticsearchRepository<PostDocument, Long> {
    // 符合jpa命名规范的接口
}

3.创建一个检索服务类SearchService.java

@Service
public class SearchService {

    @Autowired
    PostRepository postRepository;

    public List<PostDocument> search(Integer pageNum,Integer pageSize, String keyword) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        
        //构建检索条件
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices("post")
                .withTypes("post")
                .withFields("title", "authorName", "categoryName")
                .withPageable(pageable)
                .build();
        org.springframework.data.domain.Page<PostDocument> documents = postRepository.search(searchQuery);
        return List<PostDocument> list = documents.getContent();
    }
}

※这里只写了搜索的功能,增量数据的同步更新通过canal中间件来同步具体参考之前的文章《使用阿里canal,实现如何将MySQL实时同步数据到ElasticSearch》,如果不使用canal中间件的话,也可以使用Logstash来同步数据,或者直接在代码中实现,这个需要写增删改的方法,通过RabbitMQ监听MySql的数据更新消息,来达到ElasticSearch的数据同步更新。

4.写一个控制器类IndexController来调用

@Controller
public class IndexController{
    @Autowired
    HttpServletRequest req;

    @Autowired
    SearchService searchService;
    @RequestMapping("/search")
    public String search(String q) {
        int pageNum= ServletRequestUtils.getIntParameter(req, "pageNum", 1);
        int pageSize = ServletRequestUtils.getIntParameter(req, "pageSize", 10);
        List<PostDocument> list = searchService.search(pageNum, pageSize ,q);

        req.setAttribute("q", q);
        req.setAttribute("list", list);
        return "search";
    }
}

ElasticSearchTemplate的使用

上面介绍了ElasticSearchRepository的使用,还可以使用ElasticSearchTemplate来完成查询

JPA中的save方法只适用于少量数据的写入,使用ElasticSearchRepository的bulkIndex方法可以插入大量数据,如百万级的数据可以在短时间内完成插入。

Elasticsearch集群健康检查

GET http://127.0.0.1:9200/_cluster/stats
{
    "cluster_name": "elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0
}

status 有三个状态值

  • green
    所有的主分片和副本分片都已分配,集群是 100% 可用
  • yellow
    所有的主分片已经分片了,但至少还有一个副本是缺失的。不会有数据丢失,所以搜索结果依然是完整的。
  • red
    至少一个主分片(以及它的全部副本)都在缺失中。这意味着你在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常

总结

以上就是在SpringBoos中简单实用ElasticSearch的demo,对于复杂的应用,以后可以在本文里改进更新。

※docker运行ElasticSearch
ElasticSearch版本为 6.4.3,使用 docker 运行,下面是所有步骤:

  1. 下载镜像:docker pull elasticsearch:6.4.3

  2. 运行容器:docker run -d -p 9200:9200 -p 9300:9300 --name elasticsearch-6.4.3 elasticsearch:6.4.3

  3. 进入容器:docker exec -it elasticsearch-6.4.3 /bin/bash

  4. 安装 ik 分词器:./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.3/elasticsearch-analysis-ik-6.4.3.zip

  5. 修改 es 配置文件:`vi ./config/elasticsearch.yml

cluster.name: "docker-cluster"
network.host: 0.0.0.0

# minimum_master_nodes need to be explicitly set when bound on a public IP
# set to 1 to allow single node clusters
# Details: https://github.com/elastic/elasticsearch/pull/17288
discovery.zen.minimum_master_nodes: 1

# just for elasticsearch-head plugin
http.cors.enabled: true
http.cors.allow-origin: "*"
  1. 退出容器:exit
  2. 停止容器:docker stop elasticsearch-6.4.3
  3. 启动容器:docker start elasticsearch-6.4.3

※采坑点

#查看运行中的容器
>docker ps
#查看日志
> docker logs -f elasticsearch

如果遇到发现jvm内存不足,则修改elasticsearch的虚拟机配置项jvm.options文件

#原先的设置
#-Xms2g  
#-Xmx2g
#修改成以下的设置
-Xms512m  
-Xmx512m 

ElasticSearch的社区比较活跃可以经常逛逛:https://elasticsearch.cn/topic/elasticsearch
Elasticsearch: 权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

相关文章

网友评论

    本文标题:SpringBoot中如何使用Elasticsearch

    本文链接:https://www.haomeiwen.com/subject/snkwoktx.html