一、配置环境
spring boot 1.5.2.RELEASE
Elasticsearch:2.4.4 需要进行软件安装并在项目运行的过程中启动
elasticsearch依赖包:spring-boot-starter-data-elasticsearch:2.1.3.RELEASE
jNA依赖包:net.java.dev.jna:jna:4.3.0
二、配置elasticsearch端口
修改application.properties
#Elasticsearch服务地址 spring.data.elasticsearch.cluster-nodes=localhost:9300 #设置链接超时时间 spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
三、正文
//@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{ /** * 根据方法名可以替代sql查询,title是实体类的属性 * Pageable 是返回的内容 * 下午4:41:42 */ Page findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary,String content, Pageable pageable); }
测试类
@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 = "伊人"; Pagepage = 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 Listlist(@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 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=伊人