最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成Easy-Es全過程

 更新時間:2025年06月23日 09:32:52   作者:yololee_  
這篇文章主要介紹了SpringBoot集成Easy-Es全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot集成Easy-Es

Easy-Es(簡稱EE)是一款基于ElasticSearch(簡稱Es)官方提供的RestHighLevelClient打造的ORM開發(fā)框架,在 RestHighLevelClient 的基礎上,只做增強不做改變,為簡化開發(fā)、提高效率而生

一、集成demo

1、添加依賴

        <!-- 排除springboot中內(nèi)置的es依賴,以防和easy-es中的依賴沖突-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--引入es的坐標-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.14.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.14.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.14.1</version>
        </dependency>

        <dependency>
            <groupId>cn.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

2、配置信息

# 默認為true,若為false時,則認為不啟用本框架
easy-es.enable: true
#填你的es連接地址
easy-es.address : 127.0.0.1:9200
# username: 有設置才填寫,非必須
easy-es.username : elastic
# password: 有設置才填寫,非必須
easy-es.password : 123456

3、啟動類中添加 @EsMapperScan 注解,掃描 Mapper 文件夾

@SpringBootApplication
@EsMapperScan("com.example.elasticsearch.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

4、實體類和mapper

@Data
public class Document {

    /**
     * es中的唯一id,當您字段命名為id且類型為String時,且不需要采用UUID及自定義ID類型時,可省略此注解
     */
    @IndexId(type = IdType.NONE)
    private String id;
    /**
     * 文檔標題,不指定類型默認被創(chuàng)建為keyword類型,可進行精確查詢
     */
    private String title;
    /**
     * 文檔內(nèi)容,指定了類型及存儲/查詢分詞器
     */
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String content;
}

public interface DocumentMapper extends BaseEsMapper<Document> {
}

5、測試

@RestController
public class EasyEsController {

    @Autowired
    private DocumentMapper documentMapper;

    @GetMapping("/insert")
    public Integer insert() {
        // 初始化-> 新增數(shù)據(jù)
        Document document = new Document();
        document.setTitle("老漢");
        document.setContent("推*技術過硬");
        return documentMapper.insert(document);
    }

    @GetMapping("/search")
    public List<Document> search() {
        // 查詢出所有標題為老漢的文檔列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, "老漢");
        return documentMapper.selectList(wrapper);
    }

}

http://localhost:8080/insert(插入數(shù)據(jù))

http://localhost:8080/search(查詢數(shù)據(jù))

二、索引CRUD

首先說一下索引的托管模式,EE這里有三種托管模式

  1. 自動托管之平滑模式(默認):在此模式下,索引的創(chuàng)建更新數(shù)據(jù)遷移等全生命周期用戶均不需要任何操作即可完成
  2. 自動托管之非平滑模式:在此模式下,索引額創(chuàng)建及更新由EE全自動異步完成,但不處理數(shù)據(jù)遷移工作
  3. 手動模式:在此模式下,索引的所有維護工作EE框架均不介入,由用戶自行處理,EE提供了開箱即用的索引CRUD相關API

前置條件

索引CRUD相關的API都屬于手動擋范疇,因此我們執(zhí)行下述所有API前必須先配置開啟手動擋,以免和自動擋沖突

easy-es:
  global-config:
    process_index_mode: manul # 手動擋模式

創(chuàng)建索引

    @Test
    void createIndex01(){
        // 絕大多數(shù)場景推薦使用
        documentMapper.createIndex();
    }

    @Test
    void createIndex02(){
        // 適用于定時任務按日期創(chuàng)建索引場景
        String indexName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        documentMapper.createIndex(indexName);
    }

    @Test
    void createIndex03() {
        // 復雜場景使用
        LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
        // 此處簡單起見 索引名稱須保持和實體類名稱一致,字母小寫 后面章節(jié)會教大家更如何靈活配置和使用索引
        wrapper.indexName(Document.class.getSimpleName().toLowerCase());

        // 此處將文章標題映射為keyword類型(不支持分詞),文檔內(nèi)容映射為text類型(支持分詞查詢)
        wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0f)
                .mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);

        // 設置分片及副本信息,可缺省
        wrapper.settings(3, 2);
        // 創(chuàng)建索引
        boolean isOk = documentMapper.createIndex(wrapper);

    }

查詢索引

    @Test
    public void testExistsIndex() {
        // 測試是否存在指定名稱的索引
        String indexName = Document.class.getSimpleName().toLowerCase();
        boolean existsIndex = documentMapper.existsIndex(indexName);
        Assertions.assertTrue(existsIndex);
    }

    @Test
    public void testGetIndex() {
        GetIndexResponse indexResponse = documentMapper.getIndex();
        // 這里打印下索引結構信息 其它分片等信息皆可從indexResponse中取
        indexResponse.getMappings().forEach((k, v) -> System.out.println(v.getSourceAsMap()));
    }

更新索引

    /**
     * 更新索引
     */
    @Test
    public void testUpdateIndex() {
        // 測試更新索引
        LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
        // 指定要更新哪個索引
        String indexName = Document.class.getSimpleName().toLowerCase();
        wrapper.indexName(indexName);
        wrapper.mapping(Document::getTitle, FieldType.KEYWORD);
        wrapper.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
        wrapper.mapping(Document::getInfo, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
        boolean isOk = documentMapper.updateIndex(wrapper);
        Assertions.assertTrue(isOk);
    }

刪除索引

    @Test
    public void testDeleteIndex() {
        // 指定要刪除哪個索引
        String indexName = Document.class.getSimpleName().toLowerCase();
        boolean isOk = documentMapper.deleteIndex(indexName);
        Assertions.assertTrue(isOk);
    }

三、數(shù)據(jù)CURD

// 插入一條記錄,默認插入至當前mapper對應的索引
Integer insert(T entity);
// 插入一條記錄 可指定具體插入的索引,多個用逗號隔開
Integer insert(T entity, String... indexNames);

// 批量插入多條記錄
Integer insertBatch(Collection<T> entityList)
// 批量插入多條記錄 可指定具體插入的索引,多個用逗號隔開 
Integer insertBatch(Collection<T> entityList, String... indexNames);


 // 根據(jù) ID 刪除
Integer deleteById(Serializable id);
// 根據(jù) ID 刪除 可指定具體的索引,多個用逗號隔開 
Integer deleteById(Serializable id, String... indexNames);

// 根據(jù) entity 條件,刪除記錄
Integer delete(LambdaEsQueryWrapper<T> wrapper);

// 刪除(根據(jù)ID 批量刪除)
Integer deleteBatchIds(Collection<? extends Serializable> idList);
// 刪除(根據(jù)ID 批量刪除)可指定具體的索引,多個用逗號隔開 
Integer deleteBatchIds(Collection<? extends Serializable> idList, String... indexNames);


//根據(jù) ID 更新
Integer updateById(T entity);
//根據(jù) ID 更新 可指定具體的索引,多個用逗號隔開 
Integer updateById(T entity, String... indexNames);

// 根據(jù)ID 批量更新
Integer updateBatchByIds(Collection<T> entityList);
//根據(jù) ID 批量更新 可指定具體的索引,多個用逗號隔開 
Integer updateBatchByIds(Collection<T> entityList, String... indexNames);

// 根據(jù)動態(tài)條件 更新記錄
Integer update(T entity, LambdaEsUpdateWrapper<T> updateWrapper);


	// 獲取總數(shù)
    Long selectCount(LambdaEsQueryWrapper<T> wrapper);
    // 獲取總數(shù) distinct為是否去重 若為ture則必須在wrapper中指定去重字段
    Long selectCount(Wrapper<T> wrapper, boolean distinct);
    
 	// 根據(jù) ID 查詢 
    T selectById(Serializable id);
    // 根據(jù) ID 查詢 可指定具體的索引,多個用逗號隔開 
    T selectById(Serializable id, String... indexNames);
	// 查詢(根據(jù)ID 批量查詢)
    List<T> selectBatchIds(Collection<? extends Serializable> idList);
    // 查詢(根據(jù)ID 批量查詢)可指定具體的索引,多個用逗號隔開 
    List<T> selectBatchIds(Collection<? extends Serializable> idList, String... indexNames);
	// 根據(jù)動態(tài)查詢條件,查詢一條記錄 若存在多條記錄 會報錯
    T selectOne(LambdaEsQueryWrapper<T> wrapper);
    // 根據(jù)動態(tài)查詢條件,查詢?nèi)坑涗?
    List<T> selectList(LambdaEsQueryWrapper<T> wrapper);

參數(shù)文檔:Easy-Es文檔

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Spring常用注解 使用注解來構造IoC容器的方法

    Spring常用注解 使用注解來構造IoC容器的方法

    下面小編就為大家分享一篇Spring常用注解 使用注解來構造IoC容器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Java包機制及javadoc詳解

    Java包機制及javadoc詳解

    為了更好地組織類,Java提供了包機制,用于區(qū)別類名的命名空間,一般利用公司域名倒置作為包名,這篇文章主要介紹了Java包機制以及javadoc,需要的朋友可以參考下
    2022-10-10
  • Win10系統(tǒng)下配置java環(huán)境變量的全過程

    Win10系統(tǒng)下配置java環(huán)境變量的全過程

    這篇文章主要給大家介紹了關于Win10系統(tǒng)下配置java環(huán)境變量的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 解決maven父子工程install的時候排除某些子模塊,讓子模塊不install問題

    解決maven父子工程install的時候排除某些子模塊,讓子模塊不install問題

    在Maven父子工程中,如果希望某個子模塊不被安裝到本地倉庫,可以在該子模塊的`pom.xml`文件中添加以下配置: ```xml ... org.apache.maven.plugins maven-install-plugin 2.5.2 true
    2024-12-12
  • JavaCV 拉流存儲到本地示例解析

    JavaCV 拉流存儲到本地示例解析

    這篇文章主要介紹了JavaCV 拉流存儲到本地示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • 通過實例解析Java List正確使用方法

    通過實例解析Java List正確使用方法

    這篇文章主要介紹了通過實例解析Java List正確使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot打包發(fā)布到linux上(centos 7)的步驟

    SpringBoot打包發(fā)布到linux上(centos 7)的步驟

    這篇文章主要介紹了SpringBoot打包發(fā)布到linux上(centos 7)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-12-12
  • Java多線程工具CompletableFuture的使用教程

    Java多線程工具CompletableFuture的使用教程

    CompletableFuture實現(xiàn)了CompletionStage接口和Future接口,前者是對后者的一個擴展,增加了異步回調(diào)、流式處理、多個Future組合處理的能力。本文就來詳細講講CompletableFuture的使用方式,需要的可以參考一下
    2022-08-08
  • 深入淺出Java中的字節(jié)流和字符流詳解

    深入淺出Java中的字節(jié)流和字符流詳解

    Java 中的輸入輸出(I/O)流主要分為字節(jié)流和字符流,這兩類流為開發(fā)者提供了高效的文件讀寫方式,也解決了不同編碼格式下的字符處理問題,本文將帶你深入了解字節(jié)流和字符流的區(qū)別、應用場景以及如何使用它們處理文件操作
    2024-12-12
  • 在Spring Boot2中使用CompletableFuture的方法教程

    在Spring Boot2中使用CompletableFuture的方法教程

    這篇文章主要給大家介紹了關于在Spring Boot2中使用CompletableFuture的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2019-01-01

最新評論

灌南县| 博白县| 铜梁县| 永清县| 房产| 湛江市| 诸暨市| 孝感市| 石家庄市| 鄯善县| 永寿县| 潼关县| 陇南市| 襄垣县| 客服| 唐山市| 长乐市| 龙游县| 吴江市| 开鲁县| 阿坝| 永顺县| 缙云县| 卢龙县| 改则县| 德钦县| 韩城市| 虞城县| 安泽县| 林周县| 当涂县| 麻栗坡县| 南川市| 应城市| 河东区| 郁南县| 渝中区| 吉林市| 文山县| 喀喇沁旗| 类乌齐县|