SpringBoot與檢索 ElasticSearch


SpringBoot與檢索 ElasticSearch

一、檢索

我們的應用經常需要添加檢索功能,開源的ElasticSearch是目前全文搜索引|擎的
首選。他可以快速的存儲、搜索和分析海量數據。Spring Boot通過整合Spring
Data ElasticSearch為我們提供了非常便捷的檢索功能支持;
Elasticsearch是一個分布式搜索服務,提供Restful API,底層基於Lucene,采用
多shard (分片)的方式保證數據安全,並且提供自動resharding的功能,github
等大型的站點也是采用了ElasticSearch作為其搜索服務,

二、概念

  • 以員工文檔的形式存儲為例:一個文檔代表一個員工 數據。存儲數據到Elasticsearch的行為叫做索引,但在索引-個文檔之前,需要確定將文檔存儲在哪里。
  • 一個Elasticsearch集群可以包含多個索引,相應的每個索引可以包含多個,類型。這些不同的類型存儲着多個文檔,每個文檔又有 多個屬性。
  • 類似關系:
    1.索引-數據庫
    2.類型-表
    3.文檔-表中的記錄
    4.屬性-列

三、整合ElasticSearch測試

先看一下對應的目錄結構:
3

1. 引入spring-boot-starter-data-elasticsearch

<!--SpringBoot默認使用SpringData ElasticSearch模塊進行操作-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
		<dependency>
			<groupId>io.searchbox</groupId>
			<artifactId>jest</artifactId>
			<version>5.3.3</version>
		</dependency>

2. 安裝Spring Data對應版本的ElasticSearch
這里是在Docker中進行啟動並且進行操作
1
測試一下:出現下面這個界面就是正確運行
6

3. application.propertiies配置
2

4.編寫一個 ElasticsearchRepository 的子接口來操作ES;

public interface BookRepository extends ElasticsearchRepository<Book,Integer> {

    //參照
    // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
   public List<Book> findByBookNameLike(String bookName);

}

5. 測試ElasticSearch

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03ElasticApplicationTests {

	@Autowired
	JestClient jestClient;

	@Autowired
	BookRepository bookRepository;

	@Test
	public void test02(){
//		Book book = new Book();
//		book.setId(1);
//		book.setBookName("西游記");
//		book.setAuthor("吳承恩");
//		bookRepository.index(book);


		for (Book book : bookRepository.findByBookNameLike("游")) {
			System.out.println(book);
		}
	}

	@Test
	public void contextLoads() {
		//1、給Es中索引(保存)一個文檔;
		Article article = new Article();
		article.setId(1);
		article.setTitle("好消息");
		article.setAuthor("zhangsan");
		article.setContent("Hello World");

		//構建一個索引功能
		Index index = new Index.Builder(article).index("atguigu").type("news").build();

		try {
			//執行
			jestClient.execute(index);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//測試搜索
	@Test
	public void search(){

		//查詢表達式
		String json ="{\n" +
				"    \"query\" : {\n" +
				"        \"match\" : {\n" +
				"            \"content\" : \"hello\"\n" +
				"        }\n" +
				"    }\n" +
				"}";

		//更多操作:https://github.com/searchbox-io/Jest/tree/master/jest
		//構建搜索功能
		Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();

		//執行
		try {
			SearchResult result = jestClient.execute(search);
			System.out.println(result.getJsonString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

測試結果如下圖:
7
謝謝!,另外希望大家可以多多訪問我的個人博客

天涯志


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM