ES知识点二

数据聚合

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集群的脑裂:

故障转移:

相关推荐
珠海西格2 小时前
4 月 1 日起执行分布式光伏监控新规,直接影响从业者与项目收益
大数据·运维·服务器·分布式·能源
金融小师妹13 小时前
基于多模态宏观建模与历史序列对齐:原油能源供给冲击的“类1970年代”演化路径与全球应对机制再评估
大数据·人工智能·能源
播播资源13 小时前
OpenAI2026 年 3 月 18 日最新 gpt-5.4-nano模型:AI 智能体的“神经末梢”,以极低成本驱动高频任务
大数据·人工智能·gpt
GJGCY14 小时前
中小企业财务AI工具技术评测:四大类别架构差异与选型维度
大数据·人工智能·ai·架构·财务·智能体
九河云14 小时前
云上安全运营中心(SOC)建设:从被动防御到主动狩猎
大数据·人工智能·安全·架构·数字化转型
武子康15 小时前
大数据-252 离线数仓 - Airflow + Crontab 入门实战:定时调度、DAG 编排与常见报错排查
大数据·后端·apache hive
jinanwuhuaguo15 小时前
OpenClaw、飞书、Claude Code、Codex:四维AI生态体系的深度解构与颗粒化对比分析
大数据·人工智能·学习·飞书·openclaw
Rubin智造社15 小时前
# OpenClaude命令实战|核心控制三剑客/reasoning+/verbose+/status 实操指南
大数据·人工智能
华奥系科技16 小时前
智慧经济新格局:解码社区、园区与城市一体化建设逻辑
大数据·人工智能·科技·物联网·安全
TDengine (老段)16 小时前
TDengine IDMP 组态面板 —— 画布
大数据·数据库·物联网·时序数据库·tdengine·涛思数据