栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

elasticsearch入门 springboot2集成elasticsearch 实现全文搜索,图文讲解带源码

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

elasticsearch入门 springboot2集成elasticsearch 实现全文搜索,图文讲解带源码

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 {}
  • spring-data-elasticsearch同样,只要继承ElasticsearchRepository就可以实现常见的es操作了。
public interface UserESRepository extends ElasticsearchRepository {}
下面我们就来讲解下springboot2继承 spring-data-elasticsearch的具体步骤。
springboot版本 Elasticsearch版本
2.1.3.RELEASE 6.4.3
一,首先是创建springboot项目



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

  • 创建项目完成后,我们完整的pom.xml文件如下


    4.0.0
    
 org.springframework.boot
 spring-boot-starter-parent
 2.1.3.RELEASE
  
    
    com.qcl
    es
    0.0.1
    es
    Demo project for Spring Boot

    
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-data-elasticsearch
 
 
     org.springframework.boot
     spring-boot-starter-web
 

 
     org.springframework.boot
     spring-boot-starter-test
     test
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    


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


     org.springframework.boot
     spring-boot-starter-data-elasticsearch
 
二,下载elasticsearch本地版本

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

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

    选择箭头所指,点击download

    选择你所对应的系统,这里要注意,虽然官方最新版本是6.6.2,我们springboot项目里使用的是6.4.3版本。这个没有关系的,官方版本是向下兼容的。

  • 2,下载成功后解压,并进入到config文件夹下

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

    然后用下面这个文件替换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文件


    点击elasticsearch脚本,即可启动es,脚本运行完,在浏览器中输入http://localhost:9200/
    如果出现下面信息,就代表es启动成功。
三,配置es

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

#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;



@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;


public interface UserESRepository extends ElasticsearchRepository {}
  • 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;


@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 = 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 items = repositoryES.search(queryBuilder.build());
 // 总条数
 long total = items.getTotalElements();
 searchs += "总共数据数:" + total + "n";
 items.forEach(userES -> {
     searchs += userES.toString() + "n";
 });
 return searchs;
    }
}

启动springboot项目

我们简单的实现了

  • 往es里插入数据
  • 查询所有数据
  • 根据搜索key,搜索信息
验证
  • 插入一个userName=‘李四’&userPhone='272501902696’的数据
    http://localhost:8080/create?id=5&userName=‘李四’&userPhone=‘272501902696’

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

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

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

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

有任何关于编程的问题都可以留言或者私信我,我看到后会及时解答。

编程小石头,码农一枚,非著名全栈开发人员。分享自己的一些经验,学习心得,希望后来人少走弯路,少填坑。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号