莱莱 发表于 2024-10-30 21:24:11

springboot2.7整合elasticsearch

springboot2.7整合elasticsearch,利用demo

1.依赖

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
      </dependency> 2.实体

package com.example.es.model;

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;

import java.util.List;

@Document(indexName = "blog")
public class Article
{
        @Id
        private String id;

        private String title;
        @Field(type = FieldType.Nested, includeInParent = true)
        private List<Author> authors;

        public Article(String title)
        {
                this.title = title;
        }

        public String getId()
        {
                return id;
        }

        public void setId(String id)
        {
                this.id = id;
        }

        public String getTitle()
        {
                return title;
        }

        public void setTitle(String title)
        {
                this.title = title;
        }

        public List<Author> getAuthors()
        {
                return authors;
        }

        public void setAuthors(List<Author> authors)
        {
                this.authors = authors;
        }

        @Override
        public String toString()
        {
                return "Article{" + "id='" + id + '\'' + ", title='" + title + '\'' + ", authors=" + authors + '}';
        }
}
package com.example.es.model;

public class Author
{
        private String name;

        public Author(String name)
        {
                this.name = name;
        }

        public String getName()
        {
                return name;
        }

        public void setName(String name)
        {
                this.name = name;
        }

        @Override
        public String toString()
        {
                return "Author{" + "name='" + name + '\'' + '}';
        }
}
2.恒久层

package com.example.es.repository;

import com.example.es.model.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, String>
{
   Page<Article> findByAuthorsName(String name, Pageable pageable);
   Page<Article> findByTitleIsContaining(String word, Pageable pageable);
   Page<Article> findByTitle(String title, Pageable pageable);
} 3.controller控制器

package com.example.controller;

import com.example.es.model.Article;
import com.example.es.model.Author;
import com.example.es.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/es")
public class EsController
{
        @Autowired
        private ArticleRepository articleRepository;

        @RequestMapping(value = "/test/save1", method = RequestMethod.POST)
        public void testSave()
        {
                Article article1 = new Article("Spring Data ElasticSearch1");
                List<Author> authors1 = new ArrayList<>();
                authors1.add(new Author("god"));
                authors1.add(new Author("John"));

                article1.setAuthors(authors1);
                Article article2 = new Article("Spring Data ElasticSearch2");
                List<Author> authors2 = new ArrayList<>();
                authors2.add(new Author("god"));
                authors2.add(new Author("King"));
                article2.setAuthors(authors2);

                Article article3 = new Article("Spring Data ElasticSearch3");
                List<Author> authors3 = new ArrayList<>();
                authors3.add(new Author("god"));
                authors3.add(new Author("Bill"));
                article3.setAuthors(authors3);
                articleRepository.save(article1);
                articleRepository.save(article2);
                articleRepository.save(article3);
        }

        @RequestMapping(value = "/test/get", method = RequestMethod.GET)
        public List<Article> find(@RequestParam("name") String name)
        {
                Page<Article> god = articleRepository.findByAuthorsName(name, PageRequest.of(0, 1));
                System.out.println(god.getContent());
                return god.getContent();
        }
}
4.设置文件
spring.elasticsearch.uris=http://192.168.10.201:9200
spring.elasticsearch.socket-timeout=10s
spring.elasticsearch.restclient.sniffer.interval=10m
spring.elasticsearch.restclient.sniffer.delay-after-failure=30s
spring.elasticsearch.webclient.max-in-memory-size=1MB
spring.data.elasticsearch.repositories.enabled=true

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: springboot2.7整合elasticsearch