数据聚合

DSL聚合



JavaRestClient聚合


示例:


SpringBoot整合ES
第一步:引入依赖
java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
第二步:配置文件
java
# es的配置
spring:
elasticsearch:
uris: http://192.168.21.128:9200
data:
elasticsearch:
repositories:
enabled: true
第三步:创建实体类
java
@Data
@Document(indexName = "article")
public class Article {
@Id
@Field(index=false,type = FieldType.Integer)
private Integer id;
@Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
private String title;
@Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
private String context;
@Field(store = true,type = FieldType.Integer)
private Integer hits;
}
代码示例:
java
@Test
void testSave() {
for (int i = 1; i < 11; i++) {
Article article = new Article();
article.setId(i);
article.setTitle("sd-测试标题" + i);
article.setContext("sd-测试内容" + i);
article.setHits(100+i);
articleMapper.save(article);
}
}
@Test
void testDelete() {
articleMapper.deleteById(1);
}
@Test
void testUpdate() {
Article article = new Article();
article.setId(1);
article.setTitle("测试标题1");
article.setContext("测试内容1");
articleMapper.save(article);
}
//查询所有
@Test
void testFindAll() {
Iterable<Article> articles = articleMapper.findAll();
for (Article article : articles) {
System.out.println(article);
}
}
//主键查询
@Test
void testFindById() {
Optional<Article> byId = articleMapper.findById(1);
System.out.println(byId.get());
}
//分页查询
@Test
void testFindAllWithPage() {
Pageable pageable = PageRequest.of(0, 3);
Page<Article> articles = articleMapper.findAll(pageable);
articles.forEach(System.out::println);
}
//排序查询
@Test
void testFindAllWithSort() {
Sort sort = Sort.by(Sort.Order.desc("hits"));
Iterable<Article> all = articleMapper.findAll(sort);
all.forEach(System.out::println);
}
//分页+排序查询
@Test
void testFindAllWithPageAndSort() {
Sort sort = Sort.by(Sort.Order.desc("hits"));
Pageable pageable = PageRequest.of(0, 3,sort);
Page<Article> articles = articleMapper.findAll(pageable);
articles.forEach(System.out::println);
}
//根据标题查询
@Test
void testFindByTitle() {
Pageable pageable = PageRequest.of(0, 10);
Page<Article> articles = articleMapper.findByTitle("sd-测试标题", pageable);
articles.forEach(System.out::println);
}
//根据标题或内容查询
@Test
void testFindByContext() {
Pageable pageable = PageRequest.of(0, 10);
Page<Article> articles = articleMapper.findByTitleOrContext("标题", "内容", pageable);
articles.forEach(System.out::println);
}
//根据点击量范围查询
@Test
void testFindByHitsBetween() {
Pageable pageable = PageRequest.of(0, 10);
Page<Article> articles = articleMapper.findByHitsBetween(100, 106, pageable);
articles.forEach(System.out::println);
}
//查询少于指定点击量的文章
@Test
void testFindByHitsLessThan() {
Pageable pageable = PageRequest.of(0, 10);
Page<Article> articles = articleMapper.findByHitsLessThan(103, pageable);
articles.forEach(System.out::println);
}
//查询大于指定点击量的文章
@Test
void testFindByHitsGreaterThan() {
Pageable pageable = PageRequest.of(0, 10);
Page<Article> articles = articleMapper.findByHitsGreaterThan(107, pageable);
articles.forEach(System.out::println);
}
命名规则查询

ES集群搭建



ES集群的节点角色


ES集群的脑裂:

故障转移:
