spring-boot学习笔记(三)ElasticSearch介绍与项目集成

一、ElasticSearch介绍


spring-boot学习笔记(三)ElasticSearch介绍与项目集成_第1张图片

spring-boot学习笔记(三)ElasticSearch介绍与项目集成_第2张图片

分布式高可用,例如将一个系统部署在一台服务器上面,某一天崩溃了,该系统就无法使用了,但是当分布式部署到多台服务器上面之后,其中几个服务器崩溃掉了,但是不会影响整体的运作,从而实现高可用。

spring-boot学习笔记(三)ElasticSearch介绍与项目集成_第3张图片

二、ElasticSearch项目集成

1、build.gradle添加es依赖和jna依赖,application.properties配置文件添加es服务地址和连接超时时间

dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-data-jpa')
	compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
	runtime('com.h2database:h2:1.4.196')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	compile('mysql:mysql-connector-java:6.0.5')
	compile('net.java.dev.jna:jna:4.5.1')
	
}
#elasticSearch服务地址
spring.data.elasticsearch.cluster-nodes=localhost:9300
#设置连接超时时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

2、下载elasticsearch-6.3.0,解压到d:盘下,打开D:\elasticsearch-6.3.0\bin ,点击elasticsearch.bat 启动es服务,

spring-boot学习笔记(三)ElasticSearch介绍与项目集成_第4张图片访问本地9200端口,看到es版本信息说明启动成功。

3、编写测试用例

新建EsBlog实体类

 */
@Document(indexName="blog",type="blog")
public class EsBlog implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 159383211493905568L;
	
	@Id
	private String id;
	private String title;
	private String summary;
	private String content;

编写接口继承ElasticsearchRepository

public interface EsBlogRepository extends  ElasticsearchRepository {
     
	
	/**
	 * 分页查询博客去重
	 * @param title
	 * @param summary
	 * @param content
	 * @param pageable
	 * @return Page
	 */
	Page  findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title,String summary,String content,Pageable pageable); 
}

编写测试test代码

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

	@Autowired
	private EsBlogRepository esBlogRepository;

	public void initRepositoryData() {

		/**
		 * 删除所有数据
		 */
		esBlogRepository.deleteAll();

		/**
		 * 初始化三个blog
		 */
		esBlogRepository.save(new EsBlog("1", "段", "段朝旭"));
		esBlogRepository.save(new EsBlog("2", "朝", "段朝旭"));
		esBlogRepository.save(new EsBlog("3", "旭", "段朝旭"));
	}

	/**
	 * 分页查询博客去重
	 * 
	 * @param title
	 * @param summary
	 * @param content
	 * @param pageable
	 * @return Page
	 */
	@Test
	public void findDistinctEsBlogByTitleContaingOrSummaryContaingOrContentContaing() {

		initRepositoryData();
		Pageable pageable = PageRequest.of(0, 20);
		Page page = esBlogRepository
				.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining("1", "旭", "王", pageable);

		System.out.println(page.getTotalElements());
		for (EsBlog blog : page.getContent()) {
			System.out.println(blog);
		}

	}

运行测试代码之后看到控制台打印出


说明执行成功。

4、编写controller层

@RestController
@RequestMapping("/blog")
public class EsBlogController {
      
	@Autowired
	private EsBlogRepository  esBlogRepository;
	
	@GetMapping
	public List  list(@RequestParam(value="title")String title,
			@RequestParam(value="summary")String summary,
			@RequestParam(value="content")String content,
			@RequestParam(value="pageIndex",defaultValue="0")int pageIndex,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize){
		Pageable  pageable = PageRequest.of(pageIndex,pageSize);
		Page page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary,content,pageable);
		return page.getContent();
		
	}
	  
}

访问如图所示的地址,可看到返回的json格式数据


你可能感兴趣的:(javaweb,java,java,spring-boot,elasticsearch,javaweb,全文检索)