摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在中使用。呢,从其根源来讲,是索引服务,但是讲高端一点,就是分布式的实时文件存储分布式的实时分析搜索引擎。
SpringBoot应用系列文章
SpringBoot应用之配置中心
SpringBoot应用之分布式会话
SpringBoot应用之分布式索引
SpringBoot应用之分布式缓存
SpringBoot应用之消息队列
SpringBoot应用之ELK
序本文主要讲怎么在SpringBoot中使用elasticsearch。elasticsearch呢,从其根源来讲,是索引服务,但是讲高端一点,就是分布式的实时文件存储、分布式的实时分析搜索引擎。
准备es详见docker环境搭建elasticsearch这篇。
新建项目 application.propertiesspring.data.elasticsearch.repositories.enabled=true spring.data.elasticsearch.cluster-name=cn-out-of-box spring.data.elasticsearch.cluster-nodes=192.168.99.100:9300模型
@Document(indexName = "post", type = "post", shards = 1, replicas = 0) public class Post { @Id private String id; private String title; private Double rating; @Field(type= FieldType.Nested) private Listtags; 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 getTags() { return tags; } public void setTags(List tags) { this.tags = tags; } public Double getRating() { return rating; } public void setRating(Double rating) { this.rating = rating; } }
内嵌对象
public class Tag { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }repository
public interface PostRepository extends ElasticsearchRepositoryservice层{ Page findByTagsName(String name, Pageable pageable); List findByRatingBetween(Double beginning, Double end); }
@Service public class PostService { @Autowired PostRepository postRepository; public Post save(Post post){ postRepository.save(post); return post; } public Post findOne(String id) { return postRepository.findOne(id); } public IterableTestfindAll() { return postRepository.findAll(); } public Page findByTagsName(String tagName, PageRequest pageRequest) { return postRepository.findByTagsName(tagName, pageRequest); } List findByRatingBetween(Double beginning, Double end){ return postRepository.findByRatingBetween(beginning,end); } }
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = EsdemoApplication.class) public class EsdemoApplicationTests { @Autowired private PostService postService; @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Before public void before() { elasticsearchTemplate.deleteIndex(Post.class); elasticsearchTemplate.createIndex(Post.class); elasticsearchTemplate.putMapping(Post.class); elasticsearchTemplate.refresh(Post.class, true); } @Test public void testSave() throws Exception { 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("Bigining with spring boot application and elasticsearch"); post.setRating(9.5); post.setTags(Arrays.asList(tag, tag2)); postService.save(post); assertThat(post.getId(), notNullValue()); Post post2 = new Post(); post2.setId("2"); post2.setTitle("Bigining with spring boot application"); post2.setTags(Arrays.asList(tag)); post2.setRating(7.5); postService.save(post2); assertThat(post2.getId(), notNullValue()); } @Test //tag必须不是nested的 public void testFindByTagsName() throws Exception { 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("Bigining with spring boot application and elasticsearch"); post.setRating(9.4); post.setTags(Arrays.asList(tag, tag2)); postService.save(post); Post post2 = new Post(); post2.setId("1"); post2.setTitle("Bigining with spring boot application"); post2.setTags(Arrays.asList(tag)); post2.setRating(9.6); postService.save(post2); Pageposts = postService.findByTagsName("tech", new PageRequest(0,10)); Page posts2 = postService.findByTagsName("tech", new PageRequest(0,10)); Page posts3 = postService.findByTagsName("maz", new PageRequest(0,10)); assertThat(posts.getTotalElements(), is(1L)); assertThat(posts2.getTotalElements(), is(1L)); assertThat(posts3.getTotalElements(), is(0L)); } }
save的截图
NoNodeAvailableException
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
应用中指定的cluster-name与集群中的cluster.name不一致的缘故。
参考First Step with Spring Boot and Elasticsearch
Head first elastic search on java with spring boot and data features
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/34041.html
摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在里头配置输出到,使用技术栈实时查看日志。参考下使用或生成符合标准的格式 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoo...
摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在中集成,以及使用的缓存以及高效的集合操作功能。 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoot应用之消息队列 Spr...
摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在应用里头搭建分布式会话。表示秒,即分钟,默认分钟过期。 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoot应用之消息队列...
摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...
摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...
阅读 2137·2021-11-24 09:39
阅读 1864·2021-10-12 10:12
阅读 625·2021-09-24 09:47
阅读 1077·2021-08-19 11:12
阅读 3331·2019-08-29 13:06
阅读 564·2019-08-26 11:43
阅读 2326·2019-08-23 17:20
阅读 1039·2019-08-23 16:52