Record a small example of Springboot (2.0.4.RELEASE) +elasticsearch (6.2.4) +gradle integration.
1. Add dependencies to the relevant jar package within Gradle:
Compile (' Org.springframework.boot:spring-boot-starter-web ') compile (' Org.springframework.boot: Spring-boot-starter-thymeleaf ') compile (' ORG.SPRINGFRAMEWORK.BOOT:SPRING-BOOT-STARTER-DATA-JPA ')//Add Spring data Elasticsearch relies on compile (' org.springframework.boot:spring-boot-starter-data-elasticsearch ')//Add JNA dependency compile (' net.java.dev.jna:jna:4.3.0 ')
Compile (' Com.google.guava:guava:26.0-jre ')
2. Create the Entity object and add the relevant notes for Elasticsearch:
Package Com.wey.pojo.blog;import Java.io.serializable;import Org.springframework.data.annotation.id;import Org.springframework.data.elasticsearch.annotations.Document, @Document (indexname= "Blogcenter", type= "blog")// IndexName index name can be understood as the database name must be lowercase or it will be reported public class Blog implements serializable{private static final long Serialversionuid = 1L; @Id private String Id; Private String title; Private String Summary; Private String content; Protected Blog () {super (); Public Blog (string title, string summary, string content) {this.title = title; This.summary = Summary; this.content = content; } 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 String Getsummary () {return summary; } public void SetSuMmary (String summary) {this.summary = summary; } public String GetContent () {return content; The public void SetContent (String content) {this.content = content; } @Override Public String toString () {return "Blog [id=" + ID + ", title=" + title + ", summary=" + Summary + ", content=" + content + "]"; }}
3. Create Repository
Package Com.wey.repository;import Org.springframework.data.domain.page;import Org.springframework.data.domain.pageable;import Org.springframework.data.elasticsearch.repository.elasticsearchrepository;import Org.springframework.stereotype.component;import Com.wey.pojo.blog.blog;public Interface BlogRepository extends Elasticsearchrepository<blog, string> {}
4. Create a controller and simply implement add and query
@RestController @requestmapping ("/blogs") public class BlogController { @Autowired blogrepository Blogrepository; @RequestMapping ("/add") Public Blog Add (blog blog) { return blogrepository.save (blog); } @GetMapping public list<blog> findAll () { iterable<blog> elements = Blogrepository.findall (); arraylist<blog> list = lists.newarraylist (elements); return list; } @GetMapping ("/delete/{id}") Public string Remove (@PathVariable (name= "id") string id) { Blogrepository.deletebyid (ID); Return "Success"; }}
5. Open the Elasticsearch.bat file in the downloaded Elasticsearch (6.2.4) and wait a few moments until the boot is complete.
6. Start the Springboot application and simply test
Add a piece of data:
Query all data:
Springboot (2.0.4.RELEASE) +elasticsearch (6.2.4) +gradle Simple integration