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

故障转移:

相关推荐
龙山云仓8 小时前
MES系统超融合架构
大数据·数据库·人工智能·sql·机器学习·架构·全文检索
无忧智库8 小时前
某市“十五五“知识产权大数据监管平台与全链条保护系统建设方案深度解读(WORD)
大数据·人工智能
综合热讯8 小时前
股票融资融券交易时间限制一览与制度说明
大数据·人工智能·区块链
华农DrLai8 小时前
Spark SQL Catalyst 优化器详解
大数据·hive·sql·flink·spark
Pluchon8 小时前
硅基计划4.0 算法 简单模拟实现位图&布隆过滤器
java·大数据·开发语言·数据结构·算法·哈希算法
岁岁种桃花儿9 小时前
Flink从入门到上天系列第一篇:搭建第一个Flink程序
大数据·linux·flink·数据同步
历程里程碑9 小时前
普通数组-----除了自身以外数组的乘积
大数据·javascript·python·算法·elasticsearch·搜索引擎·flask
无忧智库9 小时前
某市“十五五”智慧教育2.0建设方案深度解读:从数字化转型到数智化融合的跨越之路(WORD)
大数据
eyun_185009 小时前
把健康小屋搬进单位 让职工暖心 让履职安心
大数据·人工智能·经验分享
会飞的老朱13 小时前
医药集团数智化转型,智能综合管理平台激活集团管理新效能
大数据·人工智能·oa协同办公