spring-data-elasticsearch的使用

加入相关依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!--主要依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    <dependencies>

在实体类上添加相关注解即可实现映射

@Document(indexName = "blog",type = "posts")
public class BlogBean {
    @Id //文档唯一标识
    private String id;
    private String title;
    private String author;
    private String postDate;
    private String content;

    public BlogBean(){};

    public BlogBean(String id, String title, String author, String postDate, String content) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.postDate = postDate;
        this.content = content;
    }

    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 String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPostDate() {
        return postDate;
    }

    public void setPostDate(String postDate) {
        this.postDate = postDate;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "BlogBean{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", postDate='" + postDate + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

在dao层编写BlogRepository接口

public interface BlogRepository extends ElasticsearchRepository<BlogBean,String> {
    Page<BlogBean> findByTitle(String title, Pageable pageable);
}

service层

public interface BlogService {
    BlogBean save(BlogBean blogBean);
    void delete(BlogBean blogBean);
    Page<BlogBean> findByTitle(String title, Pageable pageable);
    List<BlogBean> findAll();
}
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogRepository blogRepository;

    @Override
    public BlogBean save(BlogBean blogBean) {
        return blogRepository.save(blogBean);
    }

    @Override
    public void delete(BlogBean blogBean) {
        blogRepository.delete(blogBean);
    }

    @Override
    public Page<BlogBean> findByTitle(String title, Pageable pageable) {
        return blogRepository.findByTitle(title,pageable);
    }

    @Override
    public List<BlogBean> findAll() {
        return (List<BlogBean>) blogRepository.findAll();
    }
}

controller层

@Controller
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class BlogController {
    @Autowired
    private BlogService service;

    @RequestMapping("/save")
    @ResponseBody
    public void Save(){
        BlogBean blog = new BlogBean("1","无聊的一天","wuba","2019-10-26","今天吃饭,睡觉了");
        System.out.println(blog);
        service.save(blog);
    }

    @GetMapping("/findByTitle")
    @ResponseBody
    public void FindByTitle(@RequestParam String query){
        Pageable pageable = PageRequest.of(1,5);
        Page<BlogBean> pageBlogs = service.findByTitle(query, pageable);
        pageBlogs.getContent().forEach(blog -> System.out.println(blog));
    }
}