SpringBoot系列——ElasticSearch


  前言

  本文記錄安裝配置ES環境,在SpringBoot項目中使用SpringData-ElasticSearch對ES進行增刪改查通用操作

 

  ElasticSearch官網:https://www.elastic.co/cn/products/elasticsearch

  SpringData-ElasticSearch官網:https://spring.io/projects/spring-data-elasticsearch

 

  安裝配置ES環境

  過程參考這篇文章:https://blog.csdn.net/chen_2890/article/details/83757022

 

  下載ES

  鏈接:https://www.elastic.co/cn/downloads/elasticsearch

 

  選擇最新版下載即可,Elasticsearch無需安裝,解壓即用,直接雙擊 elasticsearch.bat 運行

  PS:下載下來后發現,最新版需要java jdk11,我的是jdk1.8,所有還是下載回6.X版本吧....

 

 

 

 

 

  安裝Head插件

  下載地址:https://github.com/mobz/elasticsearch-head

 

  PS:個人覺得,這個插件就類似PLSQL、Navicat工具,作用差不多

 

  注意:es5以上版本安裝head需要安裝node和grunt,執行 npm install -g grunt-cli 安裝grunt 

 

 

 

  

  第一步:進入Elasticsearch安裝目錄下的config目錄,修改elasticsearch.yml文件.在文件的末尾加入以下代碼

http.cors.enabled: true 
http.cors.allow-origin: "*"
node.master: true
node.data: true

  然后去掉network.host: 192.168.0.1的注釋並改為network.host: 0.0.0.0,去掉cluster.name;node.name;http.port的注釋(也就是去掉#)忽略所有注釋,最終的配置是

cluster.name: springboot-es
node.name: node-1
network.host: 0.0.0.0
http.port: 9200

http.cors.enabled: true 
http.cors.allow-origin: "*"
node.master: true
node.data: true

 

  第二步:雙擊elasticsearch.bat啟動Elasticsearch,瀏覽器訪問9200端口

 

 

 

  第三步:在https://github.com/mobz/elasticsearch-head中下載head插件,選擇下載zip

 

  第四步:解壓到指定文件夾下,修改Gruntfile.js 在對應的位置加上 hostname:'*'

  第五步:打開cmd命令行窗口 ,在解壓目錄下執行npm install 安裝,完成后執行grunt server 或者 npm run start 運行head插件,如果運行不成功建議重新安裝grunt

 

 

 

 

  打開瀏覽器訪問9100端口

 

 

 

 

  配置IK分詞器

  注意:你的Elasticsearch和IK分詞器必須版本統一

 

 

 


  GitHub地址:https://github.com/medcl/elasticsearch-analysis-ik

  下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

  解壓后把文件夾復制到ES的的plugins目錄下面,然后重啟ES

 

 

 

   使用postman測試分詞效果

  如果是ik分詞插件是6.x版本的,只能用postman測試,而且查詢條件要放在body體內,如果直接在url加上查詢條件會報錯

  http://localhost:9200/_analyze?analyzer=ik_max_word&text=我愛中華人民共和國

{
    "tokens": [
        {
            "token": "我",
            "start_offset": 0,
            "end_offset": 1,
            "type": "CN_CHAR",
            "position": 0
        },
        {
            "token": "愛",
            "start_offset": 1,
            "end_offset": 2,
            "type": "CN_CHAR",
            "position": 1
        },
        {
            "token": "中華人民共和國",
            "start_offset": 2,
            "end_offset": 9,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "中華人民",
            "start_offset": 2,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "中華",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "華人",
            "start_offset": 3,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "人民共和國",
            "start_offset": 4,
            "end_offset": 9,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "人民",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "共和國",
            "start_offset": 6,
            "end_offset": 9,
            "type": "CN_WORD",
            "position": 8
        },
        {
            "token": "共和",
            "start_offset": 6,
            "end_offset": 8,
            "type": "CN_WORD",
            "position": 9
        },
        {
            "token": "國",
            "start_offset": 8,
            "end_offset": 9,
            "type": "CN_CHAR",
            "position": 10
        }
    ]
}

 

  到這里,ES環境算是搭建成功了

  正常情況下,直接啟動ES就可以跑項目了,如果想要直觀的看到數據,就啟動head插件

 

  整合通用代碼

  工程結構

  與我們之前的通用JPA差不多,風格統一,也是直接單表基礎通用增刪改查,一人挖井,全村喝水

 

 

 

 

  創建索引、映射

  相當於數據庫、數據表

 

 

 

  save接口

  無id,新增

 

   有id,更新,可局部更新

 

 

 

 

  get接口

 

 

  delete接口

 

 

  list接口

  測試list、page接口前,先調用batchSave生成測試數據

 

 

  可支持排序跟多條件等值查詢

 

 

 

 

 

 

  page接口

  測試list、page接口前,先調用batchSave生成測試數據

 

 

  page與list相似,但多了分頁條件

 

 

 

 

 

  bug記錄 

  ik分詞我發現了一個bug,不知道是不是我沒設置對的原因,用title分詞去查詢的時候,小米手機單獨查是沒問題,小米手機查就查不到數據

 

 

 

 

 

 

 

  后記

 

  springboot整合springdata-es就到這里,還差一個高亮顯示設置了沒見有效果,后面再查一下資料,實在不行就查詢回來后收到設置高亮算了,這些后面再補充吧

 

 

 

  代碼開源

  代碼已經開源、托管到我的GitHub、碼雲:

  GitHub:https://github.com/huanzi-qch/springBoot

  碼雲:https://gitee.com/huanzi-qch/springBoot


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM