美文网首页java学习springbootJava 杂谈Spring cloud
elasticsearch入门 springboot2集成ela

elasticsearch入门 springboot2集成ela

作者: 编程小石头666 | 来源:发表于2019-03-24 11:55 被阅读1次

    springboot整合elasticsearch常用的方式有以下三种

    • 1,Java API
      这种方式基于TCP和ES通信,官方已经明确表示在ES 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它,所以不提倡。
    • 2,REST Client
      上面的方式1是基于TCP和ES通信的(而且TransPort将来会被抛弃……),官方也给出了基于HTTP的客户端REST Client(推荐使用),官方给出来的REST Client有Java Low Level REST Client和Java Hight Level REST Client两个,前者兼容所有版本的ES,后者是基于前者开发出来的,只暴露了部分API,待完善
    • 3,spring-data-elasticsearch
      除了上述方式,Spring也提供了本身基于SpringData实现的一套方案spring-data-elasticsearch

    我们今天就来为大家讲解spring-data-elasticsearch这种方式来集成es。为什们推荐这种呢,因为这种方式spring为我们封装了常见的es操作。和使用jpa操作数据库一样方便。用过jpa的同学一定知道。

    • jpa只需要简单继承JpaRepository就可以实现对数据库表的crud操作
    public interface UserRepository extends JpaRepository<UserBean, Long> {}
    
    • spring-data-elasticsearch同样,只要继承ElasticsearchRepository就可以实现常见的es操作了。
    public interface UserESRepository extends ElasticsearchRepository<UserBean, Long> {}
    

    下面我们就来讲解下springboot2继承 spring-data-elasticsearch的具体步骤。

    springboot版本 Elasticsearch版本
    2.1.3.RELEASE 6.4.3

    一,首先是创建springboot项目

    image.png
    image.png image.png

    如上图箭头所指,springboot版本选2.1.3,然后添加web和elasticsearch仓库

    • 创建项目完成后,我们完整的pom.xml文件如下
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.qcl</groupId>
        <artifactId>es</artifactId>
        <version>0.0.1</version>
        <name>es</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    spring-boot-starter-data-elasticsearch:就是我们所需要集成的es。

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    

    二,下载elasticsearch本地版本

    这里下载本地elasticsearch,其实和我们下载本地mysql是一样的,你要用elasticsearch肯定要下载一个本地版本用来存储查询数据啊。
    下面来简单的讲解下elasticsearch版本的下载步骤

    • 1,到官网
      https://www.elastic.co/downloads/

      image.png
      选择箭头所指,点击download
      image.png
      选择你所对应的系统,这里要注意,虽然官方最新版本是6.6.2,我们springboot项目里使用的是6.4.3版本。这个没有关系的,官方版本是向下兼容的。
    • 2,下载成功后解压,并进入到config文件夹下


      image.png

      进入config文件夹后,找到elasticsearch.yml


      image.png
      然后用下面这个文件替换elasticsearch.yml里面的内容
    # ======================== Elasticsearch Configuration =========================
    #
    # NOTE: Elasticsearch comes with reasonable defaults for most settings.
    #       Before you set out to tweak and tune the configuration, make sure you
    #       understand what are you trying to accomplish and the consequences.
    #
    # The primary way of configuring a node is via this file. This template lists
    # the most important settings you may want to configure for a production cluster.
    #
    # Please consult the documentation for further information on configuration options:
    # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
    #
    # ---------------------------------- Cluster -----------------------------------
    #
    # Use a descriptive name for your cluster:
    #
    cluster.name: my-application
    #
    # ------------------------------------ Node ------------------------------------
    #
    # Use a descriptive name for the node:
    #
    node.name: node-1
    #
    # Add custom attributes to the node:
    #
    #node.attr.rack: r1
    #
    # ----------------------------------- Paths ------------------------------------
    #
    # Path to directory where to store the data (separate multiple locations by comma):
    #
    #path.data: /path/to/data
    #
    # Path to log files:
    #
    #path.logs: /path/to/logs
    #
    # ----------------------------------- Memory -----------------------------------
    #
    # Lock the memory on startup:
    #
    #bootstrap.memory_lock: true
    #
    # Make sure that the heap size is set to about half the memory available
    # on the system and that the owner of the process is allowed to use this
    # limit.
    #
    # Elasticsearch performs poorly when the system is swapping the memory.
    #
    # ---------------------------------- Network -----------------------------------
    #
    # Set the bind address to a specific IP (IPv4 or IPv6):
    #
    network.host: 0.0.0.0
    #
    # Set a custom port for HTTP:
    #
    http.port: 9200
    #
    # For more information, consult the network module documentation.
    #
    # --------------------------------- Discovery ----------------------------------
    #
    # Pass an initial list of hosts to perform discovery when new node is started:
    # The default list of hosts is ["127.0.0.1", "[::1]"]
    #
    #discovery.zen.ping.unicast.hosts: ["host1", "host2"]
    #
    # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
    #
    #discovery.zen.minimum_master_nodes: 
    #
    # For more information, consult the zen discovery module documentation.
    #
    # ---------------------------------- Gateway -----------------------------------
    #
    # Block initial recovery after a full cluster restart until N nodes are started:
    #
    #gateway.recover_after_nodes: 3
    #
    # For more information, consult the gateway module documentation.
    #
    # ---------------------------------- Various -----------------------------------
    #
    # Require explicit names when deleting indices:
    #
    #action.destructive_requires_name: true
    
    #qcl自己加的
    http.cors.enabled: true 
    http.cors.allow-origin: "*"
    node.master: true
    node.data: true
    

    这里的cluster.name: my-application就代表我们的es的名称叫my-application

    • 3,启动es
      进入到bin文件
      image.png
      image.png
      点击elasticsearch脚本,即可启动es,脚本运行完,在浏览器中输入http://localhost:9200/
      如果出现下面信息,就代表es启动成功。
      image.png

    三,配置es

    在创建的springboot项目下的application.yml做如下配置


    image.png
    #url相关配置,这里配置url的基本url
    server:
      port: 8080
    spring:
      ## Elasticsearch配置文件(必须)
      ## 该配置和Elasticsearch本地文件config下的elasticsearch.yml中的配置信息有关
      data:
        elasticsearch:
          cluster-name: my-application
          cluster-nodes: 127.0.0.1:9300
    

    四,添加数据到es,并实现搜索

    • 1,创建bean
      我们像jpa那样,创建es自己的bean,如下
    package com.qcl.es;
    
    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;
    
    
    /**
     * Created by qcl on 2018/7/10.
     * ES相关
     */
    @Document(indexName = "user", type = "docs", shards = 1, replicas = 0)
    public class UserES {
    
        //主键自增长
        @Id
        private Long id;//主键
    
        @Field(type = FieldType.Text, analyzer = "ik_max_word")
        private String userName;
        private String userPhone;
    
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserPhone() {
            return userPhone;
        }
    
        public void setUserPhone(String userPhone) {
            this.userPhone = userPhone;
        }
    
        @Override
        public String toString() {
            return "UserES{" +
                    "userId=" + id +
                    ", userName='" + userName + '\'' +
                    ", userPhone='" + userPhone + '\'' +
                    '}';
        }
    }
    
    • 2,创建操作数据的Repository
    package com.qcl.es;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    
    /**
     * Created by qcl on 2019-03-23
     * 微信:2501902696
     * desc:
     */
    public interface UserESRepository extends ElasticsearchRepository<UserES, Long> {}
    
    • 3,创建controller
    package com.qcl.es;
    
    import org.elasticsearch.index.query.QueryBuilders;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by qcl on 2019-03-23
     * 微信:2501902696
     * desc:
     */
    @RestController
    public class UserController {
    
        @Autowired
        private UserESRepository repositoryES;
    
        @GetMapping("/create")
        public String create(
                @RequestParam("id") Long id,
                @RequestParam("userName") String userName,
                @RequestParam("userPhone") String userPhone) {
            UserES userES = new UserES();
            userES.setId(id);
            userES.setUserName(userName);
            userES.setUserPhone(userPhone);
            return repositoryES.save(userES).toString();
        }
    
        private String names;
    
        @GetMapping("/get")
        public String get() {
            names = "";
            Iterable<UserES> userES = repositoryES.findAll();
            userES.forEach(userES1 -> {
                names += userES1.toString() + "\n";
            });
            return names;
        }
    
        private String searchs = "";
    
        @GetMapping("/search")
        public String search(@RequestParam("searchKey") String searchKey) {
            searchs = "";
            // 构建查询条件
            NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
            // 添加基本分词查询
            queryBuilder.withQuery(QueryBuilders.matchQuery("userName", searchKey));
            // 搜索,获取结果
            Page<UserES> items = repositoryES.search(queryBuilder.build());
            // 总条数
            long total = items.getTotalElements();
            searchs += "总共数据数:" + total + "\n";
            items.forEach(userES -> {
                searchs += userES.toString() + "\n";
            });
            return searchs;
        }
    }
    

    启动springboot项目


    image.png

    我们简单的实现了

    • 往es里插入数据
    • 查询所有数据
    • 根据搜索key,搜索信息

    验证

    • 插入一个userName='李四'&userPhone='272501902696'的数据
      http://localhost:8080/create?id=5&userName='李四'&userPhone='272501902696'

      image.png
    • 查询上面的数据是否插入成功,可以看到李四这条数据已经成功插入。


      image.png
    • 搜索 userName包含'四'的信息,可以看到,成功感搜索到一条


      image.png
    • 搜索 userName包含'石'的信息,可以看到,成功感搜索到4条


      image.png

      到此我们就实现了springboot集成es的功能。后面我们再做复杂搜索就基于这个基础上做对应的操作即可。

    如果你有springboot相关的问题可以加我微信交流
    2501902696(备注java)

    相关文章

      网友评论

        本文标题:elasticsearch入门 springboot2集成ela

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