欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

springboot中如何集成elasticsearch

這篇文章主要介紹“springboot中如何集成elasticsearch”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“springboot中如何集成elasticsearch”文章能幫助大家解決問題。

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!成都創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務(wù)好的網(wǎng)站設(shè)計公司,成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)負責任的成都網(wǎng)站制作公司!

1,引入依賴

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

2,編寫實體映射類

@Data
@Document(indexName = "index", createIndex = true)
public class Index {
	@Id
    private String id;

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

3,編寫訪問接口(如果需要自動創(chuàng)建索引,該接口必須寫,否則項目啟動時不會自動檢測并創(chuàng)建索引)

@Repository
public interface IndexRepository extends ElasticsearchRepository<Index, String> {
	Page<Index> findByContent(String content, Pageable page);
}

4,測試,用了template,和repository兩種方式測試

@SpringBootTest
public class EsTest {
	@Autowired
	ElasticsearchRestTemplate esTemplate;
	@Autowired
	IndexRepository indexRepository;
	
	@BeforeEach
	public void init() {
		System.out.println("init");
		indexRepository.deleteAll();
		indexRepository.saveAll(ListUtil.of(
		new Index("1","美國留給伊拉克的是個爛攤子嗎"),
		new Index("2","公安部:各地校車將享最高路權(quán)"),
		new Index("3","中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船"),
		new Index("4","中國駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首"),
		new Index("5","中國天眼向全球正式開放下月世界大賽將比拼FAST脈沖星搜索")
		));
	}
	
	@Test
	void testRepositoryQuery() {
		Page<Index> pageList = indexRepository.findByContent("中國", PageRequest.of(0, 10));
		pageList.getContent().forEach(e -> {
			System.out.println("repositoryQuery => "+e);
		});
	}
	
	@Test
	void testTemplateQuery() {
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
				.must(QueryBuilders.simpleQueryStringQuery("中國").field("content"));
		NativeSearchQuery query = new NativeSearchQueryBuilder()
				.withQuery(queryBuilder)
				.withPageable(PageRequest.of(0, 10))
				.build();
		SearchHits<Index> search = esTemplate.search(query, Index.class);
		if(search.hasSearchHits()) {
			search.getSearchHits().forEach(e -> {
				System.out.println("templateQuery => "+e.getContent());
			});
		}
	}
}
init data
templateQuery => Index(id=3, content=中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船)
templateQuery => Index(id=4, content=中國駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首)
templateQuery => Index(id=5, content=中國天眼向全球正式開放下月世界大賽將比拼FAST脈沖星搜索)
init data
repositoryQuery => Index(id=3, content=中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船)
repositoryQuery => Index(id=4, content=中國駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首)
repositoryQuery => Index(id=5, content=中國天眼向全球正式開放下月世界大賽將比拼FAST脈沖星搜索)

5,可啟動一個定時任務(wù),定時ping,防止Connection time out

    @Scheduled(fixedRate = 15000)
	public void ping() {
		esTemplate.execute(client -> client.ping(RequestOptions.DEFAULT));
	}

關(guān)于“springboot中如何集成elasticsearch”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

新聞名稱:springboot中如何集成elasticsearch
URL網(wǎng)址:http://www.chinadenli.net/article46/jiihhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號域名注冊全網(wǎng)營銷推廣品牌網(wǎng)站建設(shè)電子商務(wù)外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營