Spring Boot series (10) Spring Boot integrated elasticsearch full-text search engine

Source: Internet
Author: User
Tags findone iterable tagname elastic search
Spring Boot series (10) Spring Boot integrated elasticsearch full-text search engine

This article introduces spring boot integration elasticsearch full-text search engine, need springboot actual combat full video tutorial, click here.


Elastic Search is an open source, distributed, real-time search and analysis engine. Spring boot provides a basic configuration for Elasticsearch and spring Data Elasticsearch based on its abstraction. Spring Boot provides a spring-boot-starter-data-elasticsearch ' starterpom ' that is used to aggregate dependencies.


Introducing Spring-boot-starter-data-elasticsearch dependencies, add the following in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>


You can inject an auto-configured elasticsearchtemplate or Elasticsearch client instance like other spring beans. By default, the instance will attempt to connect to a local memory server (a nodeclient in the Elasticsearch project). However, you can switch to a remote server (for example, transportclient) by setting Spring.data.elasticsearch.clusterNodes to a comma-separated list of host:port.


@Component
public class Mybean {
private elasticsearchtemplate template;

@Autowired
Public Mybean (elasticsearchtemplate template) {
This.template = template;
}
// ...
}


If you add a @bean of your own elasticsearchtemplate type, it will replace the default.


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 with the following code:


@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 encapsulates the interface code 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 ();
   t Ag.setid ("1");
   tag.setname ("tech");
   tag tag2 = new Tag ();
   tag2.setid (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.