<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
@Component
public class MyBean {
private ElasticsearchTemplate template;
@Autowired
public MyBean(ElasticsearchTemplate template) {
this.template = template;
}
// ...
}
elasticsearch.host=localhost elasticsearch.port=9300
@Configuration@PropertySource(value = "classpath:elasticsearch.properties")
@EnableElasticsearchRepositories(basePackages = "co.paan.repository")
public class ElasticsearchConfiguration {
@Resource
private Environment environment;
@Bean
public Client client() {
TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port")));
client.addTransportAddress(address);
return client;
}
@Beanpublic ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
}
@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<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 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;
}
}
public interface PostRepository extends ElasticsearchRepository<Post, String>{
Page<Post> findByTagsName(String name, Pageable pageable);
}
public interface PostService {
Post save(Post post);
Post findOne(String id);
Iterable<Post> findAll();
Page<Post> findByTagsName(String tagName, PageRequest pageRequest);
}
@Servicepublic class PostServiceImpl implements PostService{
@Autowired
private PostRepository postRepository;
@Override
public Post save(Post post) {
postRepository.save(post);
return post;
}
@Overridepublic Post findOne(String id) {
return postRepository.findOne(id);
}
@Overridepublic Iterable<Post> findAll() {
return postRepository.findAll();
}
@Overridepublic Page<Post> findByTagsName(String tagName, PageRequest pageRequest) {
return postRepository.findByTagsName(tagName, pageRequest);
}
}
@Test
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.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));
postService.save(post);
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));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有