SpringBoot應用之分布式索引
來自: http://segmentfault.com/a/1190000004383465
SpringBoot應用系列文章
</div>
序
本文主要講怎么在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<Tag> 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<Tag> getTags() { return tags; } public void setTags(List<Tag> tags) { this.tags = tags; } public Double getRating() { return rating; } public void setRating(Double rating) { this.rating = rating; }
}</pre>
內嵌對象
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; }
}</pre>
repository
public interface PostRepository extends ElasticsearchRepository<Post, String> {Page<Post> findByTagsName(String name, Pageable pageable); List<Post> findByRatingBetween(Double beginning, Double end);
}</pre>
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<Post> findAll() { return postRepository.findAll(); } public Page<Post> findByTagsName(String tagName, PageRequest pageRequest) { return postRepository.findByTagsName(tagName, pageRequest); } List<Post> findByRatingBetween(Double beginning, Double end){ return postRepository.findByRatingBetween(beginning,end); }
}</pre>
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<Post> posts = postService.findByTagsName("tech", new PageRequest(0,10)); Page<Post> posts2 = postService.findByTagsName("tech", new PageRequest(0,10)); Page<Post> posts3 = postService.findByTagsName("maz", new PageRequest(0,10)); assertThat(posts.getTotalElements(), is(1L)); assertThat(posts2.getTotalElements(), is(1L)); assertThat(posts3.getTotalElements(), is(0L)); }
}</pre>
save的截圖
坑
-
NoNodeAvailableException
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
應用中指定的cluster-name與集群中的cluster.name不一致的緣故。
本工程 github
參考
本文由用戶 FranciscaNV 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!