SpringBoot-2.X 學習筆記06 整合elasticsearch-7.3.0 實現簡單增刪改查

springboot-2. 整合elasticsearch-7.3.0實現簡單 增刪改查 (修改沒有實現)*
首先 elasticsearch 配置文件 elasticsearch.yml 中的 cluster.name:xxx 要和 SpringBoot 中的spring.data.elasticsearch.cluster-name=xxx 名字一樣。

cluster.name: my-application
spring.data.elasticsearch.cluster-name=my-application

springboot-2.x 整合 elasticsearch-7.3.0 配置

############################################################
# springboot-2.x 整合 elasticsearch-7.3.0 配置
############################################################
#開啓 Elasticsearch 倉庫(默認值:true)
spring.data.elasticsearch.repositories.enabled=true
#默認 9300 是 Java 客戶端的端口。9200 是支持 Restful HTTP 的接口
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
# Elasticsearch 集羣名(默認值: elasticsearch)
spring.data.elasticsearch.cluster-name=my-application
#集羣節點地址列表,用逗號分隔。如果沒有指定,就啓動一個客戶端節點
#spring.data.elasticsearch.cluster-nodes= 
#用來配置客戶端的額外屬性
#spring.data.elasticsearch.propertie= 
#存儲索引的位置
spring.data.elasticsearch.properties.path.home=/data/project/target/elastic
#連接超時的時間
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

elasticsearch-7.3.0 配置

############################################################
# elasticsearch-7.3.0 配置
############################################################
cluster.name: my-application

測試代碼如下

測試 Entity 類 Books.java

/**  
 * 
 * @Author: hyacinth
 * @Title: Books.java   
 * @Package com.xu.springboot.elasticsearch   
 * @Description: TODO: 
 * @Date: 2019年8月17日 下午8:40:02   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.springboot.elasticsearch;

import java.io.Serializable;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

/** 
 * @Author: hyacinth
 * @ClassName: Books
 * @Description: TODO    
 * @Date: 2019年8月17日 下午8:40:02   
 * @Copyright: hyacinth
 */
@Data
@Document(indexName = "books", type = "info")
public class Books implements Serializable {

	/**   
	 * @Fields serialVersionUID:TODO
	 * @Date 2019年8月17日 下午9:56:28
	 */ 
	private static final long serialVersionUID = -4201309328223793614L;

	@Id
	private String id;

	private String name;

	private String tag;
	
	private String description;
	
	public Books(String id, String name,String tag,String description) {
		this.id = id;
		this.tag = tag;
		this.name = name;
		this.description = description;
	}

	public Books() {

	}
	
}

測試 Repository 類 BookSearchRepository.java

/**  
 * 
 * @Author: hyacinth
 * @Title: BookSearchRepository.java   
 * @Package com.xu.springboot.elasticsearch   
 * @Description: TODO: 
 * @Date: 2019年8月17日 下午8:44:32   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.springboot.elasticsearch;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/** 
 * @Author: hyacinth
 * @ClassName: BookSearchRepository   
 * @Description: TODO    
 * @Date: 2019年8月17日 下午8:44:32   
 * @Copyright: hyacinth
 */
@Repository
public interface BookSearchRepository  extends ElasticsearchRepository<Books,String> {

}

測試 Service 類 BookSearchService.java

/**  
 * 
 * @Author: hyacinth
 * @Title: BookSearchService.java   
 * @Package com.xu.springboot.elasticsearch   
 * @Description: TODO: 
 * @Date: 2019年8月17日 下午8:43:01   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.springboot.elasticsearch;

/** 
 * @Author: hyacinth
 * @ClassName: BookSearchService   
 * @Description: TODO    
 * @Date: 2019年8月17日 下午8:43:01   
 * @Copyright: hyacinth
 */
public interface BookSearchService {
	
	Object save(Books esDocument);
	
	Object findAll();
	Object findById(String id);
	Object findByBookName(String name,String description);

	Object delete(String id);
	
}

測試 ServiceImpl 類

/**  
 * 
 * @Author: hyacinth
 * @Title: BookSearchServiceImpl.java   
 * @Package com.xu.springboot.elasticsearch   
 * @Description: TODO: 
 * @Date: 2019年8月17日 下午8:43:34   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.springboot.elasticsearch;

import java.util.List;

import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

/** 
 * @Author: hyacinth
 * @ClassName: BookSearchServiceImpl   
 * @Description: TODO    
 * @Date: 2019年8月17日 下午8:43:34   
 * @Copyright: hyacinth
 */
@Service
public class BookSearchServiceImpl implements BookSearchService{

	@Autowired
	private BookSearchRepository repository;

	@Autowired
	private ElasticsearchTemplate template;

	@Override
	public Object save(Books books) {
		Books save = repository.save(books);
		return save.toString();
	}

	@Override
	public Object delete(String id) {
		Books esDocument = repository.findById(id).orElse(new Books());
		repository.deleteById(id);
		return esDocument;
	}

	@Override
	public Object findById(String id) {
		Books esDocument = repository.findById(id).orElse(new Books());
		return esDocument;
	}

	@Override
	public Object findByBookName(String name, String description) {
		return null;
	}

	@Override
	public Object findAll() {
		return repository.findAll();
	}

}

測試 ElasticSearchController 類 ElasticSearchController.java

/**  
 * 
 * @Author: hyacinth
 * @Title: ElasticSearchController.java   
 * @Package com.xu.springboot.controler   
 * @Description: TODO: 
 * @Date: 2019年8月12日 下午10:22:59   
 * @Version V-1.0 
 * @Copyright: 2019 hyacinth
 * 
 */  
package com.xu.springboot.controler;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xu.springboot.elasticsearch.BookSearchService;
import com.xu.springboot.elasticsearch.Books;

/** 
 * @Author: hyacinth
 * @ClassName: ElasticSearchController   
 * @Description: TODO    
 * @Date: 2019年8月12日 下午10:22:59   
 * @Copyright: hyacinth
 */
@RestController
@RequestMapping("/es")
public class ElasticSearchController {

	@Autowired
	private BookSearchService service;


	@RequestMapping("/save")
	public Object save(String id) {//http://127.0.0.1:8080/es/save?id=2

		Books document=new Books(id,"心如孤島囚我終老","愛情",UUID.randomUUID().toString());
		service.save(document);

		List<Object> resultList=new ArrayList<Object>();
		resultList.add("succ\t"+id);
		resultList.add(service.findById(id));
		return resultList;
	}

	@RequestMapping("/getbyid")
	public Object getById(String id) {//http://127.0.0.1:8080/es/getbyid?id=2
		return service.findById(id);
	}


	@RequestMapping("/getbybame")
	public Object getByName(String name,String description) {//http://127.0.0.1:8080/es/getbybame?name=心如孤島囚我終老&description=小說
		return service.findByBookName(name, description);
	}

	@RequestMapping("/deletebyid")
	public Object delete(String id) {//http://127.0.0.1:8080/es/deletebyid?id=2
		return service.delete(id);
	}

	@RequestMapping("/getall")
	public Object getAll() {//http://127.0.0.1:8080/es/getall
		return service.findAll();
	}

}

增加 http://127.0.0.1:8080/es/save?id=2

增加

查詢 http://127.0.0.1:8080/es/getbyid?id=2

查詢

查詢所有 http://127.0.0.1:8080/es/getall

查詢所有

刪除 http://127.0.0.1:8080/es/deletebyid?id=2 結果與 查詢所有 比較

刪除
刪除後

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章