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

springboot整合elasticsearch全文检索入门

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

springboot整合elasticsearch全文检索入门

# 依赖
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.configurationimport org.elasticsearch.client.transport.TransportClientimport org.elasticsearch.common.settings.Settingsimport org.elasticsearch.common.transport.InetSocketTransportAddressimport org.elasticsearch.transport.client.PreBuiltTransportClientimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.Configurationimport org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositoriesimport java.net.InetAddress@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")    //集群名称可以在elasticsearchconfigelasticsearch.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;@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.implimport com.futao.springmvcdemo.model.entity.Articleimport org.springframework.data.elasticsearch.repository.ElasticsearchRepositoryinterface ArticleSearchDao : ElasticsearchRepository {
}
  • Service层

package com.futao.springmvcdemo.service.implimport com.alibaba.fastjson.JSONObjectimport com.futao.springmvcdemo.dao.ArticleDaoimport com.futao.springmvcdemo.dao.impl.ArticleSearchDaoimport com.futao.springmvcdemo.foundation.LogicExceptionimport com.futao.springmvcdemo.model.entity.Articleimport com.futao.springmvcdemo.model.entity.constvar.ErrorMessageimport com.futao.springmvcdemo.service.ArticleServiceimport com.futao.springmvcdemo.utils.currentTimeStampimport com.futao.springmvcdemo.utils.getFieldNameimport com.futao.springmvcdemo.utils.uuidimport org.elasticsearch.client.Clientimport org.elasticsearch.index.query.QueryBuildersimport org.springframework.data.redis.core.RedisTemplateimport org.springframework.stereotype.Serviceimport javax.annotation.Resource@Serviceopen class ArticleServiceImpl : ArticleService {
    @Resource
    private lateinit var elasticsearch: ArticleSearchDao    @Resource
    private lateinit var client: Client    override fun list(): List {        val list = articleDao.list()
        elasticsearch.saveAll(list)        return list
    } 
    override fun search(key: String): ArrayList {        val hits = client.prepareSearch("futao")
                .setTypes("article")
                .setQuery(
                        QueryBuilders
                                .boolQuery()
                                .should(QueryBuilders.matchQuery(Article::getContent.getFieldName(), key))
                                .should(QueryBuilders.matchQuery(Article::getTitle.getFieldName(), key))
                                .should(QueryBuilders.matchQuery(Article::getDescription.getFieldName(), key))

                )
                .execute()
                .actionGet()
                .hits        val list: ArrayList = arrayListOf()
        hits.forEach { it -> list.add(JSONObject.parseObject(it.sourceAsString, Article::class.java)) }
        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;@RestController@RequestMapping(path = "article", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)public class ArticleController {    @Resource
    private ArticleService articleService;    
    @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");
    }    
    @GetMapping("list")    public List list() {        return articleService.list();
    }    
    @GetMapping("search")    public List 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;@SpringBootApplication@ServletComponentScan@MapperScan("com.futao.springmvcdemo.dao")@EnableCaching//@EnableAspectJAutoProxy@EnableElasticsearchRepositories(basePackages = "com.futao.springmvcdemo")public class SpringmvcdemoApplication {    public static void main(String[] args) {        
        System.setProperty("es.set.netty.runtime.available.processors", "false");

        SpringApplication.run(SpringmvcdemoApplication.class, args);        
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }
}
# 测试
  • 启动项目,可以在health中查看到相关的健康状况


    elasticsearch健康状况

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


    将数据放入elasticsearch中

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


    kibana

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


    test

  • 调用search接口测试


    search

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


data存放地址

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



作者:FutaoSmile丶
链接:https://www.jianshu.com/p/d8abebf7aa62


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

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

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