Spring Boot整合ElasticSearch实现增删改查基本示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot整合ElasticSearch实现增删改查基本示例相关的知识,希望对你有一定的参考价值。
参考技术A ElasticSearch被命名为大数据搜索引擎,在文件检索、数据存储方面具有天然的优势。而SpringBoot作为服务整合中间件,在服务组装方面是一款万能粘合器,本文主要提供Spring Boot整合ElasticSearch基本增删改示例。ElasticSearch安装过程可参考博主之前笔文:
https://www.toutiao.com/i6827758978567504392/
ElasticSearch基本介绍可参考博主之前笔文:
https://www.toutiao.com/i6884427730096488971/
Maven工程引入:
org.springframework.boot
spring-boot-starter-data-elasticsearch
注意:ElasticSearch版本号与Spring Boot版本号是有关联的,本文笔者Spring Boot版本号是2.1.6,因此安装的ElasticSearch版本号是7.7.0。
package com.opendi.generator.elasticsearch.model;
import lombok.Data;
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;
@Data
@Document(indexName="post", type="post",shards=1,replicas = 0)
public class Post
@Id
private String id;
private String title;
@Field(type=FieldType.Nested)
private List tags;
package com.opendi.generator.model;
import lombok.Data;
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;
@Data
@Document(indexName = "book", type = "_doc", shards = 1, replicas = 0)
public class BookBean
@Id
private String id;
@Field(type = FieldType.Keyword)
private String title;
@Field(type = FieldType.Keyword)
private String author;
@Field(type = FieldType.Keyword)
private String postDate;
public BookBean()
public BookBean(String id, String title, String author, String postDate)
this.id=id;
this.title=title;
this.author=author;
this.postDate=postDate;
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;
@Override
public String toString()
return "BookBean" +
"id='" + id + ''' +
", title='" + title + ''' +
", author='" + author + ''' +
", postDate='" + postDate + ''' +
'';
package com.opendi.generator.elasticsearch.mapper;
import com.opendi.generator.elasticsearch.model.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface PostRepository extends ElasticsearchRepository
Page findByTitle(String title, Pageable pageable);
package com.opendi.generator.dao;
import com.opendi.generator.model.BookBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* 接口关系:
* ElasticsearchRepository --> ElasticsearchCrudRepository --> PagingAndSortingRepository --> CrudRepository
*/
public interface BookRepository extends ElasticsearchRepository
//Optional findById(String id);
Page findByAuthor(String author, Pageable pageable);
Page findByTitle(String title, Pageable pageable);
使用了ElasticSearch里面的操作工具对数据进行操作。
package com.opendi.generator.elasticsearch.service;
import com.opendi.generator.elasticsearch.model.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import java.util.Optional;
public interface PostService
Post save(Post post);
Optional findOne(String id);
Iterable findAll();
Page findByTitle(String title, PageRequest pageRequest);
指定操作对象:
package com.opendi.generator.elasticsearch.service.impl;
import com.opendi.generator.elasticsearch.mapper.PostRepository;
import com.opendi.generator.elasticsearch.model.Post;
import com.opendi.generator.elasticsearch.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class PostServiceImpl implements PostService
@Autowired
private PostRepository postRepository;
@Override
public Post save(Post post)
postRepository.save(post);
return post;
@Override
public Optional findOne(String id)
return postRepository.findById(id);
@Override
public Iterable findAll()
return postRepository.findAll();
@Override
public Page findByTitle(String title, PageRequest pageRequest)
return postRepository.findByTitle(title, pageRequest);
package com.opendi.generator.service;
import com.opendi.generator.model.BookBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import java.util.List;
import java.util.Optional;
public interface BookService
Optional findById(String id);
BookBean save(BookBean blog);
void delete(BookBean blog);
Optional findOne(String id);
List findAll();
Page findByAuthor(String author, PageRequest pageRequest);
Page findByTitle(String title, PageRequest pageRequest);
package com.opendi.generator.service.impl;
import com.opendi.generator.dao.BookRepository;
import com.opendi.generator.model.BookBean;
import com.opendi.generator.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service("blogService")
public class BookServiceImpl implements BookService
@Autowired
@Qualifier("bookRepository")
private BookRepository bookRepository;
@Override
public Optional findById(String id)
//CrudRepository中的方法
return bookRepository.findById(id);
@Override
public BookBean save(BookBean blog)
return bookRepository.save(blog);
@Override
public void delete(BookBean blog)
bookRepository.delete(blog);
@Override
public Optional findOne(String id)
return bookRepository.findById(id);
@Override
public List findAll()
return (List ) bookRepository.findAll();
@Override
public Page findByAuthor(String author, PageRequest pageRequest)
return bookRepository.findByAuthor(author,pageRequest);
@Override
public Page findByTitle(String title, PageRequest pageRequest)
return bookRepository.findByTitle(title,pageRequest);
会调用ElasticSearch底层功能进行操作,能够实现ElasticSearch增删改操作。
连接服务器ElasticSearch连接配置:
spring:
data:
elasticsearch:
cluster-name: es2018
cluster-nodes: 10.172.8.154:9300
repositories:
enabled: true
elasticsearch:
jest:
uris: http:// 10.172.8.154:9200
增加连接配置,这样就能连接ElasticSearch客户端了。
ElasticSearch在Controller操作:
package com.opendi.generator.controller;
import com.opendi.generator.model.BookBean;
import com.opendi.generator.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
public class ElasticController
@Autowired
private BookService bookService;
@RequestMapping("/book/id")
@ResponseBody
public BookBean getBookById(@PathVariable String id)
Optional opt =bookService.findById(id);
BookBean book=opt.get();
System.out.println(book);
return book;
@RequestMapping("/save")
@ResponseBody
public void Save()
System.setProperty("es.set.netty.runtime.available.processors", "false");
BookBean book=new BookBean("1","ES入门教程","程裕强","2018-10-01");
System.out.println(book);
bookService.save(book);
package com.opendi.generator.elasticsearch.controller;
import com.opendi.generator.elasticsearch.model.Post;
import com.opendi.generator.elasticsearch.model.Tag;
import com.opendi.generator.elasticsearch.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Api(tags="ElasticSearch示例")
@RestController
public class PostController
@Autowired
private PostService postService;
@ApiOperation(value="得到ES结果")
@GetMapping(value="getList")
public List<page > getList()</page
List<page > list = new ArrayList<page >();</page </page
Tag tag = new Tag();
tag.setId("1");
tag.setName("tech");
Tag tag2 = new Tag();
tag2.setId("2");
tag2.setName("elasticSearch");
Post post = new Post();
post.setId("1");
post.setTitle("This is post");
post.setTags(Arrays.asList(tag,tag2));
postService.save(post);
Post post2 = new Post();
post2.setId("2");
post2.setTitle("Biding2");
post2.setTags(Arrays.asList(tag));
postService.save(post2);
Page posts = postService.findByTitle("This is post", new PageRequest(0, 10));
Page posts2 = postService.findByTitle("Biding2", new PageRequest(0,10));
Page posts3 = postService.findByTitle("Biding2", new PageRequest(0,10));
list.add(posts);
list.add(posts2);
list.add(posts3);
return list;
控制台打印:
以上是关于Spring Boot整合ElasticSearch实现增删改查基本示例的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot:Spring Boot整合Mybatis案例
Spring Boot:Spring Boot整合FreeMarker