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

SpringBoot + ElasticSearch8.4.3 實現(xiàn)簡單CRUD、批量操作

這篇具有很好參考價值的文章主要介紹了SpringBoot + ElasticSearch8.4.3 實現(xiàn)簡單CRUD、批量操作。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1 Java High Level REST Client

SpringBoot + ElasticSearch8.4.3 實現(xiàn)簡單CRUD、批量操作,ElasticSearch,spring boot,elasticsearch,java

2 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot-es</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.10.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>


        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- 做斷點下載使用-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.4.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>elasticsearch-rest-client</artifactId>
                    <groupId>org.elasticsearch.client</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>8.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

3 application.yml

server:
  port: 8500




config:
  es:
    host: 192.168.38.80
    port: 9200
    scheme: http
    username: elastic
    password: 123456

4 EsConfig

package com.es.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsConfig {


    @Value("${config.es.host}")
    private String host;

    @Value("${config.es.port}")
    private Integer port;

    @Value("${config.es.scheme}")
    private String scheme;

    @Value("${config.es.username}")
    private String username;

    @Value("${config.es.password}")
    private String password;

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, scheme)).
                setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        RestClient restClient = builder.build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }


}

5 ProductVo

@AllArgsConstructor
@NoArgsConstructor
@Data
public class ProductPojo {


    private Long id;

    private String name;

    private String desc;

    private Integer price;

    private String level;

    private String type;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    private List<String> tags;

}

6 索引

		@Test
    public void testLambaIndex() throws IOException {
        final String indexName = "test_index";
        ElasticsearchIndicesClient indices = elasticsearchClient.indices();
        boolean flag = indices.exists(request -> request.index(indexName)).value();
        if (flag) {
            System.out.println("索引已經(jīng)存在");
        } else {
            CreateIndexResponse createIndexResponse = indices.create(request -> request.index(indexName));
            System.out.println("創(chuàng)建索引:" + createIndexResponse.acknowledged());
        }

        flag = indices.exists(r -> r.index(indexName)).value();
        if (flag) {
            System.out.println("索引已經(jīng)存在");
        }
        DeleteIndexResponse deleteIndexResponse = indices.delete(r -> r.index(indexName));
        System.out.println("刪除索引:" + deleteIndexResponse.acknowledged());
    }

7 創(chuàng)建數(shù)據(jù)

@Test
public void testCreateDocument() throws IOException {
	  //準備
	  ProductPojo productPojo = new ProductPojo();
	  productPojo.setCreateTime(new Date());
	  productPojo.setId(1000L);
	  productPojo.setDesc("小米手機10");
	  productPojo.setName("小米手機10");
	  productPojo.setType("手機");
	  productPojo.setLevel("旗艦機");
	  productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
	  productPojo.setPrice(5999);
	  //創(chuàng)建
	  elasticsearchClient.create(r -> r.index("product_index").id(String.valueOf(productPojo.getId())).document(productPojo));
}

@Test
public void testCreateBatchDocument() throws IOException {

  BulkRequest.Builder br = new BulkRequest.Builder();

  for (long i = 1001L; i <= 1002; i++) {
      ProductPojo productPojo = new ProductPojo();
      productPojo.setCreateTime(new Date());
      productPojo.setId(i);
      productPojo.setDesc("小米手機10");
      productPojo.setName("小米手機10");
      productPojo.setType("手機");
      productPojo.setLevel("旗艦機");
      productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
      productPojo.setPrice(5999);
      br.operations(bulk -> bulk.index(
              index -> index.index("product_index")
                      .id(String.valueOf(productPojo.getId()))
                      .document(productPojo)
      ));
  }
  elasticsearchClient.bulk(br.build());
}

8 修改數(shù)據(jù)

@Test
public void updateDocument() throws IOException {

    ProductPojo productPojo = new ProductPojo();
    productPojo.setCreateTime(new Date());
    productPojo.setId(1000L);
    productPojo.setDesc("小米手機11");
    productPojo.setName("小米手機11");
    productPojo.setType("手機");
    productPojo.setLevel("旗艦機");
    productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
    productPojo.setPrice(5999);
    elasticsearchClient.update(r -> r.index("product_index").id("1000").doc(productPojo), ProductPojo.class);

}


@Test
public void updateBatchDocument() throws IOException {
    BulkRequest.Builder br = new BulkRequest.Builder();

    for (long i = 1001L; i <= 1002; i++) {
        ProductPojo productPojo = new ProductPojo();
        productPojo.setCreateTime(new Date());
        productPojo.setId(i);
        productPojo.setDesc("小米手機10");
        productPojo.setName("小米手機10");
        productPojo.setType("手機");
        productPojo.setLevel("旗艦機");
        productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
        productPojo.setPrice(5999);
        br.operations(bulk -> bulk.index(
                index -> index.index("product_index")
                        .id(String.valueOf(productPojo.getId()))
                        .document(productPojo)
        ));
    }
    elasticsearchClient.bulk(br.build());


}

9 刪除數(shù)據(jù)

@Test
public void testDeleteDocument() throws IOException {

    elasticsearchClient.delete(r -> r.index("product_index").id(String.valueOf(1000L)));

}

@Test
public void testBatchTest() throws IOException {

    elasticsearchClient.deleteByQuery(
            r -> r.index("product_index").query(
                    q -> q.terms(
                            t -> t.field("id").terms(
                                    v -> v.value(Arrays.asList(FieldValue.of("1000"), FieldValue.of("1001"), FieldValue.of("1002")))
                            )
                    )
            )
    );

}

10 簡單數(shù)據(jù)查詢

@Test
public void testQuery() throws IOException {
    SearchResponse<ProductPojo> search = elasticsearchClient.search(
            req -> {
                req.index("product_index").query(
                        q -> q.term(
                                m -> m.field("name.keyword").value("小米手機10")
                        )
                );
                return req;
            },
            ProductPojo.class
    );
    for (Hit<ProductPojo> pro : search.hits().hits()) {
        System.out.println(JSON.toJSON(pro.source()));
    }
}

SpringBoot + ElasticSearch8.4.3 實現(xiàn)簡單CRUD、批量操作,ElasticSearch,spring boot,elasticsearch,java文章來源地址http://www.zghlxwxcb.cn/news/detail-650980.html

到了這里,關于SpringBoot + ElasticSearch8.4.3 實現(xiàn)簡單CRUD、批量操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領支付寶紅包贊助服務器費用

相關文章

  • springboot3整合elasticsearch8.7.0實現(xiàn)為bean對象創(chuàng)建索引添加映射

    springboot3整合elasticsearch8.7.0實現(xiàn)為bean對象創(chuàng)建索引添加映射

    目錄 準備工作 添加相關依賴 在yml中配置elasticsearch 主要內(nèi)容 實體類 ElasticSearch配置類 測試 確認當前沒有counter索引 啟動spring 再次查詢counter索引? 在測試類中輸出counter索引的映射 官方文檔 要注意版本對應關系 spring官方文檔中有版本對照表 目前我使用的都是最新的版本,

    2024年02月03日
    瀏覽(20)
  • ElasticSearch8 - SpringBoot整合ElasticSearch

    springboot 整合 ES 有兩種方案,ES 官方提供的 Elasticsearch Java API Client 和 spring 提供的 [Spring Data Elasticsearch](Spring Data Elasticsearch) 兩種方案各有優(yōu)劣 Spring:高度封裝,用著舒服。缺點是更新不及時,有可能無法使用 ES 的新 API ES 官方:更新及時,靈活,缺點是太靈活了,基本是一

    2024年03月25日
    瀏覽(78)
  • springboot整合elasticsearch8

    1.引入maven依賴 2.application.yml添加配置 3.編寫config文件 啟動demo項目,通過控制臺日志查看是否能夠正常連接es。 4.在DemoApplicationTests編寫簡單測試操作es。

    2024年02月12日
    瀏覽(21)
  • SpringBoot連接ElasticSearch8.*

    系統(tǒng)中需要使用到ElasticSearch進行內(nèi)容檢索,因此需要搭建SpringBoot + ElasticSearch的環(huán)境。

    2024年02月16日
    瀏覽(25)
  • 【springboot-04】ElasticSearch8.7搜索

    【springboot-04】ElasticSearch8.7搜索

    為什么學?因為它 查詢速度很快 ,而且是非關系型數(shù)據(jù)庫?(NoSql) 一些增刪改查已經(jīng)配置好了,無需重復敲碼 ElasticSearch 更新快,本篇文章將主要介紹一些常用方法。 對于 spirngboot 整合 Es 的文章很少,有些已經(jīng)過時【更新太快了】 ?依賴:Maven 配置類:EsConfig 水果信息

    2024年02月07日
    瀏覽(21)
  • java(springboot)對接elasticsearch8+

    注:jackson包es只用到了databind,之所以全部引用是因為actuator用到了其他,只升級一個會 導致版本沖突 注:因為沒有用springboot自身的es插件所以健康檢查檢測不到es狀態(tài),關閉es檢測 上邊創(chuàng)建索引是定制的加了特殊mapping,正常這樣

    2024年02月16日
    瀏覽(26)
  • springBoot整合ElasticSearch8.x版本

    導入依賴 ? dependency ? ? ? ? groupIdcom.fasterxml.jackson.core/groupId ? ? ? ? artifactIdjackson-databind/artifactId ? ? ? ? version2.13.2/version ? /dependency ? ? dependency ? ? ? ? groupIdorg.glassfish/groupId ? ? ? ? artifactIdjakarta.json/artifactId ? ? ? ? version2.0.1/version ? /dependency ? ? ? ? ? dependency ?

    2023年04月21日
    瀏覽(27)
  • springboot整合elasticsearch8組合條件查詢

    整合過程見上一篇文章 springboot整合elasticsearch8 1.es8多條件組合查詢 2.使用scroll進行大數(shù)據(jù)量查詢

    2024年02月16日
    瀏覽(19)
  • Springboot3.1+Elasticsearch8.x匹配查詢

    Springboot3.1+Elasticsearch8.x匹配查詢

    springboot-starter3.1.0中spring-data-elasticsearch的版本為5.1.0,之前很多方法和類都找不到了。這里主要講講在5.1.0版本下如何使用spring data對elesticsearch8.x進行匹配查詢。 第一步當然是配置依賴 在這里面,spring-boot-starter-data-elasticsearch是3.1.0的,里面的spring-data-elasticsearch是5.1.0的,服務

    2024年02月15日
    瀏覽(23)
  • SpringBoot3.0 整合 ElasticSearch8.5.0 及使用

    這兩個版本都是目前較新的版本,本文選用的依賴是 spring-boot-starter-data-elasticsearch:3.0 ,這個新版本也是改用了es的 elasticsearch-java API,全面推薦使用Lambda語法;另外SpringData本身推出了 Repository 功能(有些類似Mybatis-Plus)的功能,也支持注解簡化開發(fā)。 Docker 快速部署 單機 ela

    2024年02月11日
    瀏覽(22)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領取紅包

二維碼2

領紅包