2024-03-13  阅读(1)
原文作者:吴声子夜歌 原文地址: https://blog.csdn.net/cold___play/article/details/104871500

引入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>pers.zhang</groupId>
        <artifactId>sb_elasticsearch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>sb_elasticsearch</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

配置

application.yml

    spring:
      data:
        elasticsearch:
          cluster-name: elasticsearch
          cluster-nodes: 127.0.0.1:9300
          repositories:
            enabled: true

启动类

    @SpringBootApplication
    @EnableElasticsearchRepositories(basePackages = "pers.zhang")
    public class SbElasticsearchApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SbElasticsearchApplication.class, args);
        }
    
    }

实体类

    package pers.zhang.sb_elasticsearch.entity;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    
    /**
     * @Author: acton_zhang
     * @Date: 2020/3/14 3:30 下午
     * @Version 1.0
     */
    @Document(indexName = "index-article", type = "article")
    public class Article {
    
        @Id
        @Field(type = FieldType.Long, store = true)
        private Long id;
    
        @Field(type = FieldType.text, store = true, analyzer = "ik_smart")
        private String title;
    
        @Field(type = FieldType.text, store = true, analyzer = "ik_smart")
        private String content;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        @Override
        public String toString() {
            return "Article{" +
                    "id=" + id +
                    ", title='" + title + '\'' +
                    ", content='" + content + '\'' +
                    '}';
        }
    }

Repository

    public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
    
        //根据标题进行查询
        List<Article> findByTitle(String title);
    
        List<Article> findByTitleOrContent(String title, String context);
    
        List<Article> findByTitleOrContent(String title, String context, Pageable pageable);
    }

测试

    @SpringBootTest(classes = SbElasticsearchApplication.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class SbElasticsearchApplicationTests {
    
        @Autowired
        ArticleRepository articleRepository;
    
        @Autowired
        ElasticsearchTemplate elasticsearchTemplate;
    
        @Test
        public void createIndex() {
            elasticsearchTemplate.createIndex(Article.class);
        }
    
        @Test
        public void addDocument () {
            Article article = new Article();
            article.setId(3l);
            article.setTitle("Maven对象模型");
            article.setContent("佛教圣地六块腹肌塑料袋放假了撒");
            articleRepository.save(article);
        }
    
        @Test
        //查询所有
        public void findAll () {
            Iterable<Article> all = articleRepository.findAll();
            all.forEach( a -> System.out.println(a));
        }
    
        @Test
        //根据id查询
        public void findById () {
            Optional<Article> byId = articleRepository.findById(1l);
            System.out.println(byId.get());
        }
    
        @Test
        public void findByTitle () {
            List<Article> list = articleRepository.findByTitle("对象");
            list.forEach( a -> System.out.println(a));
        }
    
        @Test
        //多条件查询
        public void findByTitleOrContent () {
            List<Article> list = articleRepository.findByTitleOrContent("ddd", "放假");
            list.forEach( a -> System.out.println(a));
        }
    
        @Test
        //分页查询
        public void findByPage () {
            Pageable pageable = PageRequest.of(0, 2);
            List<Article> list = articleRepository.findByTitleOrContent("ddd", "放假", pageable);
            list.forEach( a -> System.out.println(a));
        }
    
        //原生查询
        @Test
        public void testNativeSearchQuery () {
            NativeSearchQuery query = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.queryStringQuery("佛放假").defaultField("content"))
                    .withPageable(PageRequest.of(0, 5))
                    .build();
            List<Article> articles = elasticsearchTemplate.queryForList(query, Article.class);
            articles.forEach(a -> System.out.println(a));
        }
    
        @Test
        public void deleteById () {
            articleRepository.deleteById(1l);
        }
    }

Java 面试宝典是大明哥全力打造的 Java 精品面试题,它是一份靠谱、强大、详细、经典的 Java 后端面试宝典。它不仅仅只是一道道面试题,而是一套完整的 Java 知识体系,一套你 Java 知识点的扫盲贴。

它的内容包括:

  • 大厂真题:Java 面试宝典里面的题目都是最近几年的高频的大厂面试真题。
  • 原创内容:Java 面试宝典内容全部都是大明哥原创,内容全面且通俗易懂,回答部分可以直接作为面试回答内容。
  • 持续更新:一次购买,永久有效。大明哥会持续更新 3+ 年,累计更新 1000+,宝典会不断迭代更新,保证最新、最全面。
  • 覆盖全面:本宝典累计更新 1000+,从 Java 入门到 Java 架构的高频面试题,实现 360° 全覆盖。
  • 不止面试:内容包含面试题解析、内容详解、知识扩展,它不仅仅只是一份面试题,更是一套完整的 Java 知识体系。
  • 宝典详情:https://www.yuque.com/chenssy/sike-java/xvlo920axlp7sf4k
  • 宝典总览:https://www.yuque.com/chenssy/sike-java/yogsehzntzgp4ly1
  • 宝典进展:https://www.yuque.com/chenssy/sike-java/en9ned7loo47z5aw

目前 Java 面试宝典累计更新 400+ 道,总字数 42w+。大明哥还在持续更新中,下图是大明哥在 2024-12 月份的更新情况:

想了解详情的小伙伴,扫描下面二维码加大明哥微信【daming091】咨询

同时,大明哥也整理一套目前市面最常见的热点面试题。微信搜[大明哥聊 Java]或扫描下方二维码关注大明哥的原创公众号[大明哥聊 Java] ,回复【面试题】 即可免费领取。

阅读全文