手把手教你SpringBoot整合Elasticsearch(ES)

野生D程式猿發表於2021-03-07

文末會附上完整的程式碼包供大家下載參考,碼字不易,如果對你有幫助請給個點贊和關注,謝謝!

如果只是想看java對於Elasticsearch的操作可以直接看第四大點

一、docker部署Elasticsearch(下面簡稱es)單機版教程

1、部署es

  • 拉取es映象(這裡我使用的版本是7.5.1)

    docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.1
    
  • 構建容器並啟動

    docker run -di --restart=always --name=es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" docker.elastic.co/elasticsearch/elasticsearch:7.5.1
    

    注意:es預設佔用2G記憶體,我這裡新增 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" 引數指定佔用記憶體

  • 此時如果你訪問 伺服器ip:9200會出現無法訪問的情況,這是因為還需要開啟es的跨域訪問

2、配置es允許跨域訪問

  • 進入es的容器

    docker exec -it es /bin/bash
    
  • 執行命令 vi /usr/share/elasticsearch/config/elasticsearch.yml 編輯配置檔案,在檔案末尾新增下面的配置然後 wq 儲存退出

    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
  • 執行 exit 命令退出容器,然後執行 docker restart es 命令重啟es,然後再訪問 伺服器ip:9200 ,出現下圖就表示單機版es搭建完成並可以遠端訪問了

注意:如果此時出現還是無法訪問的,稍等幾分鐘後再重新整理頁面就好了,因為es重啟是需要一些時間的

二、Elasticsearch(下面簡稱es)安裝ik分詞器教程

由於es自帶沒有中文分詞器,所以這裡新增大家用的比較多的ik分詞器(注意ik分詞器的版本必須和es版本一致)

  • 首先執行docker exec -it es bash進入es的容器

  • 然後執行cd /usr/share/elasticsearch/bin/進入bin目錄然後執行下面的命令線上安裝ik分詞器

    ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip
    
  • 執行cd ../plugins/進入plugins檢視ik分詞器是否成功安裝

三、Elasticsearch(下面簡稱es)安裝視覺化外掛head教程

  • 拉取elasticsearch-head映象

    docker pull mobz/elasticsearch-head:5
    
  • 建立並啟動容器

    docker run --restart=always --name elasticsearch-head -di -p 9100:9100 docker.io/mobz/elasticsearch-head:5
    
  • 訪問伺服器ip:9100,然後輸入伺服器ip:9200,點選“連線”按鈕

四、SpringBoot2.x整合Elasticsearch(下面簡稱es)教程

1、老規矩,先在pom.xml中新增es的依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2、在application.yml中新增es的配置

#elasticsearch配置
elasticsearch:
  rest:
    #es節點地址,叢集則用逗號隔開
    uris: 10.24.56.154:9200

3、新增es的工具類ElasticSearchUtils.java,工具類中我只新增了常用的一些方法,大家可以根據需要自行完善

package com.example.study.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ElasticSearch工具類
 *
 * @author 154594742@qq.com
 * @date 2021/3/4 19:34
 */

@Slf4j
@Component
public class ElasticSearchUtils {

    @Value("${spring.elasticsearch.rest.uris}")
    private String uris;

    private RestHighLevelClient restHighLevelClient;

    /**
     * 在Servlet容器初始化前執行
     */
    @PostConstruct
    private void init() {
        try {
            if (restHighLevelClient != null) {
                restHighLevelClient.close();
            }
            if (StringUtils.isBlank(uris)) {
                log.error("spring.elasticsearch.rest.uris is blank");
                return;
            }

            //解析yml中的配置轉化為HttpHost陣列
            String[] uriArr = uris.split(",");
            HttpHost[] httpHostArr = new HttpHost[uriArr.length];
            int i = 0;
            for (String uri : uriArr) {
                if (StringUtils.isEmpty(uris)) {
                    continue;
                }

                try {
                    //拆分出ip和埠號
                    String[] split = uri.split(":");
                    String host = split[0];
                    String port = split[1];
                    HttpHost httpHost = new HttpHost(host, Integer.parseInt(port), "http");
                    httpHostArr[i++] = httpHost;
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
            RestClientBuilder builder = RestClient.builder(httpHostArr);
            restHighLevelClient = new RestHighLevelClient(builder);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 建立索引
     *
     * @param index
     * @return
     */
    public boolean createIndex(String index) throws IOException {
        if (isIndexExist(index)) {
            log.error("Index is  exits!");
            return false;
        }
        //1.建立索引請求
        CreateIndexRequest request = new CreateIndexRequest(index);
        //2.執行客戶端請求
        CreateIndexResponse response = restHighLevelClient.indices()
                .create(request, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    }

    /**
     * 判斷索引是否存在
     *
     * @param index
     * @return
     */
    public boolean isIndexExist(String index) throws IOException {
        GetIndexRequest request = new GetIndexRequest(index);
        return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 刪除索引
     *
     * @param index
     * @return
     */
    public boolean deleteIndex(String index) throws IOException {
        if (!isIndexExist(index)) {
            log.error("Index is not exits!");
            return false;
        }
        DeleteIndexRequest request = new DeleteIndexRequest(index);
        AcknowledgedResponse delete = restHighLevelClient.indices()
                .delete(request, RequestOptions.DEFAULT);
        return delete.isAcknowledged();
    }

    /**
     * 新增/更新資料
     *
     * @param object 要新增/更新的資料
     * @param index  索引,類似資料庫
     * @param id     資料ID
     * @return
     */
    public String submitData(Object object, String index, String id) throws IOException {
        if (null == id) {
            return addData(object, index);
        }
        if (this.existsById(index, id)) {
            return this.updateDataByIdNoRealTime(object, index, id);
        } else {
            return addData(object, index, id);
        }
    }

    /**
     * 新增資料,自定義id
     *
     * @param object 要增加的資料
     * @param index  索引,類似資料庫
     * @param id     資料ID,為null時es隨機生成
     * @return
     */
    public String addData(Object object, String index, String id) throws IOException {
        if (null == id) {
            return addData(object, index);
        }
        if (this.existsById(index, id)) {
            return this.updateDataByIdNoRealTime(object, index, id);
        }
        //建立請求
        IndexRequest request = new IndexRequest(index);
        request.id(id);
        request.timeout(TimeValue.timeValueSeconds(1));
        //將資料放入請求 json
        request.source(JSON.toJSONString(object), XContentType.JSON);
        //客戶端傳送請求
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        log.info("新增資料成功 索引為: {}, response 狀態: {}, id為: {}", index, response.status().getStatus(), response.getId());
        return response.getId();
    }

    /**
     * 資料新增 隨機id
     *
     * @param object 要增加的資料
     * @param index  索引,類似資料庫
     * @return
     */
    public String addData(Object object, String index) throws IOException {
        return addData(object, index, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
    }

    /**
     * 通過ID刪除資料
     *
     * @param index 索引,類似資料庫
     * @param id    資料ID
     * @return
     */
    public String deleteDataById(String index, String id) throws IOException {
        DeleteRequest request = new DeleteRequest(index, id);
        DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        return deleteResponse.getId();
    }

    /**
     * 通過ID 更新資料
     *
     * @param object 要更新資料
     * @param index  索引,類似資料庫
     * @param id     資料ID
     * @return
     */
    public String updateDataById(Object object, String index, String id) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(index, id);
        updateRequest.timeout("1s");
        updateRequest.doc(JSON.toJSONString(object), XContentType.JSON);
        UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        log.info("索引為: {}, id為: {},updateResponseID:{}, 更新資料成功", index, id, updateResponse.getId());
        return updateResponse.getId();
    }

    /**
     * 通過ID 更新資料,保證實時性
     *
     * @param object 要增加的資料
     * @param index  索引,類似資料庫
     * @param id     資料ID
     * @return
     */
    public String updateDataByIdNoRealTime(Object object, String index, String id) throws IOException {
        //更新請求
        UpdateRequest updateRequest = new UpdateRequest(index, id);

        //保證資料實時更新
        updateRequest.setRefreshPolicy("wait_for");

        updateRequest.timeout("1s");
        updateRequest.doc(JSON.toJSONString(object), XContentType.JSON);
        //執行更新請求
        UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        log.info("索引為: {}, id為: {},updateResponseID:{}, 實時更新資料成功", index, id, updateResponse.getId());
        return updateResponse.getId();
    }

    /**
     * 通過ID獲取資料
     *
     * @param index  索引,類似資料庫
     * @param id     資料ID
     * @param fields 需要顯示的欄位,逗號分隔(預設為全部欄位)
     * @return
     */
    public Map<String, Object> searchDataById(String index, String id, String fields) throws IOException {
        GetRequest request = new GetRequest(index, id);
        if (StringUtils.isNotEmpty(fields)) {
            //只查詢特定欄位。如果需要查詢所有欄位則不設定該項。
            request.fetchSourceContext(new FetchSourceContext(true, fields.split(","), Strings.EMPTY_ARRAY));
        }
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        return response.getSource();
    }

    /**
     * 通過ID判斷文件是否存在
     *
     * @param index 索引,類似資料庫
     * @param id    資料ID
     * @return
     */
    public boolean existsById(String index, String id) throws IOException {
        GetRequest request = new GetRequest(index, id);
        //不獲取返回的_source的上下文
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");
        return restHighLevelClient.exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 批量插入false成功
     *
     * @param index   索引,類似資料庫
     * @param objects 資料
     * @return
     */
    public boolean bulkPost(String index, List<?> objects) {
        BulkRequest bulkRequest = new BulkRequest();
        BulkResponse response = null;
        //最大數量不得超過20萬
        for (Object object : objects) {
            IndexRequest request = new IndexRequest(index);
            request.source(JSON.toJSONString(object), XContentType.JSON);
            bulkRequest.add(request);
        }
        try {
            response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null != response && response.hasFailures();
    }


    /**
     * 獲取低水平客戶端
     *
     * @return
     */
    public RestClient getLowLevelClient() {
        return restHighLevelClient.getLowLevelClient();
    }

    /**
     * 高亮結果集 特殊處理
     * map轉物件 JSONObject.parseObject(JSONObject.toJSONString(map), Content.class)
     *
     * @param searchResponse
     * @param highlightField
     */
    private List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
        //解析結果
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            Map<String, HighlightField> high = hit.getHighlightFields();
            HighlightField title = high.get(highlightField);
            //原來的結果
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //解析高亮欄位,將原來的欄位換為高亮欄位
            if (title != null) {
                Text[] texts = title.fragments();
                StringBuilder nTitle = new StringBuilder();
                for (Text text : texts) {
                    nTitle.append(text);
                }
                //替換
                sourceAsMap.put(highlightField, nTitle.toString());
            }
            list.add(sourceAsMap);
        }
        return list;
    }

    /**
     * 查詢並分頁
     *
     * @param index          索引名稱
     * @param query          查詢條件
     * @param highlightField 高亮欄位
     * @return
     */
    public List<Map<String, Object>> searchListData(String index,
                                                    SearchSourceBuilder query,
                                                    String highlightField) throws IOException {
        SearchRequest request = new SearchRequest(index);

        //高亮
        HighlightBuilder highlight = new HighlightBuilder();
        highlight.field(highlightField);
        //關閉多個高亮
        highlight.requireFieldMatch(false);
        highlight.preTags("<span style='color:red'>");
        highlight.postTags("</span>");
        query.highlighter(highlight);
        //不返回源資料。只有條數之類的資料。
        //builder.fetchSource(false);
        request.source(query);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        log.info("totalHits:" + response.getHits().getTotalHits());
        if (response.status().getStatus() == 200) {
            // 解析物件
            return setSearchResponse(response, highlightField);
        }
        return null;
    }
}

4、新增es控制器ElasticSearchController.java作為測試使用

package com.example.study.controller;

import com.example.study.model.entity.UserEntity;
import com.example.study.model.vo.ResponseVo;
import com.example.study.util.BuildResponseUtils;
import com.example.study.util.ElasticSearchUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.Strings;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * ElasticSearch控制器
 *
 * @author 154594742@qq.com
 * @date 2021/3/5 10:02
 */

@Api(tags = "ElasticSearch控制器")
@RestController
@RequestMapping("elasticSearch")
public class ElasticSearchController {

    @Autowired
    private ElasticSearchUtils elasticSearchUtils;

    /**
     * 新增索引
     *
     * @param index 索引
     * @return ResponseVo
     */
    @ApiOperation("新增索引")
    @PostMapping("index")
    public ResponseVo<?> createIndex(String index) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.createIndex(index));
    }

    /**
     * 索引是否存在
     *
     * @param index index
     * @return ResponseVo
     */
    @ApiOperation("索引是否存在")
    @GetMapping("index/{index}")
    public ResponseVo<?> existIndex(@PathVariable String index) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.isIndexExist(index));
    }

    /**
     * 刪除索引
     *
     * @param index index
     * @return ResponseVo
     */
    @ApiOperation("刪除索引")
    @DeleteMapping("index/{index}")
    public ResponseVo<?> deleteIndex(@PathVariable String index) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.deleteIndex(index));
    }


    /**
     * 新增/更新資料
     *
     * @param entity 資料
     * @param index  索引
     * @param esId   esId
     * @return ResponseVo
     */
    @ApiOperation("新增/更新資料")
    @PostMapping("data")
    public ResponseVo<String> submitData(UserEntity entity, String index, String esId) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.submitData(entity, index, esId));
    }

    /**
     * 通過id刪除資料
     *
     * @param index index
     * @param id    id
     * @return ResponseVo
     */
    @ApiOperation("通過id刪除資料")
    @DeleteMapping("data/{index}/{id}")
    public ResponseVo<String> deleteDataById(@PathVariable String index, @PathVariable String id) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.deleteDataById(index, id));
    }

    /**
     * 通過id查詢資料
     *
     * @param index  index
     * @param id     id
     * @param fields 需要顯示的欄位,逗號分隔(預設為全部欄位)
     * @return ResponseVo
     */
    @ApiOperation("通過id查詢資料")
    @GetMapping("data")
    public ResponseVo<Map<String, Object>> searchDataById(String index, String id, String fields) throws IOException {
        return BuildResponseUtils.buildResponse(elasticSearchUtils.searchDataById(index, id, fields));
    }

    /**
     * 分頁查詢(這只是一個demo)
     *
     * @param index index
     * @return ResponseVo
     */
    @ApiOperation("分頁查詢")
    @GetMapping("data/page")
    public ResponseVo<?> selectPage(String index) throws IOException {
        //構建查詢條件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //精確查詢
        //boolQueryBuilder.must(QueryBuilders.wildcardQuery("name", "張三"));
        // 模糊查詢
        boolQueryBuilder.filter(QueryBuilders.wildcardQuery("name", "張"));
        // 範圍查詢 from:相當於閉區間; gt:相當於開區間(>) gte:相當於閉區間 (>=) lt:開區間(<) lte:閉區間 (<=)
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").from(18).to(32));
        SearchSourceBuilder query = new SearchSourceBuilder();
        query.query(boolQueryBuilder);
        //需要查詢的欄位,預設則查詢全部
        String fields = "";
        //需要高亮顯示的欄位
        String highlightField = "name";
        if (StringUtils.isNotBlank(fields)) {
            //只查詢特定欄位。如果需要查詢所有欄位則不設定該項。
            query.fetchSource(new FetchSourceContext(true, fields.split(","), Strings.EMPTY_ARRAY));
        }
        //分頁引數,相當於pageNum
        Integer from = 0;
        //分頁引數,相當於pageSize
        Integer size = 2;
        //設定分頁引數
        query.from(from);
        query.size(size);

        //設定排序欄位和排序方式,注意:欄位是text型別需要拼接.keyword
        //query.sort("age", SortOrder.DESC);
        query.sort("name" + ".keyword", SortOrder.ASC);

        return BuildResponseUtils.buildResponse(elasticSearchUtils.searchListData(index, query, highlightField));
    }
}

5、執行專案,然後訪問 http://localhost:8080/swagger-ui.html 測試一下效果吧

* 這裡我就只貼上分頁查詢的效果(相信這也是大家最需要的),其餘的大家自行體驗

最後,附上完整程式碼包供大家學習參考,如果對你有幫助,請給個關注或者點個贊吧! 點選下載完整程式碼包

相關文章