elasticseach作为一个高度可拓展的开源全文搜索和分析引擎,可用于快速地对大数据进行存储,搜索和分析。
主要有以下几个特点:分布式,高可用,异步写入,多API,面向文档
elasticseach的核心概念:近实时,集群,节点(保存数据),索引,分片(将索引分片),副本(分片可设置多个副本)
首先要下载elasticseach,然后运行它,点击bin文件里的elasricseach.bat,出现started说明安装成功:
接下来在我们的SpringBoot中集成elasticseach
首先要添加依赖:
pom.xml
<dependency>
<groupId> org.springframework.boot groupId>
<artifactId> spring-boot-starter-data-elasticsearch artifactId>
dependency>
修改配置文件
application.yml
spring:
data:
elasticsearch:
cluster-name: #默认为elasticsearch
cluster-nodes: #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
properties:
path:
logs: ./elasticsearch/log #elasticsearch日志存储目录
data: ./elasticsearch/data #elasticsearch数据存储目录
这里的cluster-name和cluster-nodes可不配置,默认值就行
这样就引入springboot中elasticseach的依赖了,接下来要通过一个博客小例子来运用它,需要以下几个步骤:
1.编写文档类:EsBlog
2.资源库 :EsBlogRepository
3.控制器:BlogController
4.测试用例:EsBlogRepository
EsBlog
@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(){}
public EsBlog( String title, String summary, String content) {
this.title = title;
this.summary = summary;
this.content = content;
}
//省略getset方法
}
默认情况下,添加@Document注解会对实体中的所有属性建立索引
EsBlogRepository
import com.example.oldbig.domain.EsBlog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface EsBlogRepository extends ElasticsearchRepository {
/*
* 分页查询博客(去重功能)
* 继承ElasticsearchRepository,泛型的参数分别是实体类型和主键类型
* */
Page findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title,String summary,String content,Pageable pageable);
}
BlogController类
@RestController
@RequestMapping("/blogsTest")
public class BlogCotrollerTest {
@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 = new PageRequest(pageIndex, pageSize);
Page page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining
( title, summary, content, pageable);
return page.getContent();
}
}
测试用例:EsBlogRepository
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsBlogRepositoryTest {
@Autowired
private EsBlogRepository esBlogRepository;
@Before
public void initRepository(){
//清除所有数据
// esBlogRepository.deleteAll();
esBlogRepository.save(new EsBlog("登鹳雀楼","王之涣",
"白日依山尽,黄河入海流,欲穷千里目,更上一层楼"));
esBlogRepository.save(new EsBlog("相思","王维",
"红豆生南国,春来发几枝。愿君多采撷,此物最相思。"));
esBlogRepository.save(new EsBlog("静夜思","李白",
"床前明月光,疑是地上霜。举头望明月,低头思故乡。"));
}
@Test
public void findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining() throws Exception {
org.springframework.data.domain.Pageable pageable = new PageRequest(0,20);
String title = "思";
String summary = "相思";
String content = "相思";
Page page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary,content,pageable);
// assertThat(page.getTotalElements()).isEqualTo(4);
System.out.println("----------------start-------------");
for (EsBlog blog :page.getContent()){
System.out.println(blog.toString());
}
System.out.println("--------------end--------------------");
}
}
在运行测试用例之前要启动我们的elasricseach,下载elasricseach后运行bin文件里的elasricseach.bat
运行测试用例即可初始化文档库:添加了三首古诗,这时候运行项目,并且在浏览器输入http://127.0.0.1:8080/blogsTest?title=%E6%80%9D&content=%E7%9B%B8%E6%80%9D&summary=%E7%9B%B8%E6%80%9D
便可获取文档库中搜索结果:
说明搜索成功,可以尝试其他的搜索关键字进行搜索