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

SpringBoot 集成Elasticsearch簡單八步

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot 集成Elasticsearch簡單八步。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

? 1、Elasticsearch介紹??

? ? ?Elasticsearch是一個(gè)開源的高擴(kuò)展的分布式全文檢索引擎,它可以近乎實(shí)時(shí)的存儲、檢索數(shù)據(jù);本身擴(kuò)展性很好,可以擴(kuò)展到上百臺服務(wù)器,處理PB級別的數(shù)據(jù)。?
Elasticsearch也使用Java開發(fā)并使用Lucene作為其核心來實(shí)現(xiàn)所有索引和搜索的功能,但是它的目的是通過簡單的RESTful API來隱藏Lucene的復(fù)雜性,從而讓全文搜索變得簡單。

2、安裝Elasticsearch服務(wù)并進(jìn)行相關(guān)配置,啟動(dòng)Elasticsearch服務(wù)

springboot elasticsearch8,elasticsearch,elasticsearch,spring boot,java

3、安裝Elasticsearch Header ?可視查看相關(guān)記錄信息

springboot elasticsearch8,elasticsearch,elasticsearch,spring boot,java

4、 pom.xml

        <!-- elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
        </dependency>

5、application.yml

spring:
? elasticsearch:
? ? jest:
? ? ? uris:
? ? ? ? - http://localhost:9200
? ? ? read-timeout: 5000

6、我們創(chuàng)建一個(gè)MyTestController 接口類

package com.zx.log.controller;


import com.zx.log.entity.UserOperationLog;
import com.zx.log.service.visit.UserOperationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;
import java.util.UUID;

/**
 * @Date 2022/7/20 11:05
 * 用著測試用的“”:“”“:”“:”“
 * @Version 1.0
 */
@RestController
@Api(tags = "測試測試")
@RequestMapping("/mytest")
public class MyTestController {




    @Autowired
    private UserOperationLogService userOperationLogService;

    /**
     * 創(chuàng)建es索引
     *
     * @return
     */
    @RequestMapping(value = "/createIndex")
    public String createIndex() {
        userOperationLogService.createIndex("test");
        return "ok";
    }

    /**
     * 新增行
     *
     * @return
     */
    @RequestMapping(value = "/saveEntity")
    public String saveEntity() {
        UserOperationLog userOperationLog = new UserOperationLog();
        userOperationLog.setNumId(UUID.randomUUID().toString().replace("-", ""));
        userOperationLog.setAppName("111");
        userOperationLog.setAppEnName("appEnName");
        userOperationLog.setFunName("funName");
        userOperationLog.setFunEnName("funEnName");
        userOperationLog.setOpType("opType");
        userOperationLogService.saveEntity(userOperationLog);
        return "ok";
    }

    @RequestMapping(value = "/delEntity")
    public String delEntity() {
        userOperationLogService.delEntity("JkJSIIIBXZZ9IO25Drr1");
        return "ok";
    }

 
}

7、創(chuàng)建接口類:

package com.zx.log.service.visit;

import com.zx.log.entity.UserOperationLog;

import java.util.List;

/**
 * @Author bo
 * @Version 1.0
 */
public interface UserOperationLogService {

    /**
     * 單個(gè)插入ES
     * @param entity
     */
    void saveEntity(UserOperationLog entity);

    /**
     * 刪除某條ES
     * @param id
     */
    void delEntity(String id);

    /**
     * 批量保存到ES
     * @param entityList
     */
    void saveEntity(List<UserOperationLog> entityList);

    /**
     * 在ES中搜索內(nèi)容
     */
    List<UserOperationLog> searchEntity(String searchContent);
    /**
     * 創(chuàng)建索引
     *
     * @param indexName
     */

    void createIndex(String indexName);

    /**
     * 刪除索引
     *
     * @param indexName
     */
    void deleteIndex(String indexName);

}

8、創(chuàng)建實(shí)現(xiàn)類:

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


/**
 * @Author 波
 * @Date 2022/7/21 11:05
 * @Version 1.0
 */
@Service
public class UserOperationLogServiceImpl implements UserOperationLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserOperationLogServiceImpl.class);

    @Autowired
    private JestClient jestClient;

    @Override
    public void saveEntity(UserOperationLog entity) {
        Index index = new Index.Builder(entity).id(entity.getNumId()).index("test").type("news").build();
        try {
            jestClient.execute(index);
            LOGGER.info("插入完成");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }

    @Override
    public void delEntity(String id) {
        Delete index = new Delete.Builder(id).index("test").type("news").build();
        try {
            jestClient.execute(index);
            LOGGER.info("刪除完成");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }


    @Override
    public void saveEntity(List<UserOperationLog> entityList) {
        Bulk.Builder bulk = new Bulk.Builder();
        for (UserOperationLog entity : entityList) {
            Index index = new Index.Builder(entity).id(String.valueOf(entity.getNumId())).index("test").type("news").build();
            bulk.addAction(index);
        }
        try {
            jestClient.execute(bulk.build());
            LOGGER.info("批量插入完成");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }

    /**
     * 在ES中搜索內(nèi)容
     */
    @Override
    public List<UserOperationLog> searchEntity(String searchContent) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("studentName", searchContent));
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex("test").addType("news").build();
        try {
            JestResult result = jestClient.execute(search);
            return result.getSourceAsObjectList(UserOperationLog.class);
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 創(chuàng)建索引
     *
     * @param indexName
     */
    @Override
    public void createIndex(String indexName) {
        try {
            CreateIndex createIndex = new CreateIndex.Builder(indexName).build();
            JestResult result = jestClient.execute(createIndex);
            LOGGER.info("result", result.getJsonString());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 刪除索引
     *
     * @param indexName
     */
    @Override
    public void deleteIndex(String indexName) {
        try {
            DeleteIndex deleteIndex = new DeleteIndex.Builder(indexName).build();
            JestResult result = jestClient.execute(deleteIndex);
            LOGGER.info("result", result.getJsonString());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }

    }

}

? ?上面的實(shí)體類大家可以自由發(fā)揮,隨便創(chuàng)建一個(gè)就行并指定數(shù)據(jù)行ID,主要方便數(shù)據(jù)后續(xù)的刪除。文章來源地址http://www.zghlxwxcb.cn/news/detail-582150.html

到了這里,關(guān)于SpringBoot 集成Elasticsearch簡單八步的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • Springboot集成ElasticSearch實(shí)現(xiàn)簡單的crud、簡單分頁、模糊查詢

    Springboot集成ElasticSearch實(shí)現(xiàn)簡單的crud、簡單分頁、模糊查詢

    pom.xml引入ElasticSearch application.yml配置 啟動(dòng)類加入注解@EnableElasticsearchRepositories ElasticSearchEntity Repository類繼承ElasticsearchRepository ElasticSearchService ElasticSearchController 測試 查看創(chuàng)建的索引(相當(dāng)于MySQL的表) method:GET 刪除索引 method:DELETE 查看索引里的全部數(shù)據(jù), elastic是實(shí)體類

    2023年04月18日
    瀏覽(25)
  • ElasticSearch8 - SpringBoot整合ElasticSearch

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

    2024年03月25日
    瀏覽(78)
  • SpringBoot連接ElasticSearch8.*

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

    2024年02月16日
    瀏覽(25)
  • springboot整合elasticsearch8

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

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

    【springboot-04】ElasticSearch8.7搜索

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

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

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

    2024年02月16日
    瀏覽(26)
  • springboot整合elasticsearch8組合條件查詢

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

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

    導(dǎo)入依賴 ? 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日
    瀏覽(28)
  • 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進(jìn)行匹配查詢。 第一步當(dāng)然是配置依賴 在這里面,spring-boot-starter-data-elasticsearch是3.1.0的,里面的spring-data-elasticsearch是5.1.0的,服務(wù)

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

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

    2024年02月11日
    瀏覽(22)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包