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

SpringBoot集成Easy-Es全過程

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

SpringBoot集成Easy-Es

Easy-Es(簡(jiǎn)稱EE)是一款基于ElasticSearch(簡(jiǎn)稱Es)官方提供的RestHighLevelClient打造的ORM開發(fā)框架,在 RestHighLevelClient 的基礎(chǔ)上,只做增強(qiáng)不做改變,為簡(jiǎn)化開發(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的坐標(biāo)-->
        <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、配置信息

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

3、啟動(dòng)類中添加 @EsMapperScan 注解,掃描 Mapper 文件夾

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

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

}

4、實(shí)體類和mapper

@Data
public class Document {

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

public interface DocumentMapper extends BaseEsMapper<Document> {
}

5、測(cè)試

@RestController
public class EasyEsController {

    @Autowired
    private DocumentMapper documentMapper;

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

    @GetMapping("/search")
    public List<Document> search() {
        // 查詢出所有標(biāo)題為老漢的文檔列表
        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. 自動(dòng)托管之平滑模式(默認(rèn)):在此模式下,索引的創(chuàng)建更新數(shù)據(jù)遷移等全生命周期用戶均不需要任何操作即可完成
  2. 自動(dòng)托管之非平滑模式:在此模式下,索引額創(chuàng)建及更新由EE全自動(dòng)異步完成,但不處理數(shù)據(jù)遷移工作
  3. 手動(dòng)模式:在此模式下,索引的所有維護(hù)工作EE框架均不介入,由用戶自行處理,EE提供了開箱即用的索引CRUD相關(guān)API

前置條件

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

easy-es:
  global-config:
    process_index_mode: manul # 手動(dòng)擋模式

創(chuàng)建索引

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

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

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

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

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

    }

查詢索引

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

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

更新索引

    /**
     * 更新索引
     */
    @Test
    public void testUpdateIndex() {
        // 測(cè)試更新索引
        LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
        // 指定要更新哪個(gè)索引
        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() {
        // 指定要?jiǎng)h除哪個(gè)索引
        String indexName = Document.class.getSimpleName().toLowerCase();
        boolean isOk = documentMapper.deleteIndex(indexName);
        Assertions.assertTrue(isOk);
    }

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

// 插入一條記錄,默認(rèn)插入至當(dāng)前mapper對(duì)應(yīng)的索引
Integer insert(T entity);
// 插入一條記錄 可指定具體插入的索引,多個(gè)用逗號(hào)隔開
Integer insert(T entity, String... indexNames);

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


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

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

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


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

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

// 根據(jù)動(dòng)態(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 查詢 可指定具體的索引,多個(gè)用逗號(hào)隔開 
    T selectById(Serializable id, String... indexNames);
	// 查詢(根據(jù)ID 批量查詢)
    List<T> selectBatchIds(Collection<? extends Serializable> idList);
    // 查詢(根據(jù)ID 批量查詢)可指定具體的索引,多個(gè)用逗號(hào)隔開 
    List<T> selectBatchIds(Collection<? extends Serializable> idList, String... indexNames);
	// 根據(jù)動(dòng)態(tài)查詢條件,查詢一條記錄 若存在多條記錄 會(huì)報(bào)錯(cuò)
    T selectOne(LambdaEsQueryWrapper<T> wrapper);
    // 根據(jù)動(dòng)態(tài)查詢條件,查詢?nèi)坑涗?
    List<T> selectList(LambdaEsQueryWrapper<T> wrapper);

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

總結(jié)

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

相關(guān)文章

  • Spring常用注解 使用注解來構(gòu)造IoC容器的方法

    Spring常用注解 使用注解來構(gòu)造IoC容器的方法

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

    Java包機(jī)制及javadoc詳解

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

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

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

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

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

    JavaCV 拉流存儲(chǔ)到本地示例解析

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

    通過實(shí)例解析Java List正確使用方法

    這篇文章主要介紹了通過實(shí)例解析Java List正確使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    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實(shí)現(xiàn)了CompletionStage接口和Future接口,前者是對(duì)后者的一個(gè)擴(kuò)展,增加了異步回調(diào)、流式處理、多個(gè)Future組合處理的能力。本文就來詳細(xì)講講CompletableFuture的使用方式,需要的可以參考一下
    2022-08-08
  • 深入淺出Java中的字節(jié)流和字符流詳解

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

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

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

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

最新評(píng)論

尼玛县| 柳江县| 拜城县| 墨江| 卓尼县| 加查县| 会泽县| 上栗县| 荔波县| 唐河县| 城步| 来宾市| 绍兴市| 喀什市| 友谊县| 平原县| 湖口县| 如皋市| 阿图什市| 龙海市| 蒙自县| 灌阳县| 曲阳县| 塔城市| 三门峡市| 波密县| 喜德县| 西乌珠穆沁旗| 镇巴县| 富锦市| 南郑县| 双柏县| 芦山县| 通海县| 刚察县| 华宁县| 深泽县| 商洛市| 卓尼县| 马尔康县| 罗定市|