[原创]springboot整合elasticsearch全文检索入门

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: [原创]springboot整合elasticsearch全文检索入门

# 安装


下载elasticsearch与kibana  https://www.elastic.co/start


# 依赖

springBootVersion = '2.0.5.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch'
 //请与spring-boot-starter-data-elasticsearch的jar包版本一致
compile('org.elasticsearch.client:transport:5.6.11')


springBoot 2.0.5.RELEASE 起步依赖的elasticsearch的版本是 5.6.11


image.png

# 配置


  1. 可在application.yml中配置

spring:
  data:
    # 全文检索 elasticsearch
    elasticsearch:
      cluster-name: elasticsearch #节点名称
      cluster-nodes: 127.0.0.1:9300 #节点地址
      repositories:
        enabled: true
  1. 也可以通过java代码进行配置

package com.futao.springmvcdemo.foundation.configuration
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.transport.client.PreBuiltTransportClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories
import java.net.InetAddress
/**
 * @author futao
 * Created on 2018/10/22.
 * ElasticSearch全文检索配置类
 * 可替代配置文件中的配置
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = ["com.futao.springmvcdemo.dao"])
open class ElasticSearchConfiguration {
    @Bean
    open fun client(): TransportClient {
        val node = InetSocketTransportAddress(
                InetAddress.getByName("127.0.0.1"), 9300)
        val settings = Settings.builder()
                .put("cluster.name", "springboot-elasticsearch")    //集群名称可以在\elasticsearch\config\elasticsearch.yml中配置
                .build()
        return PreBuiltTransportClient(settings).addTransportAddress(node)
    }
}


# 名词解释


elasticsearch中的名词与mysql中的名字对比


image.png

# 使用


个人理解:相当于mysql的建表,程序跑起来之后会建立相应的index与type,后续程序中就可以使用该类型的index与type进行crud

package com.futao.springmvcdemo.model.entity;
import org.springframework.data.elasticsearch.annotations.Document;
/**
 * @author futao
 * Created on 2018/10/20.
 * 文章
 * indexName=database
 * type=table
 * row=document
 * colnum=field
 */
@Document(indexName = "futao", type = "article")
public class Article extends BaseEntity {
    /**
     * 标题
     */
    private String title;
    /**
     * 简介
     */
    private String description;
    /**
     * 内容
     */
    private String content;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

# 插入数据


  • Dao层

package com.futao.springmvcdemo.dao.impl
import com.futao.springmvcdemo.model.entity.Article
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
/**
 * @author futao
 * Created on 2018/10/22.
 */
interface ArticleSearchDao : ElasticsearchRepository<Article, String> {
}
  • Service层

package com.futao.springmvcdemo.service.impl
import com.alibaba.fastjson.JSONObject
import com.futao.springmvcdemo.dao.ArticleDao
import com.futao.springmvcdemo.dao.impl.ArticleSearchDao
import com.futao.springmvcdemo.foundation.LogicException
import com.futao.springmvcdemo.model.entity.Article
import com.futao.springmvcdemo.model.entity.constvar.ErrorMessage
import com.futao.springmvcdemo.service.ArticleService
import com.futao.springmvcdemo.utils.currentTimeStamp
import com.futao.springmvcdemo.utils.getFieldName
import com.futao.springmvcdemo.utils.uuid
import org.elasticsearch.client.Client
import org.elasticsearch.index.query.QueryBuilders
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Service
import javax.annotation.Resource
/**
 * @author futao
 * Created on 2018/10/20.
 */
@Service
open class ArticleServiceImpl : ArticleService {
    @Resource
    private lateinit var elasticsearch: ArticleSearchDao
    @Resource
    private lateinit var client: Client
    override fun list(): List<Article> {
        val list = articleDao.list()
        elasticsearch.saveAll(list)
        return list
    }
/**
     * 全文检索
     * 全文索引会将输入的字符串根据语法(分词器)拆解开来,然后再到倒排索引去一一匹配,只要匹配到拆解之后的任意一个单词就可以返回该Document
     * 短语搜索phrase search要求输入的字符串必须匹配,不进行分词
     */
    override fun search(key: String, fromRange: Int, toRange: Int, size: Int, from: Int): ArrayList<Article> {
        val hits = elastic
                //查询的Index
                .prepareSearch(Article.ES_INDEX_NAME)
                //查询的Type
                .setTypes(Article.ES_TYPE)
                //关键字匹配搜索
                .setQuery(
                        QueryBuilders
                                .boolQuery()
                                .should(QueryBuilders.matchQuery(Article::getContent.getFieldName(), key).boost(1f))//权重1f
                                .should(QueryBuilders.matchQuery(Article::getTitle.getFieldName(), key).boost(2f))
                                .should(QueryBuilders.matchQuery(Article::getDescription.getFieldName(), key).boost(4f))
                )
                //范围搜索
                .setQuery(QueryBuilders.rangeQuery(Article::getVisitTimes.getFieldName()).from(fromRange).to(toRange))
                //高亮
                .highlighter(HighlightBuilder()
                        .highlightFilter(true)
                        .preTags("<em>")
                        .postTags("</em>")
                        .field(Article::getTitle.getFieldName())
                        .field(Article::getContent.getFieldName())
                )
                //Filter
//                .setPostFilter(QueryBuilders.boolQuery())
                //结果排序
                .addSort(Article::getCreateTime.getFieldName(), SortOrder.DESC)
                //分页开始
                .setFrom(from)
                //分页大小
                .setSize(size)
                .execute()
                .actionGet()
//                .get()
//                =.execute()
//                .actionGet()
                //TODO("分组查询")某个标签下的数量
                //TODO("平均价格")
                .hits
        val list: ArrayList<Article> = arrayListOf()
        //总数据量
        println("getTotalHits========" + hits.getTotalHits())
        hits.forEach { it ->
            run {
                val article = JSONObject.parseObject(it.sourceAsString, Article::class.java)
                article.title = it.highlightFields[Article::getTitle.getFieldName()]!!.fragments()[0].toString()
                article.content = it.highlightFields[Article::getContent.getFieldName()]!!.fragments()[0].toString()
                list.add(article)
            }
        }
        return list
    }
}
  • controller层

package com.futao.springmvcdemo.controller.business;
import com.futao.springmvcdemo.model.entity.Article;
import com.futao.springmvcdemo.model.entity.SingleValueResult;
import com.futao.springmvcdemo.service.ArticleService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
 * @author futao
 * Created on 2018/10/20.
 */
@RestController
@RequestMapping(path = "article", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ArticleController {
    @Resource
    private ArticleService articleService;
    /**
     * 新增文章
     *
     * @param title
     * @param desc
     * @param content
     * @return
     */
    @PostMapping(path = "add")
    public SingleValueResult add(
            @RequestParam("title") String title,
            @RequestParam("desc") String desc,
            @RequestParam("content") String content
    ) {
        articleService.add(title, desc, content);
        return new SingleValueResult("success");
    }
    /**
     * 文章列表
     *
     * @return
     */
    @GetMapping("list")
    public List<Article> list() {
        return articleService.list();
    }
    /**
     * 全文检索
     *
     * @param key
     * @return
     */
    @GetMapping("search")
    public List<Article> search(@RequestParam("key") String key) {
        return articleService.search(key);
    }
}
  • 在启动项目之前如果程序有抛出java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]异常,则需要在启动类中添加:

package com.futao.springmvcdemo;
import com.alibaba.fastjson.parser.ParserConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
 * @author futao
 * ServletComponentScan 开启servlet和filter
 */
@SpringBootApplication
@ServletComponentScan
@MapperScan("com.futao.springmvcdemo.dao")
@EnableCaching
//@EnableAspectJAutoProxy
@EnableElasticsearchRepositories(basePackages = "com.futao.springmvcdemo")
public class SpringmvcdemoApplication {
    public static void main(String[] args) {
        /**
         * 添加elasticsearch之后发生异常的解决方案
         * Springboot整合Elasticsearch 在项目启动前设置一下的属性,防止报错
         * 解决netty冲突后初始化client时会抛出异常
         * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
         */
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        SpringApplication.run(SpringmvcdemoApplication.class, args);
        /**
         * redis反序列化
         * 开启fastjson反序列化的autoType
         */
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }
}

# 测试


  • 启动项目,可以在health中查看到相关的健康状况


image.png

list接口请求(把数据放入elasticsearch中)


image.png

现在可以在kibana中查看到上面存入的数据


image.png

也可以进行简单的搜索测试


image.png

调用search接口测试


image.png

elasticsearch数据的存放位置(删除该文件夹下的数据即删除了所有的索引)


image.png

多的不说了,跟之前项目中用过的Hibernate Search很像,不过elasticsearch也是在架构层面实现的全文索引,elasticsearch可以部署在其他服务器上,减轻主服务器的压力,并通过http restful api的形式与主程序进行协调工作。elasticsearch一般通过集群方式进行部署,横向扩展非常简单,甚至只需要改几行配置就行。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
存储 搜索推荐 Java
|
2月前
|
Java
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
|
27天前
|
Java 测试技术 Maven
SpringBoot集成Elasticsearch
SpringBoot集成Elasticsearch
22 0
|
1月前
|
消息中间件 Java 关系型数据库
【二十】springboot整合ElasticSearch实战(万字篇)
【二十】springboot整合ElasticSearch实战(万字篇)
209 47
|
2月前
|
存储 关系型数据库 MySQL
ElasticSearch 入门
【2月更文挑战第7天】ElasticSearch 入门 简介 ElasticSearch 的基本概念 ElasticSearch 的查询流程 ElasticSearch 的更新流程
37 2
|
2月前
|
存储 自然语言处理 搜索推荐
ElasticSearch入门篇
ElasticSearch入门篇
|
2月前
|
Java Windows
【极光系列】springBoot集成elasticsearch
【极光系列】springBoot集成elasticsearch
|
2月前
|
Java Docker 容器
springboot整合后台框架(三)整合elasticsearch
springboot整合后台框架(三)整合elasticsearch
34 0
|
Java 应用服务中间件 Maven
传统maven项目和现在spring boot项目的区别
Spring Boot:传统 Web 项目与采用 Spring Boot 项目区别
355 0
传统maven项目和现在spring boot项目的区别
|
XML Java 数据库连接
创建springboot项目的基本流程——以宠物类别为例
创建springboot项目的基本流程——以宠物类别为例
127 0
创建springboot项目的基本流程——以宠物类别为例