1. 程式人生 > >springboot整合elasticsearch全文檢索入門

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.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
    }
 /**
     * 全文檢索
     */
    override fun search(key: String): ArrayList<Article> {
        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<Article> = 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;

/**
 * @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中檢視到相關的健康狀況 elasticsearch健康狀況
  • list介面請求(把資料放入elasticsearch中) 將資料放入elasticsearch中
  • 現在可以在kibana中檢視到上面存入的資料 kibana
  • 也可以進行簡單的搜尋測試 test
  • 呼叫search介面測試 search

elasticsearch資料的存放位置(刪除該資料夾下的資料即刪除了所有的索引) data存放地址

多的不說了,跟之前專案中用過的Hibernate Search很像,不過elasticsearch也是在架構層面實現的全文索引,elasticsearch可以部署在其他伺服器上,減輕主伺服器的壓力,並通過http restful api的形式與主程式進行協調工作。elasticsearch一般通過叢集方式進行部署,橫向擴充套件非常簡單,甚至只需要改幾行配置就行。