Elastic Search is an open source, distributed, real-time search and analytics engine. Spring Boot provides basic configuration for Elasticsearch and the abstractions based on it provided by Spring Data Elasticsearch. Spring Boot provides a spring-boot-starter-data-elasticsearch 'StarterPOM' for clustering dependencies.
Introduce the spring-boot-starter-data-elasticsearch dependency, and add the following content to the pom.xml configuration file (based on the pom.xml file in the previous chapter "Spring Boot Construction Framework"):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
An auto-configured ElasticsearchTemplate or Elasticsearch client instance can be injected like any other Spring beans. By default, the instance will attempt to connect to a local in-memory server (a NodeClient in the Elasticsearch project), but it can be switched to by setting spring.data.elasticsearch.clusterNodes to a comma-separated list of host:port A remote server (eg, TransportClient).
@Component
public class MyBean {
private ElasticsearchTemplate template;
@Autowired
public MyBean(ElasticsearchTemplate template) {
this.template = template;
}
// ...
}
If you add an own @Bean of type ElasticsearchTemplate it will replace the default one.
Application Integration ElasticSearch Case
Create a new elasticsearch.properties configuration file and add the following configuration content:
elasticsearch.host=localhost
elasticsearch.port=9300
ElasticSearch configuration, read the elasticsearch.properties configuration file information, the specific code is as follows:
@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());
}
}
Two entity classes, the specific code is as follows:
@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;
}
}
The data source inherits the ElasticsearchRepository class, and the encapsulation interface code is as follows:
public interface PostRepository extends ElasticsearchRepository<Post, String>{
Page<Post> findByTagsName(String name, Pageable pageable);
}
Data service interface and implementation class, the code is as follows:
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);
}
}
The test code is as follows:
@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));
}