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

SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4)

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


??ES就不用我說了吧,如果是安裝的話可以參考我這邊blog《Centos7.9安裝ElasticSearch6》,安裝好ES,接下來我們配置SpringBoot.在配置之前,先看看版本對(duì)應(yīng)表。
SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4),JAVA,springboot,spring boot,elasticsearch,后端,java,spring
1.修改POM文件的依賴

 <!-- ES  默認(rèn)對(duì)應(yīng)springboot的版本 -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  </dependency>

2.增加application.properties配置文件屬性值

spring.elasticsearch.uris=http://10.10.52.155:9200
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.repositories.cluster-name=elasticsearch
spring.data.elasticsearch.repositories.cluster-nodes=10.10.52.155:9300

緊接上一章的工程,我們?cè)趍odel層下增加一個(gè)po實(shí)體

package com.example.firstweb.model.po;


import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Getter
@Setter
@Data
@Document(indexName = "esinfoindex")
public class EsInfoPo {
    @Id
    private  Long id;

    @Field(type = FieldType.Keyword)
    private String title;

    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String content;

}

Spring Data通過注解來聲明字段的映射屬性,有下面的三個(gè)注解:
@Document 作用在類,標(biāo)記實(shí)體類為文檔對(duì)象,一般有兩個(gè)屬性
    indexName:對(duì)應(yīng)索引庫名稱
    type:對(duì)應(yīng)在索引庫中的類型
    shards:分片數(shù)量,默認(rèn)5
    replicas:副本數(shù)量,默認(rèn)1
  
@Id 作用在成員變量,標(biāo)記一個(gè)字段作為id主鍵
  
@Field 作用在成員變量,標(biāo)記為文檔的字段,并指定字段映射屬性:
    type:字段類型,是枚舉:FieldType,可以是text、long、short、date、integer、object等
    
text:存儲(chǔ)數(shù)據(jù)時(shí)候,會(huì)自動(dòng)分詞,并生成索引
    
keyword:存儲(chǔ)數(shù)據(jù)時(shí)候,不會(huì)分詞建立索引
    
Numerical:數(shù)值類型,分兩類
      基本數(shù)據(jù)類型:long、interger、short、byte、double、float、half_float
      浮點(diǎn)數(shù)的高精度類型:scaled_float
      需要指定一個(gè)精度因子,比如10或100。elasticsearch會(huì)把真實(shí)值乘以這個(gè)因子后存儲(chǔ),取出時(shí)再還原。
    
Date:日期類型
      elasticsearch可以對(duì)日期格式化為字符串存儲(chǔ),但是建議我們存儲(chǔ)為毫秒值,存儲(chǔ)為long,節(jié)省空間。
    
index:是否索引,布爾類型,默認(rèn)是true
    
store:是否存儲(chǔ),布爾類型,默認(rèn)是false
    
analyzer:分詞器名稱,這里的ik_max_word即使用ik分詞器

接下來編些DAO層的代碼EsInfoDao

package com.example.firstweb.dao;

import com.example.firstweb.model.po.EsInfoPo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EsInfoDao extends ElasticsearchRepository<EsInfoPo, Long> {
}

在編寫完Dao層代碼后,開始編寫Service代碼

package com.example.firstweb.service;

import com.example.firstweb.dao.EsInfoDao;

import com.example.firstweb.model.po.EsInfoPo;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.PageRequest;

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;

import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class EsInfoService {

    @Autowired
    private EsInfoDao esInfoDao;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public EsInfoPo save(EsInfoPo esInfo){
        return  esInfoDao.save(esInfo);
    }

    public void searchContent(){

        MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("長(zhǎng)城","title","content");

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(multiMatchQuery)
                .withPageable(PageRequest.of(0, 10))//分頁
                .build();
        SearchHits<EsInfoPo> search = elasticsearchRestTemplate.search(searchQuery,EsInfoPo.class);

        System.out.println("total hits:"+search.getTotalHits());

        System.out.println(search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList()));

    }
}

Service代碼寫完后,直接寫測(cè)試用例代碼,然后運(yùn)行測(cè)試用例。這里有個(gè)注意的地方就是在運(yùn)行之前,要去建立索引,可以用ElasticSearch-Head去建立 一個(gè)esinfoindex的索引,并且運(yùn)行的ES在elasticsearch.yml屬性中加上xpack.security.enabled: false屬性。

Elasticsearch現(xiàn)在的新版本已經(jīng)棄用了ElasticsearchTemplate類,Repository里原來的search方法也已經(jīng)棄用了?,F(xiàn)在都是用ElasticsearchRestTemplate類實(shí)現(xiàn)。

package com.example.firstweb;

import com.example.firstweb.dao.EsInfoDao;
import com.example.firstweb.model.po.EsInfoPo;
import com.example.firstweb.service.EsInfoService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.test.context.junit4.SpringRunner;


@SpringBootTest
class FirstwebApplicationTests {
    @Autowired
    private EsInfoService esInfoService;

    @Test
    void contextLoads() {
    }

    @Test
    void testEs(){
        EsInfoPo esi= new EsInfoPo();
        esi.setId(new Long(1));
        esi.setTitle("中國長(zhǎng)城");
        esi.setContent("姚明奪得男籃世界杯八強(qiáng)");
        esInfoService.save(esi);

        esInfoService.searchContent();
    }
}

在Springboot中直接運(yùn)行FirstwebApplicationTests ,得到如下結(jié)果
SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4),JAVA,springboot,spring boot,elasticsearch,后端,java,spring
并且在ElasticSearch-Head中可以查看到建立的那條索引
SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4),JAVA,springboot,spring boot,elasticsearch,后端,java,spring
工程源代碼可以在這里獲得:鏈接: https://pan.baidu.com/s/1hAvFotdKwXxg80tNVLOz4w 提取碼: wz4a文章來源地址http://www.zghlxwxcb.cn/news/detail-681945.html

到了這里,關(guān)于SpringBoot初級(jí)開發(fā)--加入ElasticSearch數(shù)據(jù)源(4)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(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從數(shù)據(jù)庫讀取數(shù)據(jù)數(shù)據(jù)源配置信息,動(dòng)態(tài)切換數(shù)據(jù)源

    SpringBoot從數(shù)據(jù)庫讀取數(shù)據(jù)數(shù)據(jù)源配置信息,動(dòng)態(tài)切換數(shù)據(jù)源

    ? ? ? ? 首先準(zhǔn)備多個(gè)數(shù)據(jù)庫,主庫smiling-datasource,其它庫test1、test2、test3 ? ? ? ? 接下來,我們?cè)谥鲙靤miling-datasource中,創(chuàng)建表databasesource,用于存儲(chǔ)多數(shù)據(jù)源相關(guān)信息。表結(jié)構(gòu)設(shè)計(jì)如下 ? ? ? ? 創(chuàng)建好表之后,向表databasesource中存儲(chǔ)test1、test2、test3三個(gè)數(shù)據(jù)庫的相關(guān)配置

    2024年01月16日
    瀏覽(37)
  • Springboot多路數(shù)據(jù)源

    1、多路數(shù)據(jù)源配置 (1)SpringBoot+MyBatis-Plus+Oracle實(shí)現(xiàn)多數(shù)據(jù)源配置 https://blog.csdn.net/weixin_44812604/article/details/127386828 (2)SpringBoot+Mybatis搭建Oracle多數(shù)據(jù)源配置簡(jiǎn)述 https://blog.csdn.net/HJW_233/article/details/129103370 (3)SpringBoot+Mybatis+Oracle 增刪改查(簡(jiǎn)單的案例,超詳細(xì)) https://blo

    2024年02月12日
    瀏覽(25)
  • springboot配置數(shù)據(jù)源

    Spring Framework 為 SQL 數(shù)據(jù)庫提供了廣泛的支持。從直接使用 JdbcTemplate 進(jìn)行 JDBC 訪問到完全的對(duì)象關(guān)系映射(object relational mapping)技術(shù),比如 Hibernate。Spring Data 提供了更多級(jí)別的功能,直接從接口創(chuàng)建的 Repository 實(shí)現(xiàn),并使用了約定從方法名生成查詢。 1、JDBC 1、創(chuàng)建項(xiàng)目,導(dǎo)

    2024年02月08日
    瀏覽(30)
  • springboot整合多數(shù)據(jù)源的配置以及動(dòng)態(tài)切換數(shù)據(jù)源,注解切換數(shù)據(jù)源

    springboot整合多數(shù)據(jù)源的配置以及動(dòng)態(tài)切換數(shù)據(jù)源,注解切換數(shù)據(jù)源

    在許多應(yīng)用程序中,可能需要使用多個(gè)數(shù)據(jù)庫或數(shù)據(jù)源來處理不同的業(yè)務(wù)需求。Spring Boot提供了簡(jiǎn)便的方式來配置和使用多數(shù)據(jù)源,使開發(fā)人員能夠輕松處理多個(gè)數(shù)據(jù)庫連接。如果你的項(xiàng)目中可能需要隨時(shí)切換數(shù)據(jù)源的話,那我這篇文章可能能幫助到你 ??:這里對(duì)于pom文件

    2024年02月10日
    瀏覽(33)
  • springboot多數(shù)據(jù)源使用

    springboot多數(shù)據(jù)源使用

    在工作上有一個(gè)新項(xiàng)目,現(xiàn)在需要獲取舊項(xiàng)目的用戶信息、積分的操作等等,所以需要調(diào)用另外一個(gè)項(xiàng)目的數(shù)據(jù)庫,所以我們可以配置多數(shù)據(jù)源。 yml版本 ?properties版本 在impl類上加注解@DS(\\\"master\\\"),master為配置的master名字 調(diào)用方法 ?獲取結(jié)果 ?在impl類上加注解@DS(\\\"slave_1\\\"),

    2024年02月11日
    瀏覽(24)
  • springboot,多數(shù)據(jù)源切換

    需求介紹: ????????要求做一個(gè)平臺(tái),有其他第三方系統(tǒng)接入;每個(gè)系統(tǒng)有自己的數(shù)據(jù)源配置,通過調(diào)用平臺(tái)接口,實(shí)現(xiàn)將數(shù)據(jù)保存到第三方自己的數(shù)據(jù)庫中; 實(shí)現(xiàn)過程: ? ? ? ? 1.在平臺(tái)項(xiàng)目運(yùn)行時(shí),通過接口獲取每個(gè)第三方系統(tǒng)的數(shù)據(jù)源;以key-value的形式保存到全局

    2024年02月16日
    瀏覽(18)
  • SpringBoot動(dòng)態(tài)切換數(shù)據(jù)源

    SpringBoot動(dòng)態(tài)切換數(shù)據(jù)源

    ? Spring提供一個(gè)DataSource實(shí)現(xiàn)類用于動(dòng)態(tài)切換數(shù)據(jù)源—— AbstractRoutingDataSource pom.xml 大概的項(xiàng)目結(jié)構(gòu) 注意:這兩個(gè)事務(wù)管理器,并不能處理分布式事務(wù) 鏈接:https://pan.baidu.com/s/1ymxeKYkI-cx7b5nTQX0KWQ? 提取碼:6bii? --來自百度網(wǎng)盤超級(jí)會(huì)員V4的分享? ? ? ? ? ? ? ??

    2024年02月06日
    瀏覽(19)
  • springboot之多數(shù)據(jù)源配置

    springboot之多數(shù)據(jù)源配置

    實(shí)際開發(fā)中,進(jìn)場(chǎng)可能遇到在一個(gè)引用中可能需要訪問多個(gè)數(shù)據(jù)庫的情況,以下是兩種典型場(chǎng)景: 數(shù)據(jù)分布在不同的數(shù)據(jù)庫匯總,數(shù)據(jù)庫拆了,應(yīng)用沒拆。一個(gè)公司多個(gè)子項(xiàng)目,各用各的數(shù)據(jù)庫,涉及數(shù)據(jù)共享。。。。 為了解決數(shù)據(jù)庫的讀性能瓶頸(讀比寫性能更高,寫鎖

    2024年02月07日
    瀏覽(26)
  • springboot + (mysql/pgsql) + jpa 多數(shù)據(jù)源(不同類數(shù)據(jù)源)

    ?配置文件: datasourceconfig: 數(shù)據(jù)源一: 數(shù)據(jù)源二:

    2024年02月14日
    瀏覽(22)
  • 【精·超詳細(xì)】SpringBoot 配置多個(gè)數(shù)據(jù)源(連接多個(gè)數(shù)據(jù)庫)

    【精·超詳細(xì)】SpringBoot 配置多個(gè)數(shù)據(jù)源(連接多個(gè)數(shù)據(jù)庫)

    目錄 1.項(xiàng)目路徑 2.pom.xml? 引入依賴: 3.application.yml配置文件: 4.兩個(gè)entity類 5.Conroller 6.兩個(gè)Service以及兩個(gè)ServiceImpl? 7.兩個(gè)Mapper及兩個(gè)Mapper.xml? 8.運(yùn)行Application? 然后在瀏覽器請(qǐng)求 9.查看兩個(gè)數(shù)據(jù)庫是否有新增數(shù)據(jù) ? ? ? ? ? 總結(jié): 1.pom.xml 引入依賴: dynamic-datasource-spring-b

    2024年02月12日
    瀏覽(42)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包