1, Download and launch elastic search
Download address: https://www.elastic.co/downloads/past-releases
Select a version, Download
The blogger here tests 2.4.4
You can choose the ZIP package to download
There are some subtle differences between windows and mac when starting
windows: enter bin in the file directory, and then click elasticsearch.bat
mac: execute the command bin/elasticsearch on the terminal
2, Configure Maven
- <! -- Spring Boot Elasticsearch dependency -- >
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
- <properties>
- <elasticsearch.version>2.4.4</elasticsearch.version>
- </properties>
Add the spring boot starter data elastic search dependency and set the elastic search version to 2.4.4
ES and spingboot version reference:
3, Modify application.properties
#Project server.port=8080 debug=true server.context-path=/chuyun # DataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/chuyun?characterEncodeing=utf-8&useSSL=false # JPA spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update #Thymeleaf spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false spring.thymeleaf.cache-period=0 spring.template.cache=false spring.thymeleaf.mode=HTML5 spring.thymeleaf.prefix=classpath:templates/ spring.thymeleaf.suffix=.html #Elasticsearch spring.data.elasticsearch.cluster-nodes=localhost:9300 spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
Mainly add two last faces
4, Create ES Bean and Repository
Article.java
package com.liuyanzhao.chuyun.domain.es; import org.springframework.data.elasticsearch.annotations.Document; import javax.persistence.Id; import java.util.Date; /** * @author Speech and speech * @date 2018/1/22 4:45 p.m. */ @Document(indexName="chuyun",type="article",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1") public class Article { //Article id, which must be id here @Id private Long id; //Title private String title; //content private String content; //Browse volume private Integer viewCount; //Release time private Date createTime; //Update time private Date updateTime; public Article() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getViewCount() { return viewCount; } public void setViewCount(Integer viewCount) { this.viewCount = viewCount; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + ", viewCount=" + viewCount + ", createTime=" + createTime + ", updateTime=" + updateTime + '}'; } }
ArticleRepository.java
package com.liuyanzhao.chuyun.repository.es; import com.liuyanzhao.chuyun.domain.es.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * @author Speech and speech * @date 2018/1/22 5:05 p.m. */ public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { Page<Article> findDistinctByTitleContainingOrContentContaining(String title, String content, Pageable pageable); }
5, Create test class
ArticleRepositoryTest.java
package com.liuyanzhao.chuyun.repository.es; import com.liuyanzhao.chuyun.domain.es.Article; import com.liuyanzhao.chuyun.entity.User; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; /** * @author Speech and speech * @date 2018/1/21 5:03 p.m. */ @RunWith(SpringRunner.class) @SpringBootTest public class ArticleRepositoryTest { @Autowired private ArticleRepository articleRepository; @Before public void initRepositoryData() { //Clear all data articleRepository.deleteAll(); Article article = new Article(); article.setId((long) 1); article.setTitle("<Butterfly love flower"); article.setContent("The chrysanthemum on the threshold is worried about the smoke, the orchid is weeping the dew, the curtain is light and cold, and the swallow flies away. The bright moon does not know the bitterness of leaving hate, and the slanting light comes to the dawn to wear the Zhuhu. Last night, the west wind withered the green trees, alone on the high-rise, looking at the end of the world road. Where do you know if you want to send color paper and ruler?"); article.setCreateTime(new Date()); article.setUpdateTime(new Date()); article.setViewCount(678); articleRepository.save(article); Article article2 = new Article(); article2.setId((long) 2); article2.setTitle("<Butterfly love flower"); article2.setContent("Stand on the wind of the dangerous building, look at the extreme spring sorrow, dark sky. In the light of the grass and smoke, no one can stop by saying. I want to get drunk and sing when I am drunk. It's still tasteless. my clothes grow daily more loose, yet care I not. For you am I thus wasting away in sorrow and pain."); article2.setCreateTime(new Date()); article2.setUpdateTime(new Date()); article.setViewCount(367); articleRepository.save(article2); Article article3 = new Article(); article3.setId((long) 3); article3.setTitle("<Qing Yu An·Lunar New Year's Eve"); article3.setContent("East wind night flowers thousands of trees, but also blow down, the stars like rain. BMW carvings are full of fragrance. The sound of the Phoenix and the flute moves, the light of the jade pot turns, and the fish and the dragon dance all night. Moth son snow willow golden thread, smile Ying Ying Ying Xiang to go. Looking for him in the crowd, suddenly looking back, the man was in the dim light."); article3.setCreateTime(new Date()); article3.setUpdateTime(new Date()); article3.setViewCount(786); articleRepository.save(article3); } @Test public void findDistinctByTitleContainingOrContentContainingTest() throws Exception { Pageable pageable = new PageRequest(0,20); String title = "I love Luo Qi."; String content = "Flower tree"; Page<Article> page = articleRepository.findDistinctByTitleContainingOrContentContaining(title, content, pageable); System.out.println(page); System.out.println("---start---"); for(Article article : page.getContent()) { System.out.println(article.toString()); } System.out.println("---end---"); } }
Function @Test Method of annotation
Find a piece of data according to the title and content
Modify title and content
String title = "Butterfly Love"; String content = "Dongfeng";
Three pieces of data found
6, Visit http: / / localhost: 9200 / ﹣ plugin / head/
Because in the last article, we talked about installing a head plug-in. Now we can see the data in it (the redundant data was tested before)
7, New Controller class
ArticleController.java
package com.liuyanzhao.chuyun.controller; import com.liuyanzhao.chuyun.domain.es.Article; import com.liuyanzhao.chuyun.repository.es.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author Speech and speech * @date 2018/1/22 9:07 p.m. */ @RestController @RequestMapping("/article") public class ArticleController { @Autowired private ArticleRepository ArticleRepository; @RequestMapping("") public List<Article> list(@RequestParam(value = "title", required = false) String title, @RequestParam(value = "content", required = false) String content, @RequestParam(value = "pageIndex", defaultValue = "0") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) { Pageable pageable = new PageRequest(pageIndex, pageSize); Page<Article> page = ArticleRepository.findDistinctByTitleContainingOrContentContaining(title, content, pageable); return page.getContent(); } }
Because data has been added to elasticsearch in the test class before
All are now accessible on the browser:
http://localhost:8080/chuyun/article?title = langtaosha & content = Yiren