??ES就不用我說了吧,如果是安裝的話可以參考我這邊blog《Centos7.9安裝ElasticSearch6》,安裝好ES,接下來我們配置SpringBoot.在配置之前,先看看版本對(duì)應(yīng)表。
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)。文章來源:http://www.zghlxwxcb.cn/news/detail-681945.html
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é)果
并且在ElasticSearch-Head中可以查看到建立的那條索引
工程源代碼可以在這里獲得:鏈接: 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)!