国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用springboot對(duì)Elasticsearch 進(jìn)行索引的增、刪、改、查

這篇具有很好參考價(jià)值的文章主要介紹了使用springboot對(duì)Elasticsearch 進(jìn)行索引的增、刪、改、查。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一 SpringBoot + Elasticsearch 項(xiàng)目環(huán)境搭建

1.1 修改pom文件添加依賴

目前使用spring-boot-starter-parent版本為2.2.8.RELEASE

對(duì)應(yīng)spring-data-elasticsearch版本為2.2.8.RELEASE,版本對(duì)應(yīng)可以自行百度,如果不行直接用elasticsearch-rest-high-level-client工具類吧

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

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.5.0</version>
        </dependency>
1.2 新建配置文件
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES配置類
 *
 * @author lc
 * @version 1.0
 * @date 2022/3/25 10:53
 */
@Configuration
public class ElasticSearchClientConfig {


    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.1.100", 9200, "http")));
        return client;
    }
}

二 RestHighLevelClient的使用

RestHighLevelClient是Elasticsearch 的操作方法,我們先進(jìn)行引用吧。文章來源地址http://www.zghlxwxcb.cn/news/detail-733729.html

@Autowired
private RestHighLevelClient client;

1、創(chuàng)建索引

 @Test
    void testCreateIndex() throws IOException {
        //1 創(chuàng)建索引請(qǐng)求
        CreateIndexRequest request = new CreateIndexRequest("zlc_index");
        //2 客戶端執(zhí)行請(qǐng)求
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse);
    }

2、索引是否存在

@Test
    void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("zlc_index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

3、刪除索引

@Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

4、添加文檔

@Test
    void testAddDocument() throws IOException {
        //創(chuàng)建對(duì)象
        UserES user = new UserES();
        user.setUserName("suwerw");
        user.setUserPhone("178245774");
        //創(chuàng)建請(qǐng)求
        IndexRequest request = new IndexRequest("zlc_index");
        //規(guī)則 put /zlc_index/_doc/1
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");

        //將數(shù)據(jù)放入請(qǐng)求
        request.source(JSON.toJSONString(user), XContentType.JSON);

        //客戶端發(fā)送請(qǐng)求,獲取響應(yīng)結(jié)果
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }

5、判斷文檔是否存在

@Test
    void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("zlc_index", "1");
        //不獲取返回的 _source 的上下文,提高效率
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");

        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

6、獲取文檔

 @Test
    void testGetDocument() throws IOException {
        GetRequest getRequest = new GetRequest("zlc_index", "1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse);
        System.out.println(getResponse.getSourceAsString());
    }

7、更新文檔信息

 @Test
    void testUpdateDocument() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");
        updateRequest.timeout("1s");

        UserES user = new UserES();
        user.setUserName("Zhou_LC");
        user.setUserPhone("233669");
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse);
        System.out.println(updateResponse.status());
    }

8、刪除文檔

  @Test
    void testDeleteDocument() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");
        deleteRequest.timeout("1s");

        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse);
        System.out.println(deleteResponse.status());
    }

到了這里,關(guān)于使用springboot對(duì)Elasticsearch 進(jìn)行索引的增、刪、改、查的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot中進(jìn)行elasticSearch查詢,使用QueryBuilders構(gòu)建各類條件查詢

    BoolQueryBuilder對(duì)象使用must方法build,多個(gè)and使用多個(gè)must BoolQueryBuilder對(duì)象使用should方法build,多個(gè)or使用多個(gè)should使用 本篇文章如有幫助到您,請(qǐng)給「翎野君」點(diǎn)個(gè)贊,感謝您的支持。 首發(fā)鏈接:https://www.cnblogs.com/lingyejun/p/17557467.html

    2024年02月16日
    瀏覽(20)
  • Elasticsearch集群進(jìn)行索引的備份與恢復(fù)

    準(zhǔn)備需要三臺(tái)服務(wù)器 ip如下: 10.1.40.110,10.1.40.12,10.1.40.8 我們要進(jìn)行集群部署首先要進(jìn)行,創(chuàng)建備份共享文件存儲(chǔ)地址 由于是集群部署且服務(wù)器不同所以這邊用到了NFS進(jìn)行文件的共享 備份到集群共享目錄 步驟: 1) 、搭建集群共享目錄 (這里使用NFS也可以使用其它共享目錄

    2024年02月03日
    瀏覽(19)
  • ElasticSearch篇——Restful風(fēng)格詳解以及常見的命令,涵蓋_cat命令查看ES默認(rèn)數(shù)據(jù)、索引和文檔的增刪改查以及復(fù)雜搜索,超詳細(xì)、超全面、超細(xì)節(jié)!

    ElasticSearch篇——Restful風(fēng)格詳解以及常見的命令,涵蓋_cat命令查看ES默認(rèn)數(shù)據(jù)、索引和文檔的增刪改查以及復(fù)雜搜索,超詳細(xì)、超全面、超細(xì)節(jié)!

    一種軟件架構(gòu)風(fēng)格,而不是標(biāo)準(zhǔn),只是提供了一組設(shè)計(jì)原則和約束條件。它主要是用于客戶端和服務(wù)器交互類的軟件。基于這個(gè)風(fēng)格設(shè)計(jì)的軟件可以更加簡潔,更有層次,更易于實(shí)現(xiàn)緩存等機(jī)制。 一、基本Rest命令說明 1、命令 對(duì)應(yīng)的就是head可視化界面的下面的信息(換句話

    2024年01月16日
    瀏覽(66)
  • spring elasticsearch:啟動(dòng)項(xiàng)目時(shí)自動(dòng)創(chuàng)建索引

    在springboot整合spring data elasticsearch項(xiàng)目中,當(dāng)索引數(shù)量較多,mapping結(jié)構(gòu)較為復(fù)雜時(shí),我們常常希望啟動(dòng)項(xiàng)目時(shí)能夠自動(dòng)創(chuàng)建索引及mapping,這樣就不用再到各個(gè)環(huán)境中創(chuàng)建索引了 所以今天咱們就來看看如何自動(dòng)創(chuàng)建索引 如股票使用的是spring data elasticsearch包,其 @Document 注解中

    2024年02月11日
    瀏覽(22)
  • Spring boot 實(shí)現(xiàn) Elasticsearch 動(dòng)態(tài)創(chuàng)建索引

    Spring boot 實(shí)現(xiàn) Elasticsearch 動(dòng)態(tài)創(chuàng)建索引

    查找了很多方法都是通過Spring EL表達(dá)式實(shí)現(xiàn) @Document(IndexName=\\\" #{demo.getIndexName} \\\") 這種方式的問題在于沒法解決數(shù)據(jù)庫里生成的序號(hào),例如我希望通過公司ID生成索引編號(hào)。后來在外網(wǎng)上找到一個(gè)大佬提出的解決方案,這位大佬把兩種方案都實(shí)現(xiàn)了一遍。 通過entityId自定義index

    2024年02月11日
    瀏覽(16)
  • 【SpringBoot】整合Elasticsearch 操作索引及文檔

    【SpringBoot】整合Elasticsearch 操作索引及文檔

    官網(wǎng)操作文檔:Elasticsearch Clients | Elastic ????? ???????? 踩坑太多了。。。這里表明一下Spring Boot2.4以上版本可能會(huì)出現(xiàn)問題,所以我降到了2.2.1.RELEASE。對(duì)于現(xiàn)在2023年6月而言,Es版本已經(jīng)到了8.8,而SpringBoot版本已經(jīng)到了3.x版本。如果是高版本的Boot在配置類的時(shí)候會(huì)發(fā)現(xiàn)

    2024年02月09日
    瀏覽(58)
  • spring data elasticsearch:啟動(dòng)項(xiàng)目時(shí)自動(dòng)創(chuàng)建索引

    spring data elasticsearch:啟動(dòng)項(xiàng)目時(shí)自動(dòng)創(chuàng)建索引

    在springboot整合spring data elasticsearch項(xiàng)目中,當(dāng)索引數(shù)量較多,mapping結(jié)構(gòu)較為復(fù)雜時(shí),我們常常希望啟動(dòng)項(xiàng)目時(shí)能夠自動(dòng)創(chuàng)建索引及mapping,這樣就不用再到各個(gè)環(huán)境中創(chuàng)建索引了 所以今天咱們就來看看如何自動(dòng)創(chuàng)建索引 我們已經(jīng)在實(shí)體類中聲明了索引數(shù)據(jù)結(jié)構(gòu)了,只需要識(shí)別

    2024年02月05日
    瀏覽(18)
  • SpringBoot+Elasticsearch按日期實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建索引(分表)

    SpringBoot+Elasticsearch按日期實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建索引(分表)

    ?? @ 作者: 一恍過去 ?? @ 主頁: https://blog.csdn.net/zhuocailing3390 ?? @ 社區(qū): Java技術(shù)棧交流 ?? @ 主題: SpringBoot+Elasticsearch按日期實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建索引(分表) ?? @ 創(chuàng)作時(shí)間: 2023年02月19日 SpringBoot+Elasticsearch,通過 @Document 注解,利用EL表達(dá)式指定到配置文件,實(shí)現(xiàn)動(dòng)態(tài)生成

    2023年04月08日
    瀏覽(19)
  • SpringBoot 實(shí)現(xiàn) elasticsearch 索引操作(RestHighLevelClient 的應(yīng)用)

    SpringBoot 實(shí)現(xiàn) elasticsearch 索引操作(RestHighLevelClient 的應(yīng)用)

    RestHighLevelClient 是 Elasticsearch 官方提供的Java高級(jí)客戶端,用于與 Elasticsearch 集群進(jìn)行交互和執(zhí)行各種操作。 主要特點(diǎn)和功能如下 : 強(qiáng)類型 :RestHighLevelClient 提供了強(qiáng)類型的 API,可以在編碼過程中獲得更好的類型安全性和 IDE 支持。 兼容性 :RestHighLevelClient 是 Elasticsearch 官方

    2024年02月11日
    瀏覽(86)
  • spring data elasticsearch:動(dòng)態(tài)配置實(shí)體類索引名稱indexName

    spring data elasticsearch:動(dòng)態(tài)配置實(shí)體類索引名稱indexName

    最近接到一個(gè)需要,需要在spring data elasticsearch關(guān)聯(lián)的實(shí)體類中動(dòng)態(tài)的根據(jù)配置文件動(dòng)態(tài)創(chuàng)建索引名稱,比如開發(fā)環(huán)境下索引名稱為user-dev,測試環(huán)境下為user-test,生產(chǎn)環(huán)境為user-prod 一開始接到這個(gè)需要覺得很怪,因?yàn)椴煌h(huán)境的區(qū)分直接搭建不同的es服務(wù)器環(huán)境不就行了嗎,

    2023年04月21日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包