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

故障转移:

相关推荐
Jackyzhe2 小时前
Flink源码阅读:Checkpoint机制(下)
大数据·flink
科创致远2 小时前
esop系统可量化 ROI 投资回报率客户案例故事-案例1:宁波某精密制造企业
大数据·人工智能·制造·精益工程
旺仔Sec11 小时前
2025年安徽省职业院校技能大赛(中职组)大数据应用与服务赛项样题
大数据
ctrigger12 小时前
中级统计师《统计基础理论及相关》考试大纲
大数据
做cv的小昊12 小时前
【TJU】信息检索与分析课程笔记和练习(1)认识文献
经验分享·笔记·学习·搜索引擎·全文检索
SmartBrain13 小时前
洞察:阿里通义DeepResearch 技术
大数据·人工智能·语言模型·架构
不光头强15 小时前
git知识点总结
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客15 小时前
Kibana:使用 ES|QL 构建地图,对国家或地区的指标进行对比
大数据·数据库·elasticsearch·搜索引擎·信息可视化·全文检索·kibana
fuzamei88815 小时前
AI+区块链:为数字金融构建可信交易底座—吴思进出席“中国数字金融独角兽榜单2025交流会”
大数据·人工智能