具体的开发步骤:
第一步当然还是先引入所需要用到的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.jna/jna --> <dependency> <groupId>com.sun.jna</groupId> <artifactId>jna</artifactId> <version>3.0.9</version> </dependency>
第二步在application.properties配置文件中加入两个属性:
这里的ip地址就是我们前边博客所述的在linux上搭建elasticsearch环境的主机地址
9200:网页端访问所用的端口
9300:程序端所用的端口 由于这里是程序端 所以我们用的是ip地址+9300
正式代码的编写: 例子是一个分页查询博客 这里我们走的是直接添加的数据 没有走到数据库物理表
1.创建实体
package cn.leon.entity; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import java.io.Serializable; import java.lang.annotation.Documented; /** * Created by leon on 2018/7/6. */ @Document(indexName = "blog",type = "blog") public class EsBlog implements Serializable{ @Id //主键 private Integer id; private String title; private String summary; private String content; public EsBlog(String title, String summary, String content) { this.title = title; this.summary = summary; this.content = content; } public EsBlog() { } public Integer getId() { return id; } public void setId(Integer 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; } }
2.dao层
package cn.leon.dao; import cn.leon.entity.EsBlog; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * Created by leon on 2018/7/6. */ public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String> { //分页查询博客 Page<EsBlog> findByTitleOrSummaryOrContent(String title, String summary, String content, Pageable pageable); }
3.测试类:
package cn.leon; import cn.leon.dao.EsBlogRepository; import cn.leon.entity.EsBlog; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.boot.test.context.SpringBootTest; /** * Created by leon on 2018/7/6. */ @RunWith(SpringRunner.class) @SpringBootTest public class Springboot03ApplicationTests { @Autowired private EsBlogRepository esBlogRepository; @Before public void initData() { esBlogRepository.deleteAll(); //测试数据 esBlogRepository.save(new EsBlog("劝学诗"," 颜真卿的劝学诗","黑发不知勤学早,白首方悔读书迟。")); esBlogRepository.save(new EsBlog("冬夜读书示子聿"," 陆游的冬夜读书示子聿","纸上得来终觉浅,绝知此事要躬行。")); esBlogRepository.save(new EsBlog("泊秦淮"," 杜牧的泊秦淮","商女不知亡国恨,隔江犹唱后庭花。")); } @Test public void testFind() { Pageable pageable=new PageRequest(0,2); String title="诗"; String summary="劝学诗"; String content="勤学"; Page<EsBlog> page = esBlogRepository.findByTitleOrSummaryOrContent(title, summary, content, pageable); assertThat(page.getTotalElements()).isEqualTo(1); for (EsBlog item:page.getContent()){ System.out.println(item.getTitle()); } } }
最后运行出来的结果是:打印出以下内容
第一次走test测试的时候 项目启动会比较慢,耐心等待一下
项目启动之前 必须先把ES的服务跑起来 这样项目才能正常运行