Spring Boot + Elasticsearch

Source: Internet
Author: User
Tags findone iterable tagname

Spring Data Elasticsearch Elasticsearch
2.0.0.RELEASE 2.2.0
1.4.0.m1 1.7.3
1.3.0.RELEASE 1.5.2
1.2.0.RELEASE 1.4.4
1.1.0.RELEASE 1.3.2
1.0.0.RELEASE

Https://github.com/helloworldtang/spring-data-elasticsearch

1. None of the configured nodes is available or

Org.elasticsearch.transport.RemoteTransportException:Failed to deserialize exception response from stream

Cause: The version of Spring data ElasticSearch does not match the spring boot, elasticSearch version.

Solve:

Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
X <= 1.3.5 Y <= 1.3.4 Z <= 1.7.2*
X >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= Z < 5.0.0**

This is the correspondence between versions. Spring boot 1.3.5 The default Elasticsearch version is 1.5.2, at which point the Elasticsearch client connection under version 1.7 is OK.

Note: Note that the ES default connection port for Java is 9300,9200 is the HTTP port, and these two should be differentiated in use.

2, caused by:java.lang.IllegalArgumentException: @ConditionalOnMissingBean annotations must specify at least one bean (ty PE, name or annotation)

Cause: Spring boot is a 1.3.x version, and ES has a 2.x version. Some classes have been removed in the 2.x version of ES, and these classes are still being used in the 1.3.x version of spring boot, causing this error.

Workaround: Launch a specific version of ES according to the version of issue 1.

Http://www.cnblogs.com/slimer/p/5643820.html the integration Zone is brought-to-you-partnership with 3scale. Take control of your APIs and get a free T-shirt if you complete the 3step challenge.

We often use Elasticsearch to improve performance application, especially searching and caching ation scale and adapt in real-time.

Elasticsearch is a flexible and powerful open source, distributed, real-time search and analytics engine. In this article, I would like to introduce how to use Elasticsearch in Java applications:by using Spring Boot data elasti Csearch. Spring Boot now easy and powerful, and we can build fast Java and Web applications with a simple configuration.

By following the steps below, you can start writing your first application.

Source Code:https://github.com/herotl2005/spring-data-elasticsearch-sample

Requirement enviroment

1. Install Elasticsearch

2. Install Gradle

3. IDE Eclipse or Intellij idea

Step by Step Coding

1. Gradle Build

dependencies {testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 ' compile ' org.springframework.boot: Spring-boot-starter-data-elasticsearch:1.2.0.release ' Compile ' Org.springframework.data:spring-data-cassandra : 1.1.1.RELEASE ' compile ' org.springframework:spring-test:4.1.2.release ' compile ' org.springframework.boot: Spring-boot-starter-logging:1.2.0.release ' Compile ' org.springframework.boot:spring-boot-starter-web:1.2.0. RELEASE ' compile ' org.springframework.boot:spring-boot-starter-actuator:1.2.0.release '}

2. Elasticsearch Configuration

@[email protected] (value = "Classpath:elasticsearch.properties") @EnableElasticsearchRepositories (basepackages = " Co.paan.repository ") public class Elasticsearchconfiguration {    @Resourceprivate environment environment;@ Beanpublic 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 ());}     }

You put Elasticsearch host and post in your application properties file.

Elasticsearch.host = localhost # If you use the local elasticsearch Hostelasticsearch.port = 9300

3. Data Mapping object:

In this application, we have 2 entities data Object Mapping:post and Tag

@Document (IndexName = "POST", type = "POST", shards = 1, replicas = 0) public class Post {@Idprivate 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;  }}

4. Repository:we extends from Elasticsearchrepository

Public interface Postrepository extends Elasticsearchrepository<post, string>{    page<post> Findbytagsname (String name, pageable pageable);}

5. Data Access Service

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{    @Autowiredprivate postrepository postrepository;@ Overridepublic 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);}   }

6. Testing and the result

 @Testpublic 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 ();p Ost.setid ("1");p Ost.settitle ("Bigining with spring boot application and Elasticsearch"); Post.settags (arrays.aslist (tag, tag2));p ostservice.save (POST); Post Post2 = new post ();p Ost2.setid ("1");p Ost2.settitle ("Bigining with Spring Boot application");p Ost2.settags ( Arrays.aslist (tag));p ostservice.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));} 

7. You can find detail project at Github:https://github.com/herotl2005/spring-data-elasticsearch-sample

The integration Zone is brought to your in partnership with 3scale. Learn how API providers has changed the "We think about integration in the Platform Vision of API Giants.

Reprint Address: Https://dzone.com/articles/first-step-spring-boot-and

http://blog.csdn.net/hong0220/article/details/50583409

Spring Boot + Elasticsearch

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.