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

故障转移:

相关推荐
那就学有所成吧(˵¯͒¯͒˵)27 分钟前
大数据项目(一):Hadoop 云网盘管理系统开发实践
大数据·hadoop·分布式
KKKlucifer1 小时前
数据资产地图构建:文档安全可视化与主动防御
大数据·安全
2501_943695332 小时前
高职工业大数据应用专业,怎么找智能制造企业的数据岗?
大数据·信息可视化·制造
得赢科技2 小时前
智能菜谱研发公司推荐 适配中小型餐饮
大数据·运维·人工智能
好好沉淀2 小时前
Elasticsearch 中获取返回匹配记录总数
开发语言·elasticsearch
Hello.Reader3 小时前
Flink 内存与资源调优从 Process Memory 到 Fine-Grained Resource Management
大数据·flink
有代理ip4 小时前
成功请求的密码:HTTP 2 开头响应码深度解析
java·大数据·python·算法·php
jl48638214 小时前
打造医疗设备的“可靠视窗”:医用控温仪专用屏从抗菌设计到EMC兼容的全链路解析
大数据·运维·人工智能·物联网·人机交互
好好沉淀4 小时前
ES 脚本核心语法:ctx._source [‘group_id‘]
java·elasticsearch·script
刺客xs4 小时前
git 入门常用命令
大数据·git·elasticsearch