[原創(chuàng)]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'
 //請(qǐng)與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 #節(jié)點(diǎn)名稱
      cluster-nodes: 127.0.0.1:9300 #節(jié)點(diǎn)地址
      repositories:
        enabled: true
  1. 也可以通過java代碼進(jìn)行配置
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中的名字對(duì)比


image.png

# 使用

個(gè)人理解:相當(dāng)于mysql的建表,程序跑起來之后會(huì)建立相應(yīng)的index與type,后續(xù)程序中就可以使用該類型的index與type進(jìn)行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 {
    /**
     * 標(biāo)題
     */
    private String title;
    /**
     * 簡介
     */
    private String description;
    /**
     * 內(nèi)容
     */
    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;
    }
}

# 插入數(shù)據(jù)

  • 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
    }
/**
     * 全文檢索
     * 全文索引會(huì)將輸入的字符串根據(jù)語法(分詞器)拆解開來,然后再到倒排索引去一一匹配,只要匹配到拆解之后的任意一個(gè)單詞就可以返回該Document
     * 短語搜索phrase search要求輸入的字符串必須匹配,不進(jìn)行分詞
     */
    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)
                //關(guān)鍵字匹配搜索
                .setQuery(
                        QueryBuilders
                                .boolQuery()
                                .should(QueryBuilders.matchQuery(Article::getContent.getFieldName(), key).boost(1f))//權(quán)重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())
                //結(jié)果排序
                .addSort(Article::getCreateTime.getFieldName(), SortOrder.DESC)
                //分頁開始
                .setFrom(from)
                //分頁大小
                .setSize(size)
                .execute()
                .actionGet()
//                .get()
//                =.execute()
//                .actionGet()

                //TODO("分組查詢")某個(gè)標(biāo)簽下的數(shù)量
                //TODO("平均價(jià)格")
                .hits
        val list: ArrayList<Article> = arrayListOf()
        //總數(shù)據(jù)量
        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);
    }
}
  • 在啟動(dòng)項(xiàng)目之前如果程序有拋出java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]異常,則需要在啟動(dòng)類中添加:
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之后發(fā)生異常的解決方案
         * Springboot整合Elasticsearch 在項(xiàng)目啟動(dòng)前設(shè)置一下的屬性,防止報(bào)錯(cuò)
         * 解決netty沖突后初始化client時(shí)會(huì)拋出異常
         * 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);
    }
}

# 測(cè)試

  • 啟動(dòng)項(xiàng)目,可以在health中查看到相關(guān)的健康狀況


    elasticsearch健康狀況
  • list接口請(qǐng)求(把數(shù)據(jù)放入elasticsearch中)


    將數(shù)據(jù)放入elasticsearch中
  • 現(xiàn)在可以在kibana中查看到上面存入的數(shù)據(jù)


    kibana
  • 也可以進(jìn)行簡單的搜索測(cè)試


    test
  • 調(diào)用search接口測(cè)試


    search

elasticsearch數(shù)據(jù)的存放位置(刪除該文件夾下的數(shù)據(jù)即刪除了所有的索引)


data存放地址

多的不說了,跟之前項(xiàng)目中用過的Hibernate Search很像,不過elasticsearch也是在架構(gòu)層面實(shí)現(xiàn)的全文索引,elasticsearch可以部署在其他服務(wù)器上,減輕主服務(wù)器的壓力,并通過http restful api的形式與主程序進(jìn)行協(xié)調(diào)工作。elasticsearch一般通過集群方式進(jìn)行部署,橫向擴(kuò)展非常簡單,甚至只需要改幾行配置就行。

我在這里等你:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容