登录
首页 >  数据库 >  MySQL

Elastic Search搜索引擎在SpringBoot中的实践

来源:SegmentFault

时间:2023-01-12 17:18:46 475浏览 收藏

对于一个数据库开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《Elastic Search搜索引擎在SpringBoot中的实践》,主要介绍了MySQL、算法、Java、Linux、机器学习,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

实验环境

  • ES版本:5.3.0
  • spring bt版本:1.5.9

首先当然需要安装好elastic search环境,最好再安装上可视化插件 elasticsearch-head来便于我们直观地查看数据。


Spring工程创建

这部分没有特殊要交代的,但有几个注意点一定要当心

  • 注意在新建项目时记得勾选web和NoSQL中的Elasticsearch依赖,来张图说明一下吧:

创建工程时勾选Nosql中的es依赖选项

项目自动生成以后pom.xml中会自动添加

        org.springframework.bootspring-boot-starter-data-elasticsearch1234
  • 本项目中我们使用开源的基于restful的es java客户端
            io.searchboxjest1234
    • 除此之外还必须添加
              net.java.dev.jnajna1234

      否则启动spring项目的时候会报

      server:
        port: 6325
      
      spring:
        elasticsearch:
          jest:
            uris:
            - http://113.209.119.170:9200  # ES服务器的地址!
            read-timeout: 5000123456789

      代码组织

      我的项目代码组织如下:

      项目代码组织

      各部分代码详解如下,注释都有:

      • Entity.java

      package com.hansonwang99.springboot_es_demo.entity;
      import java.io.Serializable;
      import org.springframework.data.elasticsearch.annotations.Document;
      
      public class Entity implements Serializable{
      
          private static final long serialVersionUID = -763638353551774166L;
      
          public static final String INDEX_NAME = "index_entity";
      
          public static final String TYPE = "tstype";
      
          private Long id;
      
          private String name;
      
          public Entity() {
              super();
          }
      
          public Entity(Long id, String name) {
              this.id = id;
              this.name = name;
          }
      
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
      
      }
      1234567891011121314151617181920212223242526272829303132333435363738394041424344
      • TestService.java

      package com.hansonwang99.springboot_es_demo.service;
      
      import com.hansonwang99.springboot_es_demo.entity.Entity;
      
      import java.util.List;
      
      public interface TestService {
      
          void saveEntity(Entity entity);
      
          void saveEntity(List entityList);
      
          List searchEntity(String searchContent);
      }1234567891011121314
      • TestServiceImpl.java

      package com.hansonwang99.springboot_es_demo.service.impl;
      
      import java.io.IOException;
      import java.util.List;
      
      import com.hansonwang99.springboot_es_demo.entity.Entity;
      import com.hansonwang99.springboot_es_demo.service.TestService;
      import org.elasticsearch.index.query.QueryBuilders;
      import org.elasticsearch.search.builder.SearchSourceBuilder;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import io.searchbox.client.JestClient;
      import io.searchbox.client.JestResult;
      import io.searchbox.core.Bulk;
      import io.searchbox.core.Index;
      import io.searchbox.core.Search;
      
      @Service
      public class TestServiceImpl implements TestService {
      
          private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImpl.class);
      
          @Autowired
          private JestClient jestClient;
      
          @Override
          public void saveEntity(Entity entity) {
              Index index = new Index.Builder(entity).index(Entity.INDEX_NAME).type(Entity.TYPE).build();
              try {
                  jestClient.execute(index);
                  LOGGER.info("ES 插入完成");
              } catch (IOException e) {
                  e.printStackTrace();
                  LOGGER.error(e.getMessage());
              }
          }
      
      
          /**
           * 批量保存内容到ES
           */
          @Override
          public void saveEntity(List entityList) {
              Bulk.Builder bulk = new Bulk.Builder();
              for(Entity entity : entityList) {
                  Index index = new Index.Builder(entity).index(Entity.INDEX_NAME).type(Entity.TYPE).build();
                  bulk.addAction(index);
              }
              try {
                  jestClient.execute(bulk.build());
                  LOGGER.info("ES 插入完成");
              } catch (IOException e) {
                  e.printStackTrace();
                  LOGGER.error(e.getMessage());
              }
          }
      
          /**
           * 在ES中搜索内容
           */
          @Override
          public List searchEntity(String searchContent){
              SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
              //searchSourceBuilder.query(QueryBuilders.queryStringQuery(searchContent));
              //searchSourceBuilder.field("name");
              searchSourceBuilder.query(QueryBuilders.matchQuery("name",searchContent));
              Search search = new Search.Builder(searchSourceBuilder.toString())
                      .addIndex(Entity.INDEX_NAME).addType(Entity.TYPE).build();
              try {
                  JestResult result = jestClient.execute(search);
                  return result.getSourceAsObjectList(Entity.class);
              } catch (IOException e) {
                  LOGGER.error(e.getMessage());
                  e.printStackTrace();
              }
              return null;
          }
      }
      12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
      • EntityController.java

      package com.hansonwang99.springboot_es_demo.controller;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import com.hansonwang99.springboot_es_demo.entity.Entity;
      import com.hansonwang99.springboot_es_demo.service.TestService;
      import org.apache.commons.lang.StringUtils;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      @RequestMapping("/entityController")
      public class EntityController {
      
      
          @Autowired
          TestService cityESService;
      
          @RequestMapping(value="/save", method=RequestMethod.GET)
          public String save(long id, String name) {
              System.out.println("save 接口");
              if(id>0 && StringUtils.isNotEmpty(name)) {
                  Entity newEntity = new Entity(id,name);
                  List addList = new ArrayList();
                  addList.add(newEntity);
                  cityESService.saveEntity(addList);
                  return "OK";
              }else {
                  return "Bad input value";
              }
          }
      
          @RequestMapping(value="/search", method=RequestMethod.GET)
          public List save(String name) {
              List entityList = null;
              if(StringUtils.isNotEmpty(name)) {
                  entityList = cityESService.searchEntity(name);
              }
              return entityList;
          }
      }1234567891011121314151617181920212223242526272829303132333435363738394041424344

      实际实验

      增加几条数据,可以使用postman工具,也可以直接在浏览器中输入,如增加以下5条数据:

      http://localhost:6325/entityController/save?id=1&name=南京中山陵
      http://localhost:6325/entityController/save?id=2&name=中国南京师范大学
      http://localhost:6325/entityController/save?id=3&name=南京夫子庙
      http://localhost:6325/entityController/save?id=4&name=杭州也非常不错
      http://localhost:6325/entityController/save?id=5&name=中国南边好像没有叫带京字的城市了12345

      数据插入效果如下(使用可视化插件elasticsearch-head观看):

      数据插入效果

      我们来做一下搜索的测试:例如我要搜索关键字“南京”
      我们在浏览器中输入:

      http://localhost:6325/entityController/search?name=南京1

      搜索结果如下:

      关键字“南京”的搜索结果

      刚才插入的5条记录中包含关键字“南京”的四条记录均被搜索出来了!

      当然这里用的是standard分词方式,将每个中文都作为了一个term,凡是包含“南”、“京”关键字的记录都被搜索了出来,只是评分不同而已,当然还有其他的一些分词方式,此时需要其他分词插件的支持,此处暂不涉及,后文中再做探索。

      如需要跟多资料请点击下方图片⬇(扫码加好友→备注66,不备注拒绝添加哦)

      image

      终于介绍完啦!小伙伴们,这篇关于《Elastic Search搜索引擎在SpringBoot中的实践》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表