资讯专栏INFORMATION COLUMN

SpringBoot应用之分布式索引

mumumu / 1577人阅读

摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在中使用。呢,从其根源来讲,是索引服务,但是讲高端一点,就是分布式的实时文件存储分布式的实时分析搜索引擎。

SpringBoot应用系列文章

SpringBoot应用之配置中心

SpringBoot应用之分布式会话

SpringBoot应用之分布式索引

SpringBoot应用之分布式缓存

SpringBoot应用之消息队列

SpringBoot应用之ELK

本文主要讲怎么在SpringBoot中使用elasticsearch。elasticsearch呢,从其根源来讲,是索引服务,但是讲高端一点,就是分布式的实时文件存储、分布式的实时分析搜索引擎。

准备es

详见docker环境搭建elasticsearch这篇。

新建项目

application.properties
spring.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 List tags;

    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 ElasticsearchRepository {

    Page findByTagsName(String name, Pageable pageable);

    List findByRatingBetween(Double beginning, Double end);

}
service层
@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 Iterable findAll() {
        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);
    }
}
Test
@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);

        Page posts  = 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应用ELK

    摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在里头配置输出到,使用技术栈实时查看日志。参考下使用或生成符合标准的格式 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoo...

    warkiz 评论0 收藏0
  • SpringBoot应用布式缓存

    摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在中集成,以及使用的缓存以及高效的集合操作功能。 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoot应用之消息队列 Spr...

    zhaot 评论0 收藏0
  • SpringBoot应用布式会话

    摘要:应用系列文章应用之配置中心应用之分布式会话应用之分布式索引应用之分布式缓存应用之消息队列应用之序本文主要讲怎么在应用里头搭建分布式会话。表示秒,即分钟,默认分钟过期。 SpringBoot应用系列文章 SpringBoot应用之配置中心 SpringBoot应用之分布式会话 SpringBoot应用之分布式索引 SpringBoot应用之分布式缓存 SpringBoot应用之消息队列...

    joy968 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    宋华 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<