springboot中怎么集成elasticsearch

今天就跟大家聊聊有關springboot中怎么集成elasticsearch,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)技術(shù)團隊十多年來致力于為客戶提供成都網(wǎng)站建設、網(wǎng)站設計、成都品牌網(wǎng)站建設網(wǎng)絡營銷推廣、搜索引擎SEO優(yōu)化等服務。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗豐富的技術(shù)團隊,先后服務、推廣了上千家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機構(gòu)單位。

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具體方法命名規(guī)則參考官網(wǎng):https://docs.spring.io/spring-data/elasticsearch/docs/4.1.7/reference/html/#elasticsearch.query-methods

@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","中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"),
		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=中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首)
templateQuery => Index(id=5, content=中國天眼向全球正式開放下月世界大賽將比拼FAST脈沖星搜索)
init data
repositoryQuery => Index(id=3, content=中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船)
repositoryQuery => Index(id=4, content=中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首)
repositoryQuery => Index(id=5, content=中國天眼向全球正式開放下月世界大賽將比拼FAST脈沖星搜索)

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

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

看完上述內(nèi)容,你們對springboot中怎么集成elasticsearch有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁題目:springboot中怎么集成elasticsearch
標題鏈接:http://muchs.cn/article22/jepscc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、動態(tài)網(wǎng)站、域名注冊、響應式網(wǎng)站網(wǎng)站改版、品牌網(wǎng)站設計

廣告

聲明:本網(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)

搜索引擎優(yōu)化