1、配置環境java
spring boot 1.5.2.RELEASEnode
Elasticsearch:2.4.4 須要進行軟件安裝並在項目運行的過程當中啓動spring
elasticsearch依賴包:spring-boot-starter-data-elasticsearch:2.1.3.RELEASEsql
jNA依賴包:net.java.dev.jna:jna:4.3.0app
2、配置elasticsearch端口elasticsearch
修改application.propertiestcp
#Elasticsearch服務地址 spring.data.elasticsearch.cluster-nodes=localhost:9300 #設置連接超時時間 spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
3、正文ide
//@Document 標識是一個文檔 indexName:索引的名稱 @Document(indexName = "blog",type = "blog") public class EsBlog implements Serializable{ private static final long serialVersionUID = 1L; @Id //主鍵 private String id; private String title; private String summary; private String content; protected EsBlog() { //JPA 規範要求,防止直接使用 } public EsBlog(String title, String summary, String content) { this.title = title; this.summary = summary; this.content = content; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "EsBlog [id=" + id + ", title=" + title + ", summary=" + summary + ", content=" + content + "]"; } }
//String是ID的類型 public interface EsBlogRepository extends ElasticsearchRepository<EsBlog, String> { /** * 根據方法名能夠替代sql查詢,title是實體類的屬性 * Pageable 是返回的內容 * 下午4:41:42 */ Page<EsBlog> findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary,String content, Pageable pageable); }
測試類spring-boot
@RunWith(SpringRunner.class) @SpringBootTest public class BlogRepositoryTest { @Autowired private EsBlogRepository esBlogRepository; @Before public void initRepositoryData() { // 清空全部 esBlogRepository.deleteAll(); esBlogRepository.save(new EsBlog("《關雎》","關關雎鳩", "關關雎鳩,在河之洲。窈窕淑女,君子好逑。參差荇菜,左右流之。窈窕淑女,寤寐求之。夢寐以求,寤寐思服。")); esBlogRepository.save(new EsBlog("《蒹葭》","蒹葭蒼蒼", "蒹葭蒼蒼,白露爲霜。所謂伊人,在水一方。溯洄從之,道阻且長;溯游從之,宛在水中央。蒹葭悽悽,白露未晞")); esBlogRepository.save(new EsBlog("《采薇》","采薇采薇", "采薇采薇,薇亦做止。曰歸曰歸,歲亦莫止。靡家靡室,獫狁之故。不遑啓居,獫狁之故。采薇采薇,薇亦柔止。")); } @Test public void testFindDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining() { Pageable pageable = new PageRequest(0, 20); String title = "關"; String summary = "關關"; String content = "伊人"; Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary, content, pageable); //assertThat(page.getTotalElements()).isEqualTo(2); for (EsBlog esBlog : page) { System.out.println(esBlog.toString()); } } }
Controller類:測試
@RestController @RequestMapping("/blogs") public class BlogController { @Autowired private EsBlogRepository esBlogRepository; @GetMapping public List<EsBlog> list(@RequestParam(value="title",required=false,defaultValue="") String title, @RequestParam(value="summary",required=false,defaultValue="") String summary, @RequestParam(value="content",required=false,defaultValue="") String content, @RequestParam(value="pages",required=false,defaultValue="0") int pages, @RequestParam(value="limit",required=false,defaultValue="10") int limit) { // 數據在 Test 裏面先初始化了,這裏只管取數據 Pageable pageable = new PageRequest(pages, limit); Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary, content, pageable); return page.getContent(); } }
運行以前須要啓動着Elasticsearch服務,並在application.properties中配置:spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
運行url:http://localhost:8080/blogs?title=關&summary=關關&content=伊人