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

故障转移:

相关推荐
信创天地15 分钟前
核心系统去 “O” 攻坚:信创数据库迁移的双轨运行与数据一致性保障方案
java·大数据·数据库·金融·架构·政务
それども19 分钟前
ES KQL 支持词频统计吗
elasticsearch
zhyf11926 分钟前
Max395(ubuntu24.04)AMD显卡GLM-4.7-UD-IQ1-M量化模型部署手册
大数据·elasticsearch·搜索引擎
小北方城市网32 分钟前
微服务接口设计实战指南:高可用、易维护的接口设计原则与规范
java·大数据·运维·python·微服务·fastapi·数据库架构
武子康1 小时前
大数据-210 如何在Scikit-Learn中实现逻辑回归及正则化详解(L1与L2)
大数据·后端·机器学习
xiaobaishuoAI1 小时前
全链路性能优化实战指南:从瓶颈定位到极致优化
大数据·人工智能·科技·百度·geo
乾元1 小时前
如何把 CCIE / HCIE 的实验案例改造成 AI 驱动的工程项目——从“实验室能力”到“可交付系统”的完整迁移路径
大数据·运维·网络·人工智能·深度学习·安全·机器学习
xiaobaishuoAI1 小时前
后端工程化实战指南:从规范到自动化,打造高效协作体系
java·大数据·运维·人工智能·maven·devops·geo
俊哥大数据2 小时前
【实战项目5】基于Flink新闻热搜大数据实时分析项目
大数据·flink
俊哥大数据2 小时前
【实战项目3】基于Flink广告投放业务领域大数据实时分析项目
大数据·flink